PageRenderTime 13ms CodeModel.GetById 7ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

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