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