View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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 + "] should not end with '.'");
65              } else {
66                  maxPart = str.substring(indexOfDot + 1);
67              }
68          } else {
69              minPart = str;
70          }
71  
72          if (minPart != null && minPart.length() > 0) {
73              int min = Integer.parseInt(minPart);
74              if (min >= 0) {
75                  fi.min = min;
76              } else {
77                  fi.min = -min;
78                  fi.leftPad = false;
79              }
80          }
81  
82          if (maxPart != null && maxPart.length() > 0) {
83              int max = Integer.parseInt(maxPart);
84              if (max >= 0) {
85                  fi.max = max;
86              } else {
87                  fi.max = -max;
88                  fi.leftTruncate = false;
89              }
90          }
91  
92          return fi;
93  
94      }
95  
96      public boolean isLeftPad() {
97          return leftPad;
98      }
99  
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 }