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  @Override
046  public void readFields(DataInput in) throws IOException {
047    value = in.readInt();
048  }
049
050  @Override
051  public void write(DataOutput out) throws IOException {
052    out.writeInt(value);
053  }
054
055  /** Returns true iff <code>o</code> is a IntWritable with the same value. */
056  @Override
057  public boolean equals(Object o) {
058    if (!(o instanceof IntWritable))
059      return false;
060    IntWritable other = (IntWritable)o;
061    return this.value == other.value;
062  }
063
064  @Override
065  public int hashCode() {
066    return value;
067  }
068
069  /** Compares two IntWritables. */
070  @Override
071  public int compareTo(IntWritable o) {
072    int thisValue = this.value;
073    int thatValue = o.value;
074    return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
075  }
076
077  @Override
078  public String toString() {
079    return Integer.toString(value);
080  }
081
082  /** A Comparator optimized for IntWritable. */ 
083  public static class Comparator extends WritableComparator {
084    public Comparator() {
085      super(IntWritable.class);
086    }
087    
088    @Override
089    public int compare(byte[] b1, int s1, int l1,
090                       byte[] b2, int s2, int l2) {
091      int thisValue = readInt(b1, s1);
092      int thatValue = readInt(b2, s2);
093      return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
094    }
095  }
096
097  static {                                        // register this comparator
098    WritableComparator.define(IntWritable.class, new Comparator());
099  }
100}
101