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 integer values stored in variable-length format.
027 * Such values take between one and five bytes. Smaller values take fewer bytes.
028 *
029 * @see org.apache.hadoop.io.WritableUtils#readVInt(DataInput)
030 */
031 @InterfaceAudience.Public
032 @InterfaceStability.Stable
033 public class VIntWritable implements WritableComparable<VIntWritable> {
034 private int value;
035
036 public VIntWritable() {}
037
038 public VIntWritable(int value) { set(value); }
039
040 /** Set the value of this VIntWritable. */
041 public void set(int value) { this.value = value; }
042
043 /** Return the value of this VIntWritable. */
044 public int get() { return value; }
045
046 public void readFields(DataInput in) throws IOException {
047 value = WritableUtils.readVInt(in);
048 }
049
050 public void write(DataOutput out) throws IOException {
051 WritableUtils.writeVInt(out, value);
052 }
053
054 /** Returns true iff <code>o</code> is a VIntWritable with the same value. */
055 @Override
056 public boolean equals(Object o) {
057 if (!(o instanceof VIntWritable))
058 return false;
059 VIntWritable other = (VIntWritable)o;
060 return this.value == other.value;
061 }
062
063 @Override
064 public int hashCode() {
065 return value;
066 }
067
068 /** Compares two VIntWritables. */
069 @Override
070 public int compareTo(VIntWritable o) {
071 int thisValue = this.value;
072 int thatValue = o.value;
073 return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
074 }
075
076 @Override
077 public String toString() {
078 return Integer.toString(value);
079 }
080
081 }
082