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    
019    package org.apache.hadoop.io;
020    
021    import java.io.*;
022    
023    import org.apache.hadoop.classification.InterfaceAudience;
024    import org.apache.hadoop.classification.InterfaceStability;
025    
026    /** A WritableComparable for a single byte. */
027    @InterfaceAudience.Public
028    @InterfaceStability.Stable
029    public class ByteWritable implements WritableComparable<ByteWritable> {
030      private byte value;
031    
032      public ByteWritable() {}
033    
034      public ByteWritable(byte value) { set(value); }
035    
036      /** Set the value of this ByteWritable. */
037      public void set(byte value) { this.value = value; }
038    
039      /** Return the value of this ByteWritable. */
040      public byte get() { return value; }
041    
042      @Override
043      public void readFields(DataInput in) throws IOException {
044        value = in.readByte();
045      }
046    
047      @Override
048      public void write(DataOutput out) throws IOException {
049        out.writeByte(value);
050      }
051    
052      /** Returns true iff <code>o</code> is a ByteWritable with the same value. */
053      @Override
054      public boolean equals(Object o) {
055        if (!(o instanceof ByteWritable)) {
056          return false;
057        }
058        ByteWritable other = (ByteWritable)o;
059        return this.value == other.value;
060      }
061    
062      @Override
063      public int hashCode() {
064        return (int)value;
065      }
066    
067      /** Compares two ByteWritables. */
068      @Override
069      public int compareTo(ByteWritable o) {
070        int thisValue = this.value;
071        int thatValue = o.value;
072        return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
073      }
074    
075      @Override
076      public String toString() {
077        return Byte.toString(value);
078      }
079    
080      /** A Comparator optimized for ByteWritable. */ 
081      public static class Comparator extends WritableComparator {
082        public Comparator() {
083          super(ByteWritable.class);
084        }
085    
086        @Override
087        public int compare(byte[] b1, int s1, int l1,
088                           byte[] b2, int s2, int l2) {
089          byte thisValue = b1[s1];
090          byte thatValue = b2[s2];
091          return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
092        }
093      }
094    
095      static {                                        // register this comparator
096        WritableComparator.define(ByteWritable.class, new Comparator());
097      }
098    }
099