View Javadoc

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