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.io; |
---|
20 | |
---|
21 | import java.io.*; |
---|
22 | |
---|
23 | /** A WritableComparable for ints. */ |
---|
24 | public class IntWritable implements WritableComparable { |
---|
25 | private int value; |
---|
26 | |
---|
27 | public IntWritable() {} |
---|
28 | |
---|
29 | public IntWritable(int value) { set(value); } |
---|
30 | |
---|
31 | /** Set the value of this IntWritable. */ |
---|
32 | public void set(int value) { this.value = value; } |
---|
33 | |
---|
34 | /** Return the value of this IntWritable. */ |
---|
35 | public int get() { return value; } |
---|
36 | |
---|
37 | public void readFields(DataInput in) throws IOException { |
---|
38 | value = in.readInt(); |
---|
39 | } |
---|
40 | |
---|
41 | public void write(DataOutput out) throws IOException { |
---|
42 | out.writeInt(value); |
---|
43 | } |
---|
44 | |
---|
45 | /** Returns true iff <code>o</code> is a IntWritable with the same value. */ |
---|
46 | public boolean equals(Object o) { |
---|
47 | if (!(o instanceof IntWritable)) |
---|
48 | return false; |
---|
49 | IntWritable other = (IntWritable)o; |
---|
50 | return this.value == other.value; |
---|
51 | } |
---|
52 | |
---|
53 | public int hashCode() { |
---|
54 | return value; |
---|
55 | } |
---|
56 | |
---|
57 | /** Compares two IntWritables. */ |
---|
58 | public int compareTo(Object o) { |
---|
59 | int thisValue = this.value; |
---|
60 | int thatValue = ((IntWritable)o).value; |
---|
61 | return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1)); |
---|
62 | } |
---|
63 | |
---|
64 | public String toString() { |
---|
65 | return Integer.toString(value); |
---|
66 | } |
---|
67 | |
---|
68 | /** A Comparator optimized for IntWritable. */ |
---|
69 | public static class Comparator extends WritableComparator { |
---|
70 | public Comparator() { |
---|
71 | super(IntWritable.class); |
---|
72 | } |
---|
73 | |
---|
74 | public int compare(byte[] b1, int s1, int l1, |
---|
75 | byte[] b2, int s2, int l2) { |
---|
76 | int thisValue = readInt(b1, s1); |
---|
77 | int thatValue = readInt(b2, s2); |
---|
78 | return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1)); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | static { // register this comparator |
---|
83 | WritableComparator.define(IntWritable.class, new Comparator()); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|