1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cli.transfer;
20
21 import java.util.Locale;
22
23
24
25
26
27
28
29
30
31
32
33 public class FileSizeFormat {
34 public enum ScaleUnit {
35 BYTE {
36 @Override
37 public long bytes() {
38 return 1L;
39 }
40
41 @Override
42 public String symbol() {
43 return "B";
44 }
45 },
46 KILOBYTE {
47 @Override
48 public long bytes() {
49 return 1000L;
50 }
51
52 @Override
53 public String symbol() {
54 return "kB";
55 }
56 },
57 MEGABYTE {
58 @Override
59 public long bytes() {
60 return KILOBYTE.bytes() * KILOBYTE.bytes();
61 }
62
63 @Override
64 public String symbol() {
65 return "MB";
66 }
67 },
68 GIGABYTE {
69 @Override
70 public long bytes() {
71 return MEGABYTE.bytes() * KILOBYTE.bytes();
72 }
73 ;
74
75 @Override
76 public String symbol() {
77 return "GB";
78 }
79 };
80
81 public abstract long bytes();
82
83 public abstract String symbol();
84
85 public static ScaleUnit getScaleUnit(long size) {
86 if (size < 0L) {
87 throw new IllegalArgumentException("file size cannot be negative: " + size);
88 }
89
90 if (size >= GIGABYTE.bytes()) {
91 return GIGABYTE;
92 } else if (size >= MEGABYTE.bytes()) {
93 return MEGABYTE;
94 } else if (size >= KILOBYTE.bytes()) {
95 return KILOBYTE;
96 } else {
97 return BYTE;
98 }
99 }
100 }
101
102 public FileSizeFormat(Locale locale) {}
103
104 public String format(long size) {
105 return format(size, null);
106 }
107
108 public String format(long size, ScaleUnit unit) {
109 return format(size, unit, false);
110 }
111
112 public String format(long size, ScaleUnit unit, boolean omitSymbol) {
113 StringBuilder sb = new StringBuilder();
114 format(sb, size, unit, omitSymbol);
115 return sb.toString();
116 }
117
118 public void format(StringBuilder builder, long size) {
119 format(builder, size, null, false);
120 }
121
122 public void format(StringBuilder builder, long size, ScaleUnit unit) {
123 format(builder, size, unit, false);
124 }
125
126 @SuppressWarnings("checkstyle:magicnumber")
127 private void format(StringBuilder builder, long size, ScaleUnit unit, boolean omitSymbol) {
128 if (size < 0L) {
129 throw new IllegalArgumentException("file size cannot be negative: " + size);
130 }
131 if (unit == null) {
132 unit = ScaleUnit.getScaleUnit(size);
133 }
134
135 double scaledSize = (double) size / unit.bytes();
136
137 if (unit == ScaleUnit.BYTE) {
138 builder.append(size);
139 } else if (scaledSize < 0.05d || scaledSize >= 10.0d) {
140 builder.append(Math.round(scaledSize));
141 } else {
142 builder.append(Math.round(scaledSize * 10d) / 10d);
143 }
144
145 if (!omitSymbol) {
146 builder.append(" ").append(unit.symbol());
147 }
148 }
149
150 public String formatProgress(long progressedSize, long size) {
151 StringBuilder sb = new StringBuilder();
152 formatProgress(sb, progressedSize, size);
153 return sb.toString();
154 }
155
156 public void formatProgress(StringBuilder builder, long progressedSize, long size) {
157 if (progressedSize < 0L) {
158 throw new IllegalArgumentException("progressed file size cannot be negative: " + size);
159 }
160 if (size >= 0 && progressedSize > size) {
161 throw new IllegalArgumentException(
162 "progressed file size cannot be greater than size: " + progressedSize + " > " + size);
163 }
164
165 if (size >= 0L && progressedSize != size) {
166 ScaleUnit unit = ScaleUnit.getScaleUnit(size);
167 format(builder, progressedSize, unit, true);
168 builder.append("/");
169 format(builder, size, unit, false);
170 } else {
171 ScaleUnit unit = ScaleUnit.getScaleUnit(progressedSize);
172
173 format(builder, progressedSize, unit, false);
174 }
175 }
176 }