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.*; 022 023import org.apache.hadoop.classification.InterfaceAudience; 024import 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 033public 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 @Override 047 public void readFields(DataInput in) throws IOException { 048 value = WritableUtils.readVInt(in); 049 } 050 051 @Override 052 public void write(DataOutput out) throws IOException { 053 WritableUtils.writeVInt(out, value); 054 } 055 056 /** Returns true iff <code>o</code> is a VIntWritable with the same value. */ 057 @Override 058 public boolean equals(Object o) { 059 if (!(o instanceof VIntWritable)) 060 return false; 061 VIntWritable other = (VIntWritable)o; 062 return this.value == other.value; 063 } 064 065 @Override 066 public int hashCode() { 067 return value; 068 } 069 070 /** Compares two VIntWritables. */ 071 @Override 072 public int compareTo(VIntWritable o) { 073 int thisValue = this.value; 074 int thatValue = o.value; 075 return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1)); 076 } 077 078 @Override 079 public String toString() { 080 return Integer.toString(value); 081 } 082 083} 084