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; 019 020import java.math.BigDecimal; 021import java.util.Objects; 022import org.apache.yetus.audience.InterfaceAudience; 023 024import org.apache.hbase.thirdparty.com.google.common.base.Preconditions; 025 026/** 027 * It is used to represent the size with different units. This class doesn't serve for the precise 028 * computation. 029 */ 030@InterfaceAudience.Public 031public final class Size implements Comparable<Size> { 032 public static final Size ZERO = new Size(0, Unit.KILOBYTE); 033 private static final BigDecimal SCALE_BASE = BigDecimal.valueOf(1024D); 034 035 public enum Unit { 036 // keep the room to add more units for HBase 10.x 037 PETABYTE(100, "PB"), 038 TERABYTE(99, "TB"), 039 GIGABYTE(98, "GB"), 040 MEGABYTE(97, "MB"), 041 KILOBYTE(96, "KB"), 042 BYTE(95, "B"); 043 044 private final int orderOfSize; 045 private final String simpleName; 046 047 Unit(int orderOfSize, String simpleName) { 048 this.orderOfSize = orderOfSize; 049 this.simpleName = simpleName; 050 } 051 052 public int getOrderOfSize() { 053 return orderOfSize; 054 } 055 056 public String getSimpleName() { 057 return simpleName; 058 } 059 } 060 061 private final double value; 062 private final Unit unit; 063 064 public Size(double value, Unit unit) { 065 if (value < 0) { 066 throw new IllegalArgumentException("The value:" + value + " can't be negative"); 067 } 068 this.value = value; 069 this.unit = Preconditions.checkNotNull(unit); 070 } 071 072 /** Returns size unit */ 073 public Unit getUnit() { 074 return unit; 075 } 076 077 /** 078 * get the value 079 */ 080 public long getLongValue() { 081 return (long) value; 082 } 083 084 /** 085 * get the value 086 */ 087 public double get() { 088 return value; 089 } 090 091 /** 092 * get the value which is converted to specified unit. 093 * @param unit size unit 094 * @return the converted value 095 */ 096 public double get(Unit unit) { 097 if (value == 0) { 098 return value; 099 } 100 int diff = this.unit.getOrderOfSize() - unit.getOrderOfSize(); 101 if (diff == 0) { 102 return value; 103 } 104 105 BigDecimal rval = BigDecimal.valueOf(value); 106 for (int i = 0; i != Math.abs(diff); ++i) { 107 rval = diff > 0 ? rval.multiply(SCALE_BASE) : rval.divide(SCALE_BASE); 108 } 109 return rval.doubleValue(); 110 } 111 112 @Override 113 public int compareTo(Size other) { 114 int diff = unit.getOrderOfSize() - other.unit.getOrderOfSize(); 115 if (diff == 0) { 116 return Double.compare(value, other.value); 117 } 118 119 BigDecimal thisValue = BigDecimal.valueOf(value); 120 BigDecimal otherValue = BigDecimal.valueOf(other.value); 121 if (diff > 0) { 122 for (int i = 0; i != Math.abs(diff); ++i) { 123 thisValue = thisValue.multiply(SCALE_BASE); 124 } 125 } else { 126 for (int i = 0; i != Math.abs(diff); ++i) { 127 otherValue = otherValue.multiply(SCALE_BASE); 128 } 129 } 130 return thisValue.compareTo(otherValue); 131 } 132 133 @Override 134 public String toString() { 135 return value + unit.getSimpleName(); 136 } 137 138 @Override 139 public boolean equals(Object obj) { 140 if (obj == null) { 141 return false; 142 } 143 if (obj == this) { 144 return true; 145 } 146 if (obj instanceof Size) { 147 return compareTo((Size) obj) == 0; 148 } 149 return false; 150 } 151 152 @Override 153 public int hashCode() { 154 return Objects.hash(value, unit); 155 } 156}