PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/exec/ByteWritable.java

#
Java | 107 lines | 60 code | 16 blank | 31 comment | 7 complexity | 71f3158c67ee6645c86624647fd21a89 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. package org.apache.hadoop.hive.ql.exec;
  19. import java.io.DataInput;
  20. import java.io.DataOutput;
  21. import java.io.IOException;
  22. import org.apache.hadoop.io.WritableComparable;
  23. import org.apache.hadoop.io.WritableComparator;
  24. /**
  25. * ByteWritable.
  26. *
  27. */
  28. public class ByteWritable implements WritableComparable {
  29. private int value;
  30. public void write(DataOutput out) throws IOException {
  31. out.writeByte(value);
  32. }
  33. public void readFields(DataInput in) throws IOException {
  34. value = in.readByte();
  35. }
  36. public ByteWritable(int b) {
  37. value = b & 0xff;
  38. }
  39. public ByteWritable() {
  40. value = 0;
  41. }
  42. public void set(int b) {
  43. value = b & 0xff;
  44. }
  45. /** Compares two ByteWritables. */
  46. public int compareTo(Object o) {
  47. int thisValue = value;
  48. int thatValue = ((ByteWritable) o).value;
  49. return (thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
  50. }
  51. @Override
  52. public boolean equals(Object o) {
  53. if (!(o instanceof ByteWritable)) {
  54. return false;
  55. }
  56. ByteWritable that = (ByteWritable) o;
  57. if (this == that) {
  58. return true;
  59. }
  60. if (value == that.value) {
  61. return true;
  62. } else {
  63. return false;
  64. }
  65. }
  66. @Override
  67. public int hashCode() {
  68. return (value);
  69. }
  70. /** A Comparator optimized for BytesWritable. */
  71. public static class Comparator extends WritableComparator {
  72. public Comparator() {
  73. super(ByteWritable.class);
  74. }
  75. /**
  76. * Compare the buffers in serialized form.
  77. */
  78. @Override
  79. public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
  80. /**
  81. * ok - we are implementing a dummy byte int a = b1[s1] & 0xff; int b =
  82. * b2[s1] & 0xff; if(a!=b) return a -b;
  83. */
  84. return 0;
  85. }
  86. }
  87. static {
  88. // register this comparator
  89. WritableComparator.define(ByteWritable.class, new Comparator());
  90. }
  91. }