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