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.rolling.helper; 015 016import ch.qos.logback.core.pattern.DynamicConverter; 017import ch.qos.logback.core.pattern.FormatInfo; 018 019/** 020 * When asked to convert an integer, <code>IntegerTokenConverter</code> the 021 * string value of that integer. 022 * 023 * @author Ceki Gulcu 024 */ 025public class IntegerTokenConverter extends DynamicConverter<Object> implements MonoTypedConverter { 026 027 public final static String CONVERTER_KEY = "i"; 028 029 public String convert(int i) { 030 String s = Integer.toString(i); 031 FormatInfo formattingInfo = getFormattingInfo(); 032 if (formattingInfo == null) { 033 return s; 034 } 035 int min = formattingInfo.getMin(); 036 StringBuilder sbuf = new StringBuilder(); 037 for (int j = s.length(); j < min; ++j) { 038 sbuf.append('0'); 039 } 040 return sbuf.append(s).toString(); 041 } 042 043 public String convert(Object o) { 044 if (o == null) { 045 throw new IllegalArgumentException("Null argument forbidden"); 046 } 047 if (o instanceof Integer) { 048 Integer i = (Integer) o; 049 return convert(i.intValue()); 050 } 051 throw new IllegalArgumentException("Cannot convert " + o + " of type" + o.getClass().getName()); 052 } 053 054 public boolean isApplicable(Object o) { 055 return (o instanceof Integer); 056 } 057}