001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.mapreduce;
020
021import java.io.DataInput;
022import java.io.DataOutput;
023import java.io.IOException;
024
025import org.apache.hadoop.classification.InterfaceAudience;
026import org.apache.hadoop.classification.InterfaceStability;
027import org.apache.hadoop.io.WritableComparable;
028
029/**
030 * A general identifier, which internally stores the id
031 * as an integer. This is the super class of {@link JobID}, 
032 * {@link TaskID} and {@link TaskAttemptID}.
033 * 
034 * @see JobID
035 * @see TaskID
036 * @see TaskAttemptID
037 */
038@InterfaceAudience.Public
039@InterfaceStability.Stable
040public abstract class ID implements WritableComparable<ID> {
041  protected static final char SEPARATOR = '_';
042  protected int id;
043
044  /** constructs an ID object from the given int */
045  public ID(int id) {
046    this.id = id;
047  }
048
049  protected ID() {
050  }
051
052  /** returns the int which represents the identifier */
053  public int getId() {
054    return id;
055  }
056
057  @Override
058  public String toString() {
059    return String.valueOf(id);
060  }
061
062  @Override
063  public int hashCode() {
064    return id;
065  }
066
067  @Override
068  public boolean equals(Object o) {
069    if (this == o)
070      return true;
071    if(o == null)
072      return false;
073    if (o.getClass() == this.getClass()) {
074      ID that = (ID) o;
075      return this.id == that.id;
076    }
077    else
078      return false;
079  }
080
081  /** Compare IDs by associated numbers */
082  public int compareTo(ID that) {
083    return this.id - that.id;
084  }
085
086  public void readFields(DataInput in) throws IOException {
087    this.id = in.readInt();
088  }
089
090  public void write(DataOutput out) throws IOException {
091    out.writeInt(id);
092  }
093  
094}