View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.cli.transfer;
20  
21  import java.util.Locale;
22  
23  /**
24   * Formats file size with the associated <a href="https://en.wikipedia.org/wiki/Metric_prefix">SI</a> prefix
25   * (GB, MB, kB) and using the patterns <code>#0.0</code> for numbers between 1 and 10
26   * and <code>###0</code> for numbers between 10 and 1000+ by default.
27   *
28   * @see <a href="https://en.wikipedia.org/wiki/Metric_prefix">https://en.wikipedia.org/wiki/Metric_prefix</a>
29   * @see <a href="https://en.wikipedia.org/wiki/Binary_prefix">https://en.wikipedia.org/wiki/Binary_prefix</a>
30   * @see <a
31   *      href="https://en.wikipedia.org/wiki/Octet_%28computing%29">https://en.wikipedia.org/wiki/Octet_(computing)</a>
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 }