001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.pattern; 018 019import java.util.Date; 020 021import org.apache.logging.log4j.core.config.plugins.Plugin; 022import org.apache.logging.log4j.util.PerformanceSensitive; 023 024/** 025 * Formats an integer. 026 */ 027@Plugin(name = "IntegerPatternConverter", category = "FileConverter") 028@ConverterKeys({ "i", "index" }) 029@PerformanceSensitive("allocation") 030public final class IntegerPatternConverter extends AbstractPatternConverter implements ArrayPatternConverter { 031 032 /** 033 * Singleton. 034 */ 035 private static final IntegerPatternConverter INSTANCE = new IntegerPatternConverter(); 036 037 /** 038 * Private constructor. 039 */ 040 private IntegerPatternConverter() { 041 super("Integer", "integer"); 042 } 043 044 /** 045 * Obtains an instance of pattern converter. 046 * 047 * @param options options, may be null. 048 * @return instance of pattern converter. 049 */ 050 public static IntegerPatternConverter newInstance(final String[] options) { 051 return INSTANCE; 052 } 053 054 @Override 055 public void format(final StringBuilder toAppendTo, final Object... objects) { 056 for (int i = 0; i < objects.length; i++) { 057 if (objects[i] instanceof Integer) { 058 format(objects[i], toAppendTo); 059 break; 060 } else if (objects[i] instanceof NotANumber) { 061 toAppendTo.append(NotANumber.VALUE); 062 break; 063 } 064 } 065 } 066 067 /** 068 * {@inheritDoc} 069 */ 070 @Override 071 public void format(final Object obj, final StringBuilder toAppendTo) { 072 if (obj instanceof Integer) { 073 toAppendTo.append(((Integer) obj).intValue()); 074 } else if (obj instanceof Date) { 075 toAppendTo.append(((Date) obj).getTime()); 076 } 077 } 078}