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