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