001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.types;
019
020import org.apache.hadoop.hbase.util.Bytes;
021import org.apache.hadoop.hbase.util.Order;
022import org.apache.hadoop.hbase.util.PositionedByteRange;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * An {@code DataType} for interacting with values encoded using
027 * {@link Bytes#putDouble(byte[], int, double)}. Intended to make it easier to transition away from
028 * direct use of {@link Bytes}.
029 * @see Bytes#putDouble(byte[], int, double)
030 * @see Bytes#toDouble(byte[])
031 */
032@InterfaceAudience.Public
033public class RawDouble implements DataType<Double> {
034
035  @Override
036  public boolean isOrderPreserving() {
037    return false;
038  }
039
040  @Override
041  public Order getOrder() {
042    return null;
043  }
044
045  @Override
046  public boolean isNullable() {
047    return false;
048  }
049
050  @Override
051  public boolean isSkippable() {
052    return true;
053  }
054
055  @Override
056  public int encodedLength(Double val) {
057    return Bytes.SIZEOF_DOUBLE;
058  }
059
060  @Override
061  public Class<Double> encodedClass() {
062    return Double.class;
063  }
064
065  @Override
066  public int skip(PositionedByteRange src) {
067    src.setPosition(src.getPosition() + Bytes.SIZEOF_DOUBLE);
068    return Bytes.SIZEOF_DOUBLE;
069  }
070
071  @Override
072  public Double decode(PositionedByteRange src) {
073    double val = Bytes.toDouble(src.getBytes(), src.getOffset() + src.getPosition());
074    skip(src);
075    return val;
076  }
077
078  @Override
079  public int encode(PositionedByteRange dst, Double val) {
080    Bytes.putDouble(dst.getBytes(), dst.getOffset() + dst.getPosition(), val);
081    return skip(dst);
082  }
083
084  /**
085   * Read a {@code double} value from the buffer {@code buff}.
086   */
087  public double decodeDouble(byte[] buff, int offset) {
088    double val = Bytes.toDouble(buff, offset);
089    return val;
090  }
091
092  /**
093   * Write instance {@code val} into buffer {@code buff}.
094   */
095  public int encodeDouble(byte[] buff, int offset, double val) {
096    return Bytes.putDouble(buff, offset, val);
097  }
098}