001    /**
002     * Copyright 2010-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.maven.wagon;
017    
018    import java.text.NumberFormat;
019    
020    import org.apache.commons.lang.StringUtils;
021    
022    /**
023     * Very simple formatter for formatting a few things - transfer rate, elapsed time, bytes
024     *
025     * @author Jeff Caddel
026     * @since May 27, 2010 6:46:17 PM
027     */
028    public class SimpleFormatter {
029        private static final double KB = 1024;
030        private static final double MB = 1024 * KB;
031        private static final double GB = 1024 * MB;
032        private static final double ONE_SECOND = 1000;
033        private static final double ONE_MINUTE = 60 * ONE_SECOND;
034        private static final double FIFTEEN_MINUTES = 15 * ONE_MINUTE;
035    
036        NumberFormat sizeFormatter = NumberFormat.getInstance();
037        NumberFormat timeFormatter = NumberFormat.getInstance();
038        NumberFormat rateFormatter = NumberFormat.getInstance();
039    
040        public SimpleFormatter() {
041            super();
042            sizeFormatter.setGroupingUsed(false);
043            sizeFormatter.setMaximumFractionDigits(1);
044            sizeFormatter.setMinimumFractionDigits(1);
045            timeFormatter.setGroupingUsed(false);
046            timeFormatter.setMaximumFractionDigits(3);
047            timeFormatter.setMinimumFractionDigits(3);
048            rateFormatter.setGroupingUsed(false);
049            rateFormatter.setMaximumFractionDigits(1);
050            rateFormatter.setMinimumFractionDigits(1);
051        }
052    
053        /**
054         * Given milliseconds and bytes return kilobytes per second
055         */
056        public String getRate(final long millis, final long bytes) {
057            int pad = 1;
058            double seconds = millis / 1000D;
059            double kilobytes = bytes / 1024D;
060            double kilobytesPerSecond = kilobytes / seconds;
061            if (kilobytesPerSecond < 1024) {
062                return StringUtils.leftPad(rateFormatter.format(kilobytesPerSecond) + " kB/s", pad, " ");
063            } else {
064                double transferRate = kilobytesPerSecond / 1024;
065                return StringUtils.leftPad(rateFormatter.format(transferRate) + " MB/s", pad, " ");
066            }
067        }
068    
069        /**
070         * Given milliseconds, return seconds or minutes
071         */
072        public String getTime(final long millis) {
073            int pad = 1;
074            if (millis < ONE_SECOND) {
075                return StringUtils.leftPad(millis + "ms", pad, " ");
076            } else if (millis < 10 * ONE_SECOND) {
077                return StringUtils.leftPad(timeFormatter.format(millis / ONE_SECOND) + "s", pad, " ");
078            } else if (millis < FIFTEEN_MINUTES) {
079                return StringUtils.leftPad(rateFormatter.format(millis / ONE_SECOND) + "s", pad, " ");
080            } else {
081                return StringUtils.leftPad(rateFormatter.format(millis / ONE_MINUTE) + "m", pad, " ");
082            }
083        }
084    
085        /**
086         * Given bytes, return kilobytes if it is less than a megabyte, megabytes if it is less than a gigabyte, otherwise
087         * gigabytes
088         */
089        public String getSize(final long bytes) {
090            int pad = 1;
091            if (bytes < MB) {
092                return StringUtils.leftPad(sizeFormatter.format(bytes / KB) + "k", pad, " ");
093            }
094            if (bytes < GB) {
095                return StringUtils.leftPad(sizeFormatter.format(bytes / MB) + "m", pad, " ");
096            }
097            return StringUtils.leftPad(sizeFormatter.format(bytes / GB) + "g", pad, " ");
098        }
099    }