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 longs. */
030@InterfaceAudience.Public
031@InterfaceStability.Stable
032public class LongWritable implements WritableComparable<LongWritable> {
033  private long value;
034
035  public LongWritable() {}
036
037  public LongWritable(long value) { set(value); }
038
039  /** Set the value of this LongWritable. */
040  public void set(long value) { this.value = value; }
041
042  /** Return the value of this LongWritable. */
043  public long get() { return value; }
044
045  public void readFields(DataInput in) throws IOException {
046    value = in.readLong();
047  }
048
049  public void write(DataOutput out) throws IOException {
050    out.writeLong(value);
051  }
052
053  /** Returns true iff <code>o</code> is a LongWritable with the same value. */
054  public boolean equals(Object o) {
055    if (!(o instanceof LongWritable))
056      return false;
057    LongWritable other = (LongWritable)o;
058    return this.value == other.value;
059  }
060
061  public int hashCode() {
062    return (int)value;
063  }
064
065  /** Compares two LongWritables. */
066  public int compareTo(LongWritable o) {
067    long thisValue = this.value;
068    long thatValue = o.value;
069    return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
070  }
071
072  public String toString() {
073    return Long.toString(value);
074  }
075
076  /** A Comparator optimized for LongWritable. */ 
077  public static class Comparator extends WritableComparator {
078    public Comparator() {
079      super(LongWritable.class);
080    }
081
082    public int compare(byte[] b1, int s1, int l1,
083                       byte[] b2, int s2, int l2) {
084      long thisValue = readLong(b1, s1);
085      long thatValue = readLong(b2, s2);
086      return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
087    }
088  }
089
090  /** A decreasing Comparator optimized for LongWritable. */ 
091  public static class DecreasingComparator extends Comparator {
092    
093    @Override
094    public int compare(WritableComparable a, WritableComparable b) {
095      return -super.compare(a, b);
096    }
097    public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
098      return -super.compare(b1, s1, l1, b2, s2, l2);
099    }
100  }
101
102  static {                                       // register default comparator
103    WritableComparator.define(LongWritable.class, new Comparator());
104  }
105
106}
107