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#putFloat(byte[], int, float)}. Intended to make it easier to
028 * transition away from direct use of {@link Bytes}.
029 * @see Bytes#putFloat(byte[], int, float)
030 * @see Bytes#toFloat(byte[])
031 */
032@InterfaceAudience.Public
033public class RawFloat implements DataType<Float> {
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(Float val) {
057    return Bytes.SIZEOF_FLOAT;
058  }
059
060  @Override
061  public Class<Float> encodedClass() {
062    return Float.class;
063  }
064
065  @Override
066  public int skip(PositionedByteRange src) {
067    src.setPosition(src.getPosition() + Bytes.SIZEOF_FLOAT);
068    return Bytes.SIZEOF_FLOAT;
069  }
070
071  @Override
072  public Float decode(PositionedByteRange src) {
073    float val = Bytes.toFloat(src.getBytes(), src.getOffset() + src.getPosition());
074    skip(src);
075    return val;
076  }
077
078  @Override
079  public int encode(PositionedByteRange dst, Float val) {
080    Bytes.putFloat(dst.getBytes(), dst.getOffset() + dst.getPosition(), val);
081    return skip(dst);
082  }
083
084  /**
085   * Read a {@code float} value from the buffer {@code buff}.
086   */
087  public float decodeFloat(byte[] buff, int offset) {
088    return Bytes.toFloat(buff, offset);
089  }
090
091  /**
092   * Write instance {@code val} into buffer {@code buff}.
093   */
094  public int encodeFloat(byte[] buff, int offset, float val) {
095    return Bytes.putFloat(buff, offset, val);
096  }
097}