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.rolling.helper;
15  
16  import ch.qos.logback.core.pattern.DynamicConverter;
17  import ch.qos.logback.core.pattern.FormatInfo;
18  
19  /**
20   * When asked to convert an integer, <code>IntegerTokenConverter</code> the
21   * string value of that integer.
22   * 
23   * @author Ceki Gulcu
24   */
25  public class IntegerTokenConverter extends DynamicConverter<Object> implements MonoTypedConverter {
26  
27      public final static String CONVERTER_KEY = "i";
28  
29      public String convert(int i) {
30          String s = Integer.toString(i);
31          FormatInfo formattingInfo = getFormattingInfo();
32          if (formattingInfo == null) {
33              return s;
34          }
35          int min = formattingInfo.getMin();
36          StringBuilder sbuf = new StringBuilder();
37          for (int j = s.length(); j < min; ++j) {
38              sbuf.append('0');
39          }
40          return sbuf.append(s).toString();
41      }
42  
43      public String convert(Object o) {
44          if (o == null) {
45              throw new IllegalArgumentException("Null argument forbidden");
46          }
47          if (o instanceof Integer) {
48              Integer i = (Integer) o;
49              return convert(i.intValue());
50          }
51          throw new IllegalArgumentException("Cannot convert " + o + " of type" + o.getClass().getName());
52      }
53  
54      public boolean isApplicable(Object o) {
55          return (o instanceof Integer);
56      }
57  }