489819ffc900e7d12fdaf7a743d96f2c09a94207
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.common
.core
.format
;
12 import java
.math
.BigDecimal
;
13 import java
.text
.DecimalFormat
;
14 import java
.text
.FieldPosition
;
15 import java
.text
.Format
;
16 import java
.text
.NumberFormat
;
17 import java
.text
.ParsePosition
;
19 import org
.eclipse
.jdt
.annotation
.NonNull
;
22 * Provides a formatter for data sizes along with the unit of size (KG, MB, GB
23 * ou TB). It receives a size in bytes and it formats a number in the closest
24 * thousand's unit, with at most 3 decimals.
26 * @author Matthew Khouzam
29 public class DataSizeWithUnitFormat
extends Format
{
31 private static final @NonNull Format INSTANCE
= new DataSizeWithUnitFormat();
33 private static final long serialVersionUID
= 3934127385682676804L;
34 private static final String B
= "B"; //$NON-NLS-1$
35 private static final String K
= "K"; //$NON-NLS-1$
36 private static final String M
= "M"; //$NON-NLS-1$
37 private static final String G
= "G"; //$NON-NLS-1$
38 private static final String T
= "T"; //$NON-NLS-1$
39 private static final long KILO
= 1024;
40 private static final Format FORMAT
= new DecimalFormat("#.###"); //$NON-NLS-1$
43 * Protected constructor
45 protected DataSizeWithUnitFormat() {
50 * Returns the instance of this formatter
52 * @return The instance of this formatter
54 public static @NonNull Format
getInstance() {
59 public StringBuffer
format(Object obj
, StringBuffer toAppendTo
, FieldPosition pos
) {
60 if (obj
instanceof Number
) {
61 Number num
= (Number
) obj
;
62 double value
= num
.doubleValue();
63 double abs
= Math
.abs(value
);
65 return toAppendTo
.append("0"); //$NON-NLS-1$
67 if (abs
>= KILO
* KILO
* KILO
* KILO
) {
68 return toAppendTo
.append(FORMAT
.format(value
/ (KILO
* KILO
* KILO
* KILO
))).append(' ').append(T
).append(B
);
70 if (abs
>= KILO
* KILO
* KILO
) {
71 return toAppendTo
.append(FORMAT
.format(value
/ (KILO
* KILO
* KILO
))).append(' ').append(G
).append(B
);
73 if (abs
>= KILO
* KILO
) {
74 return toAppendTo
.append(FORMAT
.format(value
/ (KILO
* KILO
))).append(' ').append(M
).append(B
);
77 return toAppendTo
.append(FORMAT
.format(value
/ (KILO
))).append(' ').append(K
).append(B
);
79 return toAppendTo
.append(FORMAT
.format(value
)).append(' ').append(B
);
81 return toAppendTo
.append(obj
);
88 public Number
parseObject(String source
, ParsePosition pos
) {
89 Number number
= NumberFormat
.getInstance().parse(source
, pos
);
93 String unit
= source
.substring(pos
.getIndex()).trim().toUpperCase();
95 if (!unit
.isEmpty()) {
96 if (unit
.startsWith(K
)) {
98 } else if (unit
.startsWith(M
)) {
99 multiplier
= KILO
* KILO
;
100 } else if (unit
.startsWith(G
)) {
101 multiplier
= KILO
* KILO
* KILO
;
102 } else if (unit
.startsWith(T
)) {
103 multiplier
= KILO
* KILO
* KILO
* KILO
;
106 if (multiplier
!= 1 && Double
.isFinite(number
.doubleValue())) {
107 BigDecimal bd
= new BigDecimal(number
.toString());
108 bd
= bd
.multiply(BigDecimal
.valueOf(multiplier
));
109 if (bd
.remainder(BigDecimal
.ONE
).equals(BigDecimal
.ZERO
) &&
110 bd
.abs().compareTo(new BigDecimal(Long
.MAX_VALUE
)) < 0) {
111 return bd
.longValue();
113 return bd
.doubleValue();
This page took 0.043549 seconds and 4 git commands to generate.