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.pattern;
015
016/**
017 * FormattingInfo instances contain the information obtained when parsing
018 * formatting modifiers in conversion modifiers.
019 * 
020 * @author Ceki Gülcü
021 */
022public class FormatInfo {
023    private int min = Integer.MIN_VALUE;
024    private int max = Integer.MAX_VALUE;
025    private boolean leftPad = true;
026    private boolean leftTruncate = true;
027
028    public FormatInfo() {
029    }
030
031    public FormatInfo(int min, int max) {
032        this.min = min;
033        this.max = max;
034    }
035
036    public FormatInfo(int min, int max, boolean leftPad, boolean leftTruncate) {
037        this.min = min;
038        this.max = max;
039        this.leftPad = leftPad;
040        this.leftTruncate = leftTruncate;
041    }
042
043    /**
044     * This method is used to parse a string such as "5", ".7", "5.7" or "-5.7" into
045     * a FormatInfo.
046     * 
047     * @param str A String to convert into a FormatInfo object
048     * @return A newly created and appropriately initialized FormatInfo object.
049     * @throws IllegalArgumentException
050     */
051    public static FormatInfo valueOf(String str) throws IllegalArgumentException {
052        if (str == null) {
053            throw new NullPointerException("Argument cannot be null");
054        }
055
056        FormatInfo fi = new FormatInfo();
057
058        int indexOfDot = str.indexOf('.');
059        String minPart = null;
060        String maxPart = null;
061        if (indexOfDot != -1) {
062            minPart = str.substring(0, indexOfDot);
063            if (indexOfDot + 1 == str.length()) {
064                throw new IllegalArgumentException("Formatting string [" + str + "] should not end with '.'");
065            } else {
066                maxPart = str.substring(indexOfDot + 1);
067            }
068        } else {
069            minPart = str;
070        }
071
072        if (minPart != null && minPart.length() > 0) {
073            int min = Integer.parseInt(minPart);
074            if (min >= 0) {
075                fi.min = min;
076            } else {
077                fi.min = -min;
078                fi.leftPad = false;
079            }
080        }
081
082        if (maxPart != null && maxPart.length() > 0) {
083            int max = Integer.parseInt(maxPart);
084            if (max >= 0) {
085                fi.max = max;
086            } else {
087                fi.max = -max;
088                fi.leftTruncate = false;
089            }
090        }
091
092        return fi;
093
094    }
095
096    public boolean isLeftPad() {
097        return leftPad;
098    }
099
100    public void setLeftPad(boolean leftAlign) {
101        this.leftPad = leftAlign;
102    }
103
104    public int getMax() {
105        return max;
106    }
107
108    public void setMax(int max) {
109        this.max = max;
110    }
111
112    public int getMin() {
113        return min;
114    }
115
116    public void setMin(int min) {
117        this.min = min;
118    }
119
120    public boolean isLeftTruncate() {
121        return leftTruncate;
122    }
123
124    public void setLeftTruncate(boolean leftTruncate) {
125        this.leftTruncate = leftTruncate;
126    }
127
128    public boolean equals(Object o) {
129        if (this == o) {
130            return true;
131        }
132        if (!(o instanceof FormatInfo)) {
133            return false;
134        }
135        FormatInfo r = (FormatInfo) o;
136
137        return (min == r.min) && (max == r.max) && (leftPad == r.leftPad) && (leftTruncate == r.leftTruncate);
138    }
139
140    @Override
141    public int hashCode() {
142        int result = min;
143        result = 31 * result + max;
144        result = 31 * result + (leftPad ? 1 : 0);
145        result = 31 * result + (leftTruncate ? 1 : 0);
146        return result;
147    }
148
149    public String toString() {
150        return "FormatInfo(" + min + ", " + max + ", " + leftPad + ", " + leftTruncate + ")";
151    }
152}