    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 java.util.Date;
    020
    021    import org.apache.logging.log4j.core.config.plugins.Plugin;
    022
    023
    024    /**
    025     * Formats an integer.
    026     */
    027    @Plugin(name = "IntegerPatternConverter", category = "FileConverter")
    028    @ConverterKeys({ "i", "index" })
    029    public final class IntegerPatternConverter extends AbstractPatternConverter implements ArrayPatternConverter {
    030
    031        /**
    032         * Singleton.
    033         */
    034        private static final IntegerPatternConverter INSTANCE = 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        @Override
    055        public void format(final StringBuilder toAppendTo, final Object... objects) {
    056            for (final Object obj : objects) {
    057                if (obj instanceof Integer) {
    058                    format(obj, toAppendTo);
    059                    break;
    060                }
    061            }
    062        }
    063
    064        /**
    065         * {@inheritDoc}
    066         */
    067        @Override
    068        public void format(final Object obj, final StringBuilder toAppendTo) {
    069            if (obj instanceof Integer) {
    070                toAppendTo.append(obj.toString());
    071            } else if (obj instanceof Date) {
    072                toAppendTo.append(Long.toString(((Date) obj).getTime()));
    073            }
    074        }
    075    }