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.DataInput;
022import java.io.DataOutput;
023import java.io.IOException;
024
025import org.apache.hadoop.classification.InterfaceAudience;
026import org.apache.hadoop.classification.InterfaceStability;
027
028/**
029 * Writable for Double values.
030 */
031@InterfaceAudience.Public
032@InterfaceStability.Stable
033public class DoubleWritable implements WritableComparable<DoubleWritable> {
034
035  private double value = 0.0;
036  
037  public DoubleWritable() {
038    
039  }
040  
041  public DoubleWritable(double value) {
042    set(value);
043  }
044  
045  public void readFields(DataInput in) throws IOException {
046    value = in.readDouble();
047  }
048
049  public void write(DataOutput out) throws IOException {
050    out.writeDouble(value);
051  }
052  
053  public void set(double value) { this.value = value; }
054  
055  public double get() { return value; }
056
057  /**
058   * Returns true iff <code>o</code> is a DoubleWritable with the same value.
059   */
060  @Override
061  public boolean equals(Object o) {
062    if (!(o instanceof DoubleWritable)) {
063      return false;
064    }
065    DoubleWritable other = (DoubleWritable)o;
066    return this.value == other.value;
067  }
068  
069  @Override
070  public int hashCode() {
071    return (int)Double.doubleToLongBits(value);
072  }
073  
074  @Override
075  public int compareTo(DoubleWritable o) {
076    return (value < o.value ? -1 : (value == o.value ? 0 : 1));
077  }
078  
079  @Override
080  public String toString() {
081    return Double.toString(value);
082  }
083
084  /** A Comparator optimized for DoubleWritable. */ 
085  public static class Comparator extends WritableComparator {
086    public Comparator() {
087      super(DoubleWritable.class);
088    }
089
090    @Override
091    public int compare(byte[] b1, int s1, int l1,
092                       byte[] b2, int s2, int l2) {
093      double thisValue = readDouble(b1, s1);
094      double thatValue = readDouble(b2, s2);
095      return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
096    }
097  }
098
099  static {                                        // register this comparator
100    WritableComparator.define(DoubleWritable.class, new Comparator());
101  }
102
103}
104