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

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

#
Java | 101 lines | 34 code | 8 blank | 59 comment | 2 complexity | 69080cb2777a7d7df41e98c654755c40 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 */
 18package org.apache.hadoop.hive.serde2.lazy;
 19
 20import org.apache.hadoop.hive.serde2.io.ShortWritable;
 21import org.apache.hadoop.hive.serde2.lazy.objectinspector.primitive.LazyShortObjectInspector;
 22
 23/**
 24 * LazyObject for storing a value of Short.
 25 * 
 26 * <p>
 27 * Part of the code is adapted from Apache Harmony Project.
 28 * 
 29 * As with the specification, this implementation relied on code laid out in <a
 30 * href="http://www.hackersdelight.org/">Henry S. Warren, Jr.'s Hacker's
 31 * Delight, (Addison Wesley, 2002)</a> as well as <a
 32 * href="http://aggregate.org/MAGIC/">The Aggregate's Magic Algorithms</a>.
 33 * </p>
 34 * 
 35 */
 36public class LazyShort extends
 37    LazyPrimitive<LazyShortObjectInspector, ShortWritable> {
 38
 39  public LazyShort(LazyShortObjectInspector oi) {
 40    super(oi);
 41    data = new ShortWritable();
 42  }
 43
 44  public LazyShort(LazyShort copy) {
 45    super(copy);
 46    data = new ShortWritable(copy.data.get());
 47  }
 48
 49  @Override
 50  public void init(ByteArrayRef bytes, int start, int length) {
 51    try {
 52      data.set(parseShort(bytes.getData(), start, length));
 53      isNull = false;
 54    } catch (NumberFormatException e) {
 55      isNull = true;
 56    }
 57  }
 58
 59  /**
 60   * Parses the string argument as if it was a short value and returns the
 61   * result. Throws NumberFormatException if the string does not represent an
 62   * short quantity.
 63   * 
 64   * @param bytes
 65   * @param start
 66   * @param length
 67   *          a UTF-8 encoded string representation of a short quantity.
 68   * @return short the value represented by the argument
 69   * @exception NumberFormatException
 70   *              if the argument could not be parsed as a short quantity.
 71   */
 72  public static short parseShort(byte[] bytes, int start, int length) {
 73    return parseShort(bytes, start, length, 10);
 74  }
 75
 76  /**
 77   * Parses the string argument as if it was a short value and returns the
 78   * result. Throws NumberFormatException if the string does not represent a
 79   * single short quantity. The second argument specifies the radix to use when
 80   * parsing the value.
 81   * 
 82   * @param bytes
 83   * @param start
 84   * @param length
 85   *          a UTF-8 encoded string representation of a short quantity.
 86   * @param radix
 87   *          the radix to use when parsing.
 88   * @return short the value represented by the argument
 89   * @exception NumberFormatException
 90   *              if the argument could not be parsed as a short quantity.
 91   */
 92  public static short parseShort(byte[] bytes, int start, int length, int radix) {
 93    int intValue = LazyInteger.parseInt(bytes, start, length, radix);
 94    short result = (short) intValue;
 95    if (result == intValue) {
 96      return result;
 97    }
 98    throw new NumberFormatException();
 99  }
100
101}