001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.util;
015
016import java.util.Objects;
017import java.util.regex.Matcher;
018import java.util.regex.Pattern;
019
020/**
021 * Instances of this class represent the size of a file. Internally, the size is
022 * stored as long.
023 * 
024 * <p>
025 * The {@link #valueOf} method can convert strings such as "3 kb", "5 mb", into
026 * FileSize instances. The recognized unit specifications for file size are the
027 * "kb", "mb", and "gb". The unit name may be followed by an "s". Thus, "2 kbs"
028 * and "2 kb" are equivalent. In the absence of a time unit specification, byte
029 * is assumed.
030 * 
031 * @author Ceki G&uuml;lc&uuml;
032 * 
033 */
034public class FileSize {
035
036    private final static String LENGTH_PART = "([0-9]+)";
037    private final static int DOUBLE_GROUP = 1;
038
039    private final static String UNIT_PART = "(|kb|mb|gb)s?";
040    private final static int UNIT_GROUP = 2;
041
042    private static final Pattern FILE_SIZE_PATTERN = Pattern.compile(LENGTH_PART + "\\s*" + UNIT_PART,
043            Pattern.CASE_INSENSITIVE);
044
045    static public final long KB_COEFFICIENT = 1024;
046    static public final long MB_COEFFICIENT = 1024 * KB_COEFFICIENT;
047    static public final long GB_COEFFICIENT = 1024 * MB_COEFFICIENT;
048
049    final long size;
050
051    public FileSize(long size) {
052        this.size = size;
053    }
054
055    public long getSize() {
056        return size;
057    }
058
059    static public FileSize valueOf(String fileSizeStr) {
060        Matcher matcher = FILE_SIZE_PATTERN.matcher(fileSizeStr);
061
062        long coefficient;
063        if (matcher.matches()) {
064            String lenStr = matcher.group(DOUBLE_GROUP);
065            String unitStr = matcher.group(UNIT_GROUP);
066
067            long lenValue = Long.valueOf(lenStr);
068            if (unitStr.equalsIgnoreCase("")) {
069                coefficient = 1;
070            } else if (unitStr.equalsIgnoreCase("kb")) {
071                coefficient = KB_COEFFICIENT;
072            } else if (unitStr.equalsIgnoreCase("mb")) {
073                coefficient = MB_COEFFICIENT;
074            } else if (unitStr.equalsIgnoreCase("gb")) {
075                coefficient = GB_COEFFICIENT;
076            } else {
077                throw new IllegalStateException("Unexpected " + unitStr);
078            }
079            return new FileSize(lenValue * coefficient);
080        } else {
081            throw new IllegalArgumentException("String value [" + fileSizeStr + "] is not in the expected format.");
082        }
083    }
084
085    @Override
086    public String toString() {
087        long inKB = size / KB_COEFFICIENT;
088
089        if (inKB == 0)
090            return size + " Bytes";
091
092        long inMB = size / MB_COEFFICIENT;
093        if (inMB == 0) {
094            return inKB + " KB";
095        }
096
097        long inGB = size / GB_COEFFICIENT;
098        if (inGB == 0) {
099            return inMB + " MB";
100        }
101
102        return inGB + " GB";
103
104    }
105
106    @Override
107    public boolean equals(Object o) {
108        if (this == o)
109            return true;
110        if (o == null || getClass() != o.getClass())
111            return false;
112        FileSize fileSize = (FileSize) o;
113        return size == fileSize.size;
114    }
115
116    @Override
117    public int hashCode() {
118        return Objects.hashCode(size);
119    }
120}