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