|
10 | 10 | package org.elasticsearch.common.unit; |
11 | 11 |
|
12 | 12 | import org.elasticsearch.ElasticsearchParseException; |
| 13 | +import org.elasticsearch.common.Strings; |
13 | 14 | import org.elasticsearch.common.io.stream.StreamInput; |
14 | 15 | import org.elasticsearch.common.io.stream.StreamOutput; |
15 | 16 | import org.elasticsearch.common.io.stream.Writeable; |
16 | 17 | import org.elasticsearch.common.logging.DeprecationLogger; |
17 | | -import org.elasticsearch.core.SuppressForbidden; |
18 | 18 | import org.elasticsearch.xcontent.ToXContentFragment; |
19 | 19 | import org.elasticsearch.xcontent.XContentBuilder; |
20 | 20 |
|
21 | 21 | import java.io.IOException; |
22 | 22 | import java.math.BigDecimal; |
23 | | -import java.text.DecimalFormat; |
24 | 23 | import java.util.Locale; |
25 | 24 | import java.util.Objects; |
26 | 25 |
|
@@ -233,33 +232,26 @@ public String getStringRep() { |
233 | 232 |
|
234 | 233 | @Override |
235 | 234 | public String toString() { |
236 | | - return format2Decimals(sizeInBytes, preferredUnit.toBytes(1)) + preferredUnit.getSuffix(); |
237 | | - } |
238 | | - |
239 | | - /** |
240 | | - * Strictly speaking, this is only <em>guaranteed</em> to return the exact right string |
241 | | - * when {@code numerator} is the rounded value of some multiple of 1% of {@code denominator}. |
242 | | - * Other arbitrary values could yield surprising results; for example, you might see |
243 | | - * {@code 123.0} or even {@code 1221.00} in place of {@code 123}. |
244 | | - */ |
245 | | - static String format2Decimals(long numerator, long denominator) { |
246 | | - // If numerator >> denominator, we risk exceeding the precision of a double, so we enlist |
247 | | - // the help of MAX_TWO_DECIMALS only for the fractional portion, and we handle the whole-number |
248 | | - // portion ourselves. |
249 | | - long wholeNumberPortion = numerator / denominator; |
250 | | - long remainder = numerator % denominator; |
251 | | - if (remainder == 0) { |
252 | | - return Long.toString(wholeNumberPortion); |
253 | | - } else { |
254 | | - return wholeNumberPortion + MAX_TWO_DECIMALS.format(remainder / (double) denominator); |
| 235 | + long bytes = getBytes(); |
| 236 | + double value = bytes; |
| 237 | + String suffix = ByteSizeUnit.BYTES.getSuffix(); |
| 238 | + if (bytes >= ByteSizeUnit.C5) { |
| 239 | + value = getPbFrac(); |
| 240 | + suffix = ByteSizeUnit.PB.getSuffix(); |
| 241 | + } else if (bytes >= ByteSizeUnit.C4) { |
| 242 | + value = getTbFrac(); |
| 243 | + suffix = ByteSizeUnit.TB.getSuffix(); |
| 244 | + } else if (bytes >= ByteSizeUnit.C3) { |
| 245 | + value = getGbFrac(); |
| 246 | + suffix = ByteSizeUnit.GB.getSuffix(); |
| 247 | + } else if (bytes >= ByteSizeUnit.C2) { |
| 248 | + value = getMbFrac(); |
| 249 | + suffix = ByteSizeUnit.MB.getSuffix(); |
| 250 | + } else if (bytes >= ByteSizeUnit.C1) { |
| 251 | + value = getKbFrac(); |
| 252 | + suffix = ByteSizeUnit.KB.getSuffix(); |
255 | 253 | } |
256 | | - } |
257 | | - |
258 | | - private static final DecimalFormat MAX_TWO_DECIMALS = maxTwoDecimalsFormat(); |
259 | | - |
260 | | - @SuppressForbidden(reason = "We truly just want two decimals; Locale settings don't matter here") |
261 | | - private static DecimalFormat maxTwoDecimalsFormat() { |
262 | | - return new DecimalFormat(".##"); |
| 254 | + return Strings.format1Decimals(value, suffix); |
263 | 255 | } |
264 | 256 |
|
265 | 257 | public static ByteSizeValue parseBytesSizeValue(String sValue, String settingName) throws ElasticsearchParseException { |
|
0 commit comments