1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | |
---|
19 | package org.apache.hadoop.mapreduce; |
---|
20 | |
---|
21 | import java.io.DataInput; |
---|
22 | import java.io.DataOutput; |
---|
23 | import java.io.IOException; |
---|
24 | |
---|
25 | import org.apache.hadoop.io.WritableComparable; |
---|
26 | |
---|
27 | /** |
---|
28 | * A general identifier, which internally stores the id |
---|
29 | * as an integer. This is the super class of {@link JobID}, |
---|
30 | * {@link TaskID} and {@link TaskAttemptID}. |
---|
31 | * |
---|
32 | * @see JobID |
---|
33 | * @see TaskID |
---|
34 | * @see TaskAttemptID |
---|
35 | */ |
---|
36 | public abstract class ID implements WritableComparable<ID> { |
---|
37 | protected static final char SEPARATOR = '_'; |
---|
38 | protected int id; |
---|
39 | |
---|
40 | /** constructs an ID object from the given int */ |
---|
41 | public ID(int id) { |
---|
42 | this.id = id; |
---|
43 | } |
---|
44 | |
---|
45 | protected ID() { |
---|
46 | } |
---|
47 | |
---|
48 | /** returns the int which represents the identifier */ |
---|
49 | public int getId() { |
---|
50 | return id; |
---|
51 | } |
---|
52 | |
---|
53 | @Override |
---|
54 | public String toString() { |
---|
55 | return String.valueOf(id); |
---|
56 | } |
---|
57 | |
---|
58 | @Override |
---|
59 | public int hashCode() { |
---|
60 | return Integer.valueOf(id).hashCode(); |
---|
61 | } |
---|
62 | |
---|
63 | @Override |
---|
64 | public boolean equals(Object o) { |
---|
65 | if (this == o) |
---|
66 | return true; |
---|
67 | if(o == null) |
---|
68 | return false; |
---|
69 | if (o.getClass() == this.getClass()) { |
---|
70 | ID that = (ID) o; |
---|
71 | return this.id == that.id; |
---|
72 | } |
---|
73 | else |
---|
74 | return false; |
---|
75 | } |
---|
76 | |
---|
77 | /** Compare IDs by associated numbers */ |
---|
78 | public int compareTo(ID that) { |
---|
79 | return this.id - that.id; |
---|
80 | } |
---|
81 | |
---|
82 | public void readFields(DataInput in) throws IOException { |
---|
83 | this.id = in.readInt(); |
---|
84 | } |
---|
85 | |
---|
86 | public void write(DataOutput out) throws IOException { |
---|
87 | out.writeInt(id); |
---|
88 | } |
---|
89 | |
---|
90 | } |
---|