489819ffc900e7d12fdaf7a743d96f2c09a94207
[deliverable/tracecompass.git] / common / org.eclipse.tracecompass.common.core / src / org / eclipse / tracecompass / common / core / format / DataSizeWithUnitFormat.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
3 *
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 *******************************************************************************/
9
10 package org.eclipse.tracecompass.common.core.format;
11
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;
18
19 import org.eclipse.jdt.annotation.NonNull;
20
21 /**
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.
25 *
26 * @author Matthew Khouzam
27 * @since 2.0
28 */
29 public class DataSizeWithUnitFormat extends Format {
30
31 private static final @NonNull Format INSTANCE = new DataSizeWithUnitFormat();
32
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$
41
42 /**
43 * Protected constructor
44 */
45 protected DataSizeWithUnitFormat() {
46 super();
47 }
48
49 /**
50 * Returns the instance of this formatter
51 *
52 * @return The instance of this formatter
53 */
54 public static @NonNull Format getInstance() {
55 return INSTANCE;
56 }
57
58 @Override
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);
64 if (value == 0) {
65 return toAppendTo.append("0"); //$NON-NLS-1$
66 }
67 if (abs >= KILO * KILO * KILO * KILO) {
68 return toAppendTo.append(FORMAT.format(value / (KILO * KILO * KILO * KILO))).append(' ').append(T).append(B);
69 }
70 if (abs >= KILO * KILO * KILO) {
71 return toAppendTo.append(FORMAT.format(value / (KILO * KILO * KILO))).append(' ').append(G).append(B);
72 }
73 if (abs >= KILO * KILO) {
74 return toAppendTo.append(FORMAT.format(value / (KILO * KILO))).append(' ').append(M).append(B);
75 }
76 if (abs >= KILO) {
77 return toAppendTo.append(FORMAT.format(value / (KILO))).append(' ').append(K).append(B);
78 }
79 return toAppendTo.append(FORMAT.format(value)).append(' ').append(B);
80 }
81 return toAppendTo.append(obj);
82 }
83
84 /**
85 * @since 2.1
86 */
87 @Override
88 public Number parseObject(String source, ParsePosition pos) {
89 Number number = NumberFormat.getInstance().parse(source, pos);
90 if (number == null) {
91 return null;
92 }
93 String unit = source.substring(pos.getIndex()).trim().toUpperCase();
94 long multiplier = 1;
95 if (!unit.isEmpty()) {
96 if (unit.startsWith(K)) {
97 multiplier = KILO;
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;
104 }
105 }
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();
112 }
113 return bd.doubleValue();
114 }
115 return number;
116 }
117 }
This page took 0.043549 seconds and 4 git commands to generate.