001/* 
002 * Copyright (C) 2016 Hobrasoft s.r.o.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU Affero General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU Affero General Public License for more details.
013 *
014 * You should have received a copy of the GNU Affero General Public License
015 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
016 */
017package cz.hobrasoft.pdfmu;
018
019import org.apache.commons.collections4.Transformer;
020
021/**
022 * Transformer implementation that returns the result of calling
023 * {@link Integer#valueOf(String)} on the string representation (the result of
024 * {@code toString()}) of the input object.
025 *
026 * <p>
027 * Inspiration:
028 * {@link org.apache.commons.collections4.functors.StringValueTransformer}
029 *
030 * @param <T> the input type.
031 * @author <a href="mailto:filip.bartek@hobrasoft.cz">Filip Bartek</a>
032 */
033public final class IntegerValueTransformer<T> implements Transformer<T, Integer> {
034
035    /**
036     * Singleton predicate instance.
037     */
038    private static final Transformer<Object, Integer> INSTANCE = new IntegerValueTransformer<>();
039
040    /**
041     * Factory returning the singleton instance.
042     *
043     * @param <T> the input type.
044     * @return the singleton instance.
045     */
046    //@SuppressWarnings("unchecked")
047    public static <T> Transformer<T, Integer> integerValueTransformer() {
048        return (Transformer<T, Integer>) INSTANCE;
049    }
050
051    /**
052     * Restricted constructor.
053     */
054    private IntegerValueTransformer() {
055        super();
056    }
057
058    /**
059     * Transforms the input to result by calling {@link Integer#valueOf(String)}
060     * on the result of {@code input.toString()}.
061     *
062     * @param input the input object to transform.
063     * @return the transformed result.
064     */
065    @Override
066    public Integer transform(final T input) {
067        return Integer.valueOf(input.toString());
068    }
069
070}