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