PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/serde/src/java/org/apache/hadoop/hive/serde2/io/DoubleWritable.java

#
Java | 109 lines | 61 code | 20 blank | 28 comment | 4 complexity | acd1e542cd61a77c31ba172d3da78070 MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19/**
 20 * This file is back-ported from hadoop-0.19, to make sure hive can run 
 21 * with hadoop-0.17.
 22 */
 23package org.apache.hadoop.hive.serde2.io;
 24
 25import java.io.DataInput;
 26import java.io.DataOutput;
 27import java.io.IOException;
 28
 29import org.apache.hadoop.io.WritableComparable;
 30import org.apache.hadoop.io.WritableComparator;
 31
 32/**
 33 * Writable for Double values.
 34 */
 35public class DoubleWritable implements WritableComparable {
 36
 37  private double value = 0.0;
 38
 39  public DoubleWritable() {
 40
 41  }
 42
 43  public DoubleWritable(double value) {
 44    set(value);
 45  }
 46
 47  public void readFields(DataInput in) throws IOException {
 48    value = in.readDouble();
 49  }
 50
 51  public void write(DataOutput out) throws IOException {
 52    out.writeDouble(value);
 53  }
 54
 55  public void set(double value) {
 56    this.value = value;
 57  }
 58
 59  public double get() {
 60    return value;
 61  }
 62
 63  /**
 64   * Returns true iff <code>o</code> is a DoubleWritable with the same value.
 65   */
 66  @Override
 67  public boolean equals(Object o) {
 68    if (!(o instanceof DoubleWritable)) {
 69      return false;
 70    }
 71    DoubleWritable other = (DoubleWritable) o;
 72    return value == other.value;
 73  }
 74
 75  @Override
 76  public int hashCode() {
 77    long v = Double.doubleToLongBits(value);
 78    return (int) (v ^ (v >>> 32));
 79  }
 80
 81  public int compareTo(Object o) {
 82    DoubleWritable other = (DoubleWritable) o;
 83    return (value < other.value ? -1 : (value == other.value ? 0 : 1));
 84  }
 85
 86  @Override
 87  public String toString() {
 88    return Double.toString(value);
 89  }
 90
 91  /** A Comparator optimized for DoubleWritable. */
 92  public static class Comparator extends WritableComparator {
 93    public Comparator() {
 94      super(DoubleWritable.class);
 95    }
 96
 97    @Override
 98    public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
 99      double thisValue = readDouble(b1, s1);
100      double thatValue = readDouble(b2, s2);
101      return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
102    }
103  }
104
105  static { // register this comparator
106    WritableComparator.define(DoubleWritable.class, new Comparator());
107  }
108
109}