View Javadoc

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.hbase.types;
19  
20  import org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.classification.InterfaceStability;
22  import org.apache.hadoop.hbase.util.Bytes;
23  import org.apache.hadoop.hbase.util.Order;
24  import org.apache.hadoop.hbase.util.PositionedByteRange;
25  
26  /**
27   * An {@code DataType} for interacting with values encoded using
28   * {@link Bytes#putShort(byte[], int, short)}. Intended to make it easier to
29   * transition away from direct use of {@link Bytes}.
30   * @see Bytes#putShort(byte[], int, short)
31   * @see Bytes#toShort(byte[])
32   */
33  @InterfaceAudience.Public
34  @InterfaceStability.Evolving
35  public class RawShort implements DataType<Short> {
36  
37    @Override
38    public boolean isOrderPreserving() { return false; }
39  
40    @Override
41    public Order getOrder() { return null; }
42  
43    @Override
44    public boolean isNullable() { return false; }
45  
46    @Override
47    public boolean isSkippable() { return true; }
48  
49    @Override
50    public int encodedLength(Short val) { return Bytes.SIZEOF_SHORT; }
51  
52    @Override
53    public Class<Short> encodedClass() { return Short.class; }
54  
55    @Override
56    public int skip(PositionedByteRange src) {
57      src.setPosition(src.getPosition() + Bytes.SIZEOF_SHORT);
58      return Bytes.SIZEOF_SHORT;
59    }
60  
61    @Override
62    public Short decode(PositionedByteRange src) {
63      short val = Bytes.toShort(src.getBytes(), src.getOffset() + src.getPosition());
64      skip(src);
65      return val;
66    }
67  
68    @Override
69    public int encode(PositionedByteRange dst, Short val) {
70      Bytes.putShort(dst.getBytes(), dst.getOffset() + dst.getPosition(), val);
71      return skip(dst);
72    }
73  
74    /**
75     * Read a {@code short} value from the buffer {@code buff}.
76     */
77    public short decodeShort(byte[] buff, int offset) {
78      return Bytes.toShort(buff, offset);
79    }
80  
81    /**
82     * Write instance {@code val} into buffer {@code buff}.
83     */
84    public int encodeShort(byte[] buff, int offset, short val) {
85      return Bytes.putShort(buff, offset, val);
86    }
87  }