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.Order;
021import org.apache.hadoop.hbase.util.OrderedBytes;
022import org.apache.hadoop.hbase.util.PositionedByteRange;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * A {@code double} of 64-bits using a fixed-length encoding. Built on
027 * {@link OrderedBytes#encodeFloat64(PositionedByteRange, double, Order)}.
028 */
029@InterfaceAudience.Public
030public class OrderedFloat64 extends OrderedBytesBase<Double> {
031  /**
032   * @deprecated since 3.0.0 and will be removed in 4.0.0
033   */
034  @Deprecated
035  public static final OrderedFloat64 ASCENDING = new OrderedFloat64(Order.ASCENDING);
036  /**
037   * @deprecated since 3.0.0 and will be removed in 4.0.0
038   */
039  @Deprecated
040  public static final OrderedFloat64 DESCENDING = new OrderedFloat64(Order.DESCENDING);
041
042  /**
043   * Creates a new 64-bite {@code double} with a fixed-length encoding.
044   *
045   * @param order the {@link Order} to use
046   */
047  public OrderedFloat64(Order order) {
048    super(order);
049  }
050
051  @Override
052  public boolean isNullable() {
053    return false;
054  }
055
056  @Override
057  public int encodedLength(Double val) {
058    return 9;
059  }
060
061  @Override
062  public Class<Double> encodedClass() {
063    return Double.class;
064  }
065
066  @Override
067  public Double decode(PositionedByteRange src) {
068    return OrderedBytes.decodeFloat64(src);
069  }
070
071  @Override
072  public int encode(PositionedByteRange dst, Double val) {
073    if (null == val) {
074      throw new IllegalArgumentException("Null values not supported.");
075    }
076    return OrderedBytes.encodeFloat64(dst, val, order);
077  }
078
079  /**
080   * Read a {@code double} value from the buffer {@code src}.
081   *
082   * @param src the {@link PositionedByteRange} to read the {@code double} from
083   * @return the {@code double} floating-point value with the same bit pattern
084   */
085  public double decodeDouble(PositionedByteRange src) {
086    return OrderedBytes.decodeFloat64(src);
087  }
088
089  /**
090   * Write instance {@code val} into buffer {@code dst}.
091   *
092   * @param dst the {@link PositionedByteRange} to write to
093   * @param val the value to write to {@code dst}
094   * @return the number of bytes written
095   */
096  public int encodeDouble(PositionedByteRange dst, double val) {
097    return OrderedBytes.encodeFloat64(dst, val, order);
098  }
099}