|
| 1 | +/* |
| 2 | + * Copyright (c) 2012-2016, Luigi R. Viggiano |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This software is distributed under the BSD license. |
| 6 | + * See the terms of the BSD license in the documentation provided with this software. |
| 7 | + */ |
| 8 | + |
| 9 | +package net.cactusthorn.config.core.converter.bytesize; |
| 10 | + |
| 11 | +import java.math.BigDecimal; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import static net.cactusthorn.config.core.converter.bytesize.ByteSizeStandard.*; |
| 16 | + |
| 17 | +/** |
| 18 | + * Specifies the available byte size units that a {@link ByteSize} can have. |
| 19 | + * |
| 20 | + * A byte size unit has a {@link ByteSizeStandard} that dictates the base value |
| 21 | + * to be raised to a given power as well as the power value that dictates the |
| 22 | + * magnitude of the unit. For example, a unit having the SI standard with a |
| 23 | + * power value of 4 is known as a terabyte while a unit having the IEC standard |
| 24 | + * with a power value of 4 is known as a tebibyte. The former has a factor of |
| 25 | + * 1000^4 while the latter has a factor of 1024^4. |
| 26 | + * |
| 27 | + * @author Stefan Freyr Stefansson |
| 28 | + */ |
| 29 | +public enum ByteSizeUnit { |
| 30 | + /** |
| 31 | + * The base unit. Has a factor of 1. |
| 32 | + */ |
| 33 | + BYTES("", "B", SI, 0), |
| 34 | + |
| 35 | + /** |
| 36 | + * The SI kilobyte. Has a factor of 1000^1. |
| 37 | + */ |
| 38 | + KILOBYTES("kilo", "KB", SI, 1), |
| 39 | + /** |
| 40 | + * The IEC kibibyte. Has a factor of 1024^1. |
| 41 | + */ |
| 42 | + KIBIBYTES("kibi", "KiB", IEC, 1), |
| 43 | + /** |
| 44 | + * The SI megabyte. Has a factor of 1000^2. |
| 45 | + */ |
| 46 | + MEGABYTES("mega", "MB", SI, 2), |
| 47 | + /** |
| 48 | + * The IEC mebibyte. Has a factor of 1024^2. |
| 49 | + */ |
| 50 | + MEBIBYTES("mebi", "MiB", IEC, 2), |
| 51 | + /** |
| 52 | + * The SI gigabyte. Has a factor of 1000^3. |
| 53 | + */ |
| 54 | + GIGABYTES("giga", "GB", SI, 3), |
| 55 | + /** |
| 56 | + * The IEC gibibyte. Has a factor of 1024^3. |
| 57 | + */ |
| 58 | + GIBIBYTES("gibi", "GiB", IEC, 3), |
| 59 | + /** |
| 60 | + * The SI terabyte. Has a factor of 1000^4. |
| 61 | + */ |
| 62 | + TERABYTES("tera", "TB", SI, 4), |
| 63 | + /** |
| 64 | + * The IEC tebibyte. Has a factor of 1024^4. |
| 65 | + */ |
| 66 | + TEBIBYTES("tebi", "TiB", IEC, 4), |
| 67 | + /** |
| 68 | + * The SI petabyte. Has a factor of 1000^5. |
| 69 | + */ |
| 70 | + PETABYTES("peta", "PB", SI, 5), |
| 71 | + /** |
| 72 | + * The IEC pebibyte. Has a factor of 1024^5. |
| 73 | + */ |
| 74 | + PEBIBYTES("pebi", "PiB", IEC, 5), |
| 75 | + /** |
| 76 | + * The SI exabyte. Has a factor of 1000^6. |
| 77 | + */ |
| 78 | + EXABYTES("exa", "EB", SI, 6), |
| 79 | + /** |
| 80 | + * The IEC exibyte. Has a factor of 1024^6. |
| 81 | + */ |
| 82 | + EXBIBYTES("exbi", "EiB", IEC, 6), |
| 83 | + /** |
| 84 | + * The SI zettabyte. Has a factor of 1000^7. |
| 85 | + */ |
| 86 | + ZETTABYTES("zetta", "ZB", SI, 7), |
| 87 | + /** |
| 88 | + * The IEC zebibyte. Has a factor of 1024^7. |
| 89 | + */ |
| 90 | + ZEBIBYTES("zebi", "ZiB", IEC, 7), |
| 91 | + /** |
| 92 | + * The SI yottabyte. Has a factor of 1000^8. |
| 93 | + */ |
| 94 | + YOTTABYTES("yotta", "YB", SI, 8), |
| 95 | + /** |
| 96 | + * The IEC yobibyte. Has a factor of 1024^8. |
| 97 | + */ |
| 98 | + YOBIBYTES("yobi", "YiB", IEC, 8); |
| 99 | + |
| 100 | + private final String prefix; |
| 101 | + private final String shortLabel; |
| 102 | + private final ByteSizeStandard standard; |
| 103 | + private final int power; |
| 104 | + |
| 105 | + ByteSizeUnit(String prefix, String shortLabel, ByteSizeStandard standard, int power) { |
| 106 | + this.prefix = prefix; |
| 107 | + this.shortLabel = shortLabel; |
| 108 | + this.standard = standard; |
| 109 | + this.power = power; |
| 110 | + } |
| 111 | + |
| 112 | + private static Map<String, ByteSizeUnit> makeUnitsMap() { |
| 113 | + Map<String, ByteSizeUnit> map = new HashMap<String, ByteSizeUnit>(); |
| 114 | + for (ByteSizeUnit unit : ByteSizeUnit.values()) { |
| 115 | + map.put(unit.prefix + "byte", unit); |
| 116 | + map.put(unit.prefix + "bytes", unit); |
| 117 | + if (unit.prefix.length() == 0) { |
| 118 | + map.put("b", unit); |
| 119 | + map.put("", unit); // no unit specified means bytes |
| 120 | + } else { |
| 121 | + String first = unit.prefix.substring(0, 1); |
| 122 | + if (unit.standard == IEC) { |
| 123 | + map.put(first, unit); // 512m |
| 124 | + map.put(first + "i", unit); // 512mi |
| 125 | + map.put(first + "ib", unit); // 512mib |
| 126 | + } else { //unit.standard == SI |
| 127 | + map.put(first + "b", unit); // 512kb |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + return map; |
| 132 | + } |
| 133 | + |
| 134 | + private static Map<String, ByteSizeUnit> unitsMap = makeUnitsMap(); |
| 135 | + |
| 136 | + /** |
| 137 | + * Parses a string representation of a byte size unit and returns the |
| 138 | + * corresponding {@link ByteSizeUnit}. |
| 139 | + * |
| 140 | + * There is support for various formats. Below is a list describing them where |
| 141 | + * [prefix] represents the long form prefix of the unit (such as "kilo", "mega", |
| 142 | + * "tera", etc) and [first] represents the first letter in the prefix (such as |
| 143 | + * "k", "m", "t", etc): |
| 144 | + * <ul> |
| 145 | + * <li>"" (empty string) - refers to the {@link ByteSizeUnit#BYTES}</li> |
| 146 | + * <li>"b" - refers to the {@link ByteSizeUnit#BYTES}.</li> |
| 147 | + * <li>"[prefix]byte" - the prefix determines what {@link ByteSizeStandard} is |
| 148 | + * being used.</li> |
| 149 | + * <li>"[prefix]bytes" - the prefix determines what {@link ByteSizeStandard} is |
| 150 | + * being used.</li> |
| 151 | + * <li>"[first]" - refers to the {@link ByteSizeStandard#IEC} standard for the |
| 152 | + * corresponding prefix, eg. "k" is equal to "kibibyte"</li> |
| 153 | + * <li>"[first]i" - refers to the {@link ByteSizeStandard#IEC} standard for the |
| 154 | + * corresponding prefix, eg. "ki" is equal to "kibibyte"</li> |
| 155 | + * <li>"[first]ib" - refers to the {@link ByteSizeStandard#IEC} standard for the |
| 156 | + * corresponding prefix, eg. "kib" is equal to "kibibyte"</li> |
| 157 | + * <li>"[first]b" - refers to the {@link ByteSizeStandard#SI} standard for the |
| 158 | + * corresponding prefix, eg. "kb" is equal to "kilobyte"</li> |
| 159 | + * </ul> |
| 160 | + * |
| 161 | + * The parsing is case insensitive, meaning that the following strings are |
| 162 | + * equivalent: "KiB", "kIb", "KIB", etc. |
| 163 | + * |
| 164 | + * This method will return <code>null</code> if the specified string is not a |
| 165 | + * valid {@link ByteSizeUnit} string as described above. |
| 166 | + * |
| 167 | + * @param unit the string representation of the desired {@link ByteSizeUnit}. |
| 168 | + * |
| 169 | + * @return the {@link ByteSizeUnit} represented by the specified string or |
| 170 | + * <code>null</code> if the string could not be translated into a known |
| 171 | + * unit. |
| 172 | + */ |
| 173 | + public static ByteSizeUnit parse(String unit) { |
| 174 | + return unitsMap.get(unit.toLowerCase()); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Returns whether this {@link ByteSizeUnit} is an SI unit. |
| 179 | + * |
| 180 | + * @return true iff this unit is an SI unit. |
| 181 | + */ |
| 182 | + public boolean isSI() { |
| 183 | + return this.standard == SI; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Returns whether this {@link ByteSizeUnit} is an IEC unit. |
| 188 | + * |
| 189 | + * @return true iff this unit is an IEC unit. |
| 190 | + */ |
| 191 | + public boolean isIEC() { |
| 192 | + return this.standard == IEC; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Gets the multiplication factor for this {@link ByteSizeUnit}. |
| 197 | + * |
| 198 | + * Returns the result of raising the poweOf value of this units standard to the |
| 199 | + * power specified in this unit. |
| 200 | + * |
| 201 | + * @return the factor by which to multiply for this unit. |
| 202 | + */ |
| 203 | + public BigDecimal getFactor() { |
| 204 | + return BigDecimal.valueOf(standard.powerOf()).pow(power); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Returns the long string representation of this unit, such as "kilobytes", |
| 209 | + * "megabytes" or "bytes". |
| 210 | + * |
| 211 | + * Note that this method always returns the plural form. |
| 212 | + * |
| 213 | + * @return the plural long string representation of this unit. |
| 214 | + */ |
| 215 | + public String toStringLongForm() { |
| 216 | + return prefix + "bytes"; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Returns the short string representation of this unit, such as "KiB", "B" or |
| 221 | + * "MB". |
| 222 | + * |
| 223 | + * @return the short string representation of this unit. |
| 224 | + */ |
| 225 | + public String toStringShortForm() { |
| 226 | + return shortLabel; |
| 227 | + } |
| 228 | +} |
0 commit comments