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