View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.pattern;
15  
16  /**
17   * FormattingInfo instances contain the information obtained when parsing
18   * formatting modifiers in conversion modifiers.
19   * 
20   * @author Ceki Gülcü
21   */
22  public class FormatInfo {
23    private int min = Integer.MIN_VALUE;
24    private int max = Integer.MAX_VALUE;
25    private boolean leftPad = true;
26    private boolean leftTruncate = true;
27  
28    public FormatInfo() {
29    }
30  
31    public FormatInfo(int min, int max) {
32      this.min = min;
33      this.max = max;
34    }
35  
36    public FormatInfo(int min, int max, boolean leftPad, boolean leftTruncate) {
37      this.min = min;
38      this.max = max;
39      this.leftPad = leftPad;
40      this.leftTruncate = leftTruncate;
41    }
42  
43    /**
44     * This method is used to parse a string such as "5", ".7", "5.7" or "-5.7" into
45     * a FormatInfo.
46     * 
47     * @param str A String to convert into a FormatInfo object
48     * @return A newly created and appropriately initialized FormatInfo object.
49     * @throws IllegalArgumentException
50     */
51    public static FormatInfo valueOf(String str) throws IllegalArgumentException {
52      if (str == null) {
53        throw new NullPointerException("Argument cannot be null");
54      }
55  
56      FormatInfo fi = new FormatInfo();
57  
58      int indexOfDot = str.indexOf('.');
59      String minPart = null;
60      String maxPart = null;
61      if (indexOfDot != -1) {
62        minPart = str.substring(0, indexOfDot);
63        if (indexOfDot + 1 == str.length()) {
64          throw new IllegalArgumentException("Formatting string [" + str
65              + "] should not end with '.'");
66        } else {
67          maxPart = str.substring(indexOfDot + 1);
68        }
69      } else {
70        minPart = str;
71      }
72  
73      if (minPart != null && minPart.length() > 0) {
74        int min = Integer.parseInt(minPart);
75        if (min >= 0) {
76          fi.min = min;
77        } else {
78          fi.min = -min;
79          fi.leftPad = false;
80        }
81      }
82  
83      if (maxPart != null && maxPart.length() > 0) {
84        int max = Integer.parseInt(maxPart);
85        if (max >= 0) {
86          fi.max = max;
87        } else {
88          fi.max = -max;
89          fi.leftTruncate = false;
90        }
91      }
92  
93      return fi;
94  
95    }
96  
97    public boolean isLeftPad() {
98      return leftPad;
99    }
100 
101   public void setLeftPad(boolean leftAlign) {
102     this.leftPad = leftAlign;
103   }
104 
105   public int getMax() {
106     return max;
107   }
108 
109   public void setMax(int max) {
110     this.max = max;
111   }
112 
113   public int getMin() {
114     return min;
115   }
116 
117   public void setMin(int min) {
118     this.min = min;
119   }
120 
121   public boolean isLeftTruncate() {
122     return leftTruncate;
123   }
124 
125   public void setLeftTruncate(boolean leftTruncate) {
126     this.leftTruncate = leftTruncate;
127   }
128 
129   public boolean equals(Object o) {
130     if (this == o) {
131       return true;
132     }
133     if (!(o instanceof FormatInfo)) {
134       return false;
135     }
136     FormatInfo r = (FormatInfo) o;
137 
138     return (min == r.min) && (max == r.max) && (leftPad == r.leftPad)
139         && (leftTruncate == r.leftTruncate);
140   }
141 
142   public String toString() {
143     return "FormatInfo(" + min + ", " + max + ", " + leftPad + ", "
144         + leftTruncate + ")";
145   }
146 }