PageRenderTime 45ms CodeModel.GetById 20ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. * This file is back-ported from hadoop-0.19, to make sure hive can run
  20. * with hadoop-0.17.
  21. */
  22. package org.apache.hadoop.hive.serde2.io;
  23. import java.io.DataInput;
  24. import java.io.DataOutput;
  25. import java.io.IOException;
  26. import org.apache.hadoop.io.WritableComparable;
  27. import org.apache.hadoop.io.WritableComparator;
  28. /**
  29. * Writable for Double values.
  30. */
  31. public class DoubleWritable implements WritableComparable {
  32. private double value = 0.0;
  33. public DoubleWritable() {
  34. }
  35. public DoubleWritable(double value) {
  36. set(value);
  37. }
  38. public void readFields(DataInput in) throws IOException {
  39. value = in.readDouble();
  40. }
  41. public void write(DataOutput out) throws IOException {
  42. out.writeDouble(value);
  43. }
  44. public void set(double value) {
  45. this.value = value;
  46. }
  47. public double get() {
  48. return value;
  49. }
  50. /**
  51. * Returns true iff <code>o</code> is a DoubleWritable with the same value.
  52. */
  53. @Override
  54. public boolean equals(Object o) {
  55. if (!(o instanceof DoubleWritable)) {
  56. return false;
  57. }
  58. DoubleWritable other = (DoubleWritable) o;
  59. return value == other.value;
  60. }
  61. @Override
  62. public int hashCode() {
  63. long v = Double.doubleToLongBits(value);
  64. return (int) (v ^ (v >>> 32));
  65. }
  66. public int compareTo(Object o) {
  67. DoubleWritable other = (DoubleWritable) o;
  68. return (value < other.value ? -1 : (value == other.value ? 0 : 1));
  69. }
  70. @Override
  71. public String toString() {
  72. return Double.toString(value);
  73. }
  74. /** A Comparator optimized for DoubleWritable. */
  75. public static class Comparator extends WritableComparator {
  76. public Comparator() {
  77. super(DoubleWritable.class);
  78. }
  79. @Override
  80. public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
  81. double thisValue = readDouble(b1, s1);
  82. double thatValue = readDouble(b2, s2);
  83. return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
  84. }
  85. }
  86. static { // register this comparator
  87. WritableComparator.define(DoubleWritable.class, new Comparator());
  88. }
  89. }