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     */
017    package org.apache.logging.log4j.core.pattern;
018    
019    import org.apache.logging.log4j.core.config.plugins.Plugin;
020    
021    import java.util.Date;
022    
023    
024    /**
025     * Formats an integer.
026     */
027    @Plugin(name = "IntegerPatternConverter", type = "FileConverter")
028    @ConverterKeys({"i", "index" })
029    public final class IntegerPatternConverter extends AbstractPatternConverter implements ArrayPatternConverter {
030        /**
031         * Singleton.
032         */
033        private static final IntegerPatternConverter INSTANCE =
034            new IntegerPatternConverter();
035    
036        /**
037         * Private constructor.
038         */
039        private IntegerPatternConverter() {
040            super("Integer", "integer");
041        }
042    
043        /**
044         * Obtains an instance of pattern converter.
045         *
046         * @param options options, may be null.
047         * @return instance of pattern converter.
048         */
049        public static IntegerPatternConverter newInstance(
050            final String[] options) {
051            return INSTANCE;
052        }
053    
054        public void format(final StringBuilder toAppendTo, Object... objects) {
055            for (Object obj : objects) {
056                if (obj instanceof Integer) {
057                    format(obj, toAppendTo);
058                    break;
059                }
060            }
061        }
062    
063        /**
064         * {@inheritDoc}
065         */
066        public void format(Object obj, final StringBuilder toAppendTo) {
067            if (obj instanceof Integer) {
068                toAppendTo.append(obj.toString());
069            }
070    
071            if (obj instanceof Date) {
072                toAppendTo.append(Long.toString(((Date) obj).getTime()));
073            }
074        }
075    }