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