You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "sebastien-doyon (via GitHub)" <gi...@apache.org> on 2023/10/20 13:42:59 UTC

[PR] [MNG-7899] Various memory usage improvements 4-1 [maven]

sebastien-doyon opened a new pull request, #1296:
URL: https://github.com/apache/maven/pull/1296

   - `BatchModeMavenTransferListener` removed
   - `FileSizeFormat` moved to top level class
   - `FileSizeFormat.formatProcess()` refactored to accept an StringBuilder
   as argument
   - `FileSizeFormat` refactored `format()` methods
   
   
   To make clear that you license your contribution under
   the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   you have to acknowledge this by using the following check-box.
   
    - [ X] I hereby declare this contribution to be licenced under the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   
    - [ X] In any other case, please file an [Apache Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MNG-7899] Various memory usage improvements 4-1 [maven]

Posted by "sebastien-doyon (via GitHub)" <gi...@apache.org>.
sebastien-doyon commented on code in PR #1296:
URL: https://github.com/apache/maven/pull/1296#discussion_r1367135089


##########
maven-embedder/src/main/java/org/apache/maven/cli/transfer/FileSizeFormat.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.cli.transfer;
+
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.Locale;
+
+/**
+ * Formats file size with the associated <a href="https://en.wikipedia.org/wiki/Metric_prefix">SI</a> prefix
+ * (GB, MB, kB) and using the patterns <code>#0.0</code> for numbers between 1 and 10
+ * and <code>###0</code> for numbers between 10 and 1000+ by default.
+ *
+ * @see <a href="https://en.wikipedia.org/wiki/Metric_prefix">https://en.wikipedia.org/wiki/Metric_prefix</a>
+ * @see <a href="https://en.wikipedia.org/wiki/Binary_prefix">https://en.wikipedia.org/wiki/Binary_prefix</a>
+ * @see <a
+ *      href="https://en.wikipedia.org/wiki/Octet_%28computing%29">https://en.wikipedia.org/wiki/Octet_(computing)</a>
+ */
+public class FileSizeFormat {
+    enum ScaleUnit {
+        BYTE {
+            @Override
+            public long bytes() {
+                return 1L;
+            }
+
+            @Override
+            public String symbol() {
+                return "B";
+            }
+        },
+        KILOBYTE {
+            @Override
+            public long bytes() {
+                return 1000L;
+            }
+
+            @Override
+            public String symbol() {
+                return "kB";
+            }
+        },
+        MEGABYTE {
+            @Override
+            public long bytes() {
+                return KILOBYTE.bytes() * KILOBYTE.bytes();
+            }
+
+            @Override
+            public String symbol() {
+                return "MB";
+            }
+        },
+        GIGABYTE {
+            @Override
+            public long bytes() {
+                return MEGABYTE.bytes() * KILOBYTE.bytes();
+            }
+            ;
+
+            @Override
+            public String symbol() {
+                return "GB";
+            }
+        };
+
+        public abstract long bytes();
+
+        public abstract String symbol();
+
+        public static ScaleUnit getScaleUnit(long size) {
+            if (size < 0L) {
+                throw new IllegalArgumentException("file size cannot be negative: " + size);
+            }
+
+            if (size >= GIGABYTE.bytes()) {
+                return GIGABYTE;
+            } else if (size >= MEGABYTE.bytes()) {
+                return MEGABYTE;
+            } else if (size >= KILOBYTE.bytes()) {
+                return KILOBYTE;
+            } else {
+                return BYTE;
+            }
+        }
+    }
+
+    private DecimalFormat smallFormat;
+    private DecimalFormat largeFormat;
+
+    public FileSizeFormat(Locale locale) {
+        smallFormat = new DecimalFormat("#0.0", new DecimalFormatSymbols(locale));
+        largeFormat = new DecimalFormat("###0", new DecimalFormatSymbols(locale));
+    }
+
+    public String format(long size) {
+        return format(size, ScaleUnit.getScaleUnit(size));
+    }
+
+    public String getScaleSymbol(long size) {
+        return ScaleUnit.getScaleUnit(size).symbol();
+    }
+
+    @SuppressWarnings("checkstyle:magicnumber")
+    private String format(long size, ScaleUnit unit) {
+        if (size < 0L) {
+            throw new IllegalArgumentException("file size cannot be negative: " + size);
+        }
+
+        double scaledSize = (double) size / unit.bytes();
+
+        if (unit == ScaleUnit.BYTE) {
+            return largeFormat.format(size);

Review Comment:
   Would have been nice, but the NumberFormat.format() method takes only StringBuffer as parameter, the synchronized StringBuilder. They are not interchangeable  and I am not sure we should switch StringBuilder for StringBuffer for that....



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MNG-7899] Various memory usage improvements 4-1 [maven]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #1296:
URL: https://github.com/apache/maven/pull/1296#discussion_r1367394828


##########
maven-embedder/src/main/java/org/apache/maven/cli/transfer/FileSizeFormat.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.cli.transfer;
+
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.Locale;
+
+/**
+ * Formats file size with the associated <a href="https://en.wikipedia.org/wiki/Metric_prefix">SI</a> prefix
+ * (GB, MB, kB) and using the patterns <code>#0.0</code> for numbers between 1 and 10
+ * and <code>###0</code> for numbers between 10 and 1000+ by default.
+ *
+ * @see <a href="https://en.wikipedia.org/wiki/Metric_prefix">https://en.wikipedia.org/wiki/Metric_prefix</a>
+ * @see <a href="https://en.wikipedia.org/wiki/Binary_prefix">https://en.wikipedia.org/wiki/Binary_prefix</a>
+ * @see <a
+ *      href="https://en.wikipedia.org/wiki/Octet_%28computing%29">https://en.wikipedia.org/wiki/Octet_(computing)</a>
+ */
+public class FileSizeFormat {
+    enum ScaleUnit {
+        BYTE {
+            @Override
+            public long bytes() {
+                return 1L;
+            }
+
+            @Override
+            public String symbol() {
+                return "B";
+            }
+        },
+        KILOBYTE {
+            @Override
+            public long bytes() {
+                return 1000L;
+            }
+
+            @Override
+            public String symbol() {
+                return "kB";
+            }
+        },
+        MEGABYTE {
+            @Override
+            public long bytes() {
+                return KILOBYTE.bytes() * KILOBYTE.bytes();
+            }
+
+            @Override
+            public String symbol() {
+                return "MB";
+            }
+        },
+        GIGABYTE {
+            @Override
+            public long bytes() {
+                return MEGABYTE.bytes() * KILOBYTE.bytes();
+            }
+            ;
+
+            @Override
+            public String symbol() {
+                return "GB";
+            }
+        };
+
+        public abstract long bytes();
+
+        public abstract String symbol();
+
+        public static ScaleUnit getScaleUnit(long size) {
+            if (size < 0L) {
+                throw new IllegalArgumentException("file size cannot be negative: " + size);
+            }
+
+            if (size >= GIGABYTE.bytes()) {
+                return GIGABYTE;
+            } else if (size >= MEGABYTE.bytes()) {
+                return MEGABYTE;
+            } else if (size >= KILOBYTE.bytes()) {
+                return KILOBYTE;
+            } else {
+                return BYTE;
+            }
+        }
+    }
+
+    private DecimalFormat smallFormat;
+    private DecimalFormat largeFormat;
+
+    public FileSizeFormat(Locale locale) {
+        smallFormat = new DecimalFormat("#0.0", new DecimalFormatSymbols(locale));
+        largeFormat = new DecimalFormat("###0", new DecimalFormatSymbols(locale));
+    }
+
+    public String format(long size) {
+        return format(size, ScaleUnit.getScaleUnit(size));
+    }
+
+    public String getScaleSymbol(long size) {
+        return ScaleUnit.getScaleUnit(size).symbol();
+    }
+
+    @SuppressWarnings("checkstyle:magicnumber")
+    private String format(long size, ScaleUnit unit) {
+        if (size < 0L) {
+            throw new IllegalArgumentException("file size cannot be negative: " + size);
+        }
+
+        double scaledSize = (double) size / unit.bytes();
+
+        if (unit == ScaleUnit.BYTE) {
+            return largeFormat.format(size);

Review Comment:
   What about `new Formatter(locale, builder).format("%3.f", size)`  ? i.e. do not use the `NumberFormat`, but `Formatter` ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MNG-7899] Various memory usage improvements 4-1 [maven]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet merged PR #1296:
URL: https://github.com/apache/maven/pull/1296


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MNG-7899] Various memory usage improvements 4-1 [maven]

Posted by "sebastien-doyon (via GitHub)" <gi...@apache.org>.
sebastien-doyon commented on code in PR #1296:
URL: https://github.com/apache/maven/pull/1296#discussion_r1367520121


##########
maven-embedder/src/main/java/org/apache/maven/cli/transfer/FileSizeFormat.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.cli.transfer;
+
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.Locale;
+
+/**
+ * Formats file size with the associated <a href="https://en.wikipedia.org/wiki/Metric_prefix">SI</a> prefix
+ * (GB, MB, kB) and using the patterns <code>#0.0</code> for numbers between 1 and 10
+ * and <code>###0</code> for numbers between 10 and 1000+ by default.
+ *
+ * @see <a href="https://en.wikipedia.org/wiki/Metric_prefix">https://en.wikipedia.org/wiki/Metric_prefix</a>
+ * @see <a href="https://en.wikipedia.org/wiki/Binary_prefix">https://en.wikipedia.org/wiki/Binary_prefix</a>
+ * @see <a
+ *      href="https://en.wikipedia.org/wiki/Octet_%28computing%29">https://en.wikipedia.org/wiki/Octet_(computing)</a>
+ */
+public class FileSizeFormat {
+    enum ScaleUnit {
+        BYTE {
+            @Override
+            public long bytes() {
+                return 1L;
+            }
+
+            @Override
+            public String symbol() {
+                return "B";
+            }
+        },
+        KILOBYTE {
+            @Override
+            public long bytes() {
+                return 1000L;
+            }
+
+            @Override
+            public String symbol() {
+                return "kB";
+            }
+        },
+        MEGABYTE {
+            @Override
+            public long bytes() {
+                return KILOBYTE.bytes() * KILOBYTE.bytes();
+            }
+
+            @Override
+            public String symbol() {
+                return "MB";
+            }
+        },
+        GIGABYTE {
+            @Override
+            public long bytes() {
+                return MEGABYTE.bytes() * KILOBYTE.bytes();
+            }
+            ;
+
+            @Override
+            public String symbol() {
+                return "GB";
+            }
+        };
+
+        public abstract long bytes();
+
+        public abstract String symbol();
+
+        public static ScaleUnit getScaleUnit(long size) {
+            if (size < 0L) {
+                throw new IllegalArgumentException("file size cannot be negative: " + size);
+            }
+
+            if (size >= GIGABYTE.bytes()) {
+                return GIGABYTE;
+            } else if (size >= MEGABYTE.bytes()) {
+                return MEGABYTE;
+            } else if (size >= KILOBYTE.bytes()) {
+                return KILOBYTE;
+            } else {
+                return BYTE;
+            }
+        }
+    }
+
+    private DecimalFormat smallFormat;
+    private DecimalFormat largeFormat;
+
+    public FileSizeFormat(Locale locale) {
+        smallFormat = new DecimalFormat("#0.0", new DecimalFormatSymbols(locale));
+        largeFormat = new DecimalFormat("###0", new DecimalFormatSymbols(locale));
+    }
+
+    public String format(long size) {
+        return format(size, ScaleUnit.getScaleUnit(size));
+    }
+
+    public String getScaleSymbol(long size) {
+        return ScaleUnit.getScaleUnit(size).symbol();
+    }
+
+    @SuppressWarnings("checkstyle:magicnumber")
+    private String format(long size, ScaleUnit unit) {
+        if (size < 0L) {
+            throw new IllegalArgumentException("file size cannot be negative: " + size);
+        }
+
+        double scaledSize = (double) size / unit.bytes();
+
+        if (unit == ScaleUnit.BYTE) {
+            return largeFormat.format(size);

Review Comment:
   Using a Formatter, the pattern string is passed as parameter to the format() method. Doing this the pattern is recompiled each method call and not reused. This generates a lot of garbage. A more optimised solution would be to avoid using generic Format or Formatter classes and implement a custom logic specific to this particular case. Some math logic to round the numbers as needed. This would reduce even more the memory allocation. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MNG-7899] Various memory usage improvements 4-1 [maven]

Posted by "sebastien-doyon (via GitHub)" <gi...@apache.org>.
sebastien-doyon commented on PR #1296:
URL: https://github.com/apache/maven/pull/1296#issuecomment-1803979760

   @gnodet I just fixed a format problem that made the build fail.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MNG-7899] Various memory usage improvements 4-1 [maven]

Posted by "sebastien-doyon (via GitHub)" <gi...@apache.org>.
sebastien-doyon commented on PR #1296:
URL: https://github.com/apache/maven/pull/1296#issuecomment-1803945482

   @gnodet your changes are cleaner. I agree with your contribution, thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MNG-7899] Various memory usage improvements 4-1 [maven]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #1296:
URL: https://github.com/apache/maven/pull/1296#discussion_r1367067777


##########
maven-embedder/src/main/java/org/apache/maven/cli/transfer/FileSizeFormat.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.cli.transfer;
+
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.Locale;
+
+/**
+ * Formats file size with the associated <a href="https://en.wikipedia.org/wiki/Metric_prefix">SI</a> prefix
+ * (GB, MB, kB) and using the patterns <code>#0.0</code> for numbers between 1 and 10
+ * and <code>###0</code> for numbers between 10 and 1000+ by default.
+ *
+ * @see <a href="https://en.wikipedia.org/wiki/Metric_prefix">https://en.wikipedia.org/wiki/Metric_prefix</a>
+ * @see <a href="https://en.wikipedia.org/wiki/Binary_prefix">https://en.wikipedia.org/wiki/Binary_prefix</a>
+ * @see <a
+ *      href="https://en.wikipedia.org/wiki/Octet_%28computing%29">https://en.wikipedia.org/wiki/Octet_(computing)</a>
+ */
+public class FileSizeFormat {
+    enum ScaleUnit {
+        BYTE {
+            @Override
+            public long bytes() {
+                return 1L;
+            }
+
+            @Override
+            public String symbol() {
+                return "B";
+            }
+        },
+        KILOBYTE {
+            @Override
+            public long bytes() {
+                return 1000L;
+            }
+
+            @Override
+            public String symbol() {
+                return "kB";
+            }
+        },
+        MEGABYTE {
+            @Override
+            public long bytes() {
+                return KILOBYTE.bytes() * KILOBYTE.bytes();
+            }
+
+            @Override
+            public String symbol() {
+                return "MB";
+            }
+        },
+        GIGABYTE {
+            @Override
+            public long bytes() {
+                return MEGABYTE.bytes() * KILOBYTE.bytes();
+            }
+            ;
+
+            @Override
+            public String symbol() {
+                return "GB";
+            }
+        };
+
+        public abstract long bytes();
+
+        public abstract String symbol();
+
+        public static ScaleUnit getScaleUnit(long size) {
+            if (size < 0L) {
+                throw new IllegalArgumentException("file size cannot be negative: " + size);
+            }
+
+            if (size >= GIGABYTE.bytes()) {
+                return GIGABYTE;
+            } else if (size >= MEGABYTE.bytes()) {
+                return MEGABYTE;
+            } else if (size >= KILOBYTE.bytes()) {
+                return KILOBYTE;
+            } else {
+                return BYTE;
+            }
+        }
+    }
+
+    private DecimalFormat smallFormat;
+    private DecimalFormat largeFormat;
+
+    public FileSizeFormat(Locale locale) {
+        smallFormat = new DecimalFormat("#0.0", new DecimalFormatSymbols(locale));
+        largeFormat = new DecimalFormat("###0", new DecimalFormatSymbols(locale));
+    }
+
+    public String format(long size) {
+        return format(size, ScaleUnit.getScaleUnit(size));
+    }
+
+    public String getScaleSymbol(long size) {
+        return ScaleUnit.getScaleUnit(size).symbol();
+    }
+
+    @SuppressWarnings("checkstyle:magicnumber")
+    private String format(long size, ScaleUnit unit) {
+        if (size < 0L) {
+            throw new IllegalArgumentException("file size cannot be negative: " + size);
+        }
+
+        double scaledSize = (double) size / unit.bytes();
+
+        if (unit == ScaleUnit.BYTE) {
+            return largeFormat.format(size);

Review Comment:
   The goal is to avoid those calls.  
   `NumberFormat` has methods that take a `StringBuilder`...
   In `NumberFormat`, I have the following declaration:
   ```
       public final String format(long number) {
           return format(number, new StringBuffer(),
                         DontCareFieldPosition.INSTANCE).toString();
       }
   ```
   so we should go straight to the method that takes a `StringBuffer` to avoid unneeded allocations + toString....



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MNG-7899] Various memory usage improvements 4-1 [maven]

Posted by "sebastien-doyon (via GitHub)" <gi...@apache.org>.
sebastien-doyon commented on code in PR #1296:
URL: https://github.com/apache/maven/pull/1296#discussion_r1367546942


##########
maven-embedder/src/main/java/org/apache/maven/cli/transfer/FileSizeFormat.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.cli.transfer;
+
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.Locale;
+
+/**
+ * Formats file size with the associated <a href="https://en.wikipedia.org/wiki/Metric_prefix">SI</a> prefix
+ * (GB, MB, kB) and using the patterns <code>#0.0</code> for numbers between 1 and 10
+ * and <code>###0</code> for numbers between 10 and 1000+ by default.
+ *
+ * @see <a href="https://en.wikipedia.org/wiki/Metric_prefix">https://en.wikipedia.org/wiki/Metric_prefix</a>
+ * @see <a href="https://en.wikipedia.org/wiki/Binary_prefix">https://en.wikipedia.org/wiki/Binary_prefix</a>
+ * @see <a
+ *      href="https://en.wikipedia.org/wiki/Octet_%28computing%29">https://en.wikipedia.org/wiki/Octet_(computing)</a>
+ */
+public class FileSizeFormat {
+    enum ScaleUnit {
+        BYTE {
+            @Override
+            public long bytes() {
+                return 1L;
+            }
+
+            @Override
+            public String symbol() {
+                return "B";
+            }
+        },
+        KILOBYTE {
+            @Override
+            public long bytes() {
+                return 1000L;
+            }
+
+            @Override
+            public String symbol() {
+                return "kB";
+            }
+        },
+        MEGABYTE {
+            @Override
+            public long bytes() {
+                return KILOBYTE.bytes() * KILOBYTE.bytes();
+            }
+
+            @Override
+            public String symbol() {
+                return "MB";
+            }
+        },
+        GIGABYTE {
+            @Override
+            public long bytes() {
+                return MEGABYTE.bytes() * KILOBYTE.bytes();
+            }
+            ;
+
+            @Override
+            public String symbol() {
+                return "GB";
+            }
+        };
+
+        public abstract long bytes();
+
+        public abstract String symbol();
+
+        public static ScaleUnit getScaleUnit(long size) {
+            if (size < 0L) {
+                throw new IllegalArgumentException("file size cannot be negative: " + size);
+            }
+
+            if (size >= GIGABYTE.bytes()) {
+                return GIGABYTE;
+            } else if (size >= MEGABYTE.bytes()) {
+                return MEGABYTE;
+            } else if (size >= KILOBYTE.bytes()) {
+                return KILOBYTE;
+            } else {
+                return BYTE;
+            }
+        }
+    }
+
+    private DecimalFormat smallFormat;
+    private DecimalFormat largeFormat;
+
+    public FileSizeFormat(Locale locale) {
+        smallFormat = new DecimalFormat("#0.0", new DecimalFormatSymbols(locale));
+        largeFormat = new DecimalFormat("###0", new DecimalFormatSymbols(locale));
+    }
+
+    public String format(long size) {
+        return format(size, ScaleUnit.getScaleUnit(size));
+    }
+
+    public String getScaleSymbol(long size) {
+        return ScaleUnit.getScaleUnit(size).symbol();
+    }
+
+    @SuppressWarnings("checkstyle:magicnumber")
+    private String format(long size, ScaleUnit unit) {
+        if (size < 0L) {
+            throw new IllegalArgumentException("file size cannot be negative: " + size);
+        }
+
+        double scaledSize = (double) size / unit.bytes();
+
+        if (unit == ScaleUnit.BYTE) {
+            return largeFormat.format(size);

Review Comment:
   @gnodet you can find the refactored `FileSizeFormat` class using Math logic in the last commit



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org