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