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 java.util.Collection;
020import java.util.Properties;
021import org.apache.commons.collections4.CollectionUtils;
022
023/**
024 * Properties with integer values
025 *
026 * @author <a href="mailto:filip.bartek@hobrasoft.cz">Filip Bartek</a>
027 */
028public class IntProperties extends Properties {
029
030    private final int def;
031
032    /**
033     * Creates an empty property list with a default value.
034     *
035     * @param def the default property value.
036     */
037    public IntProperties(int def) {
038        super();
039        this.def = def;
040    }
041
042    /**
043     * Returns the integer value of the property with the specified key in this
044     * property list. The underlying {@link String} value is converted to an
045     * integer using {@link Integer#parseInt(String)}.
046     *
047     * @param key the property key.
048     * @return the value associated with the specified key, or the default value
049     * if the property is not found.
050     */
051    public int getIntProperty(String key) {
052        String valueString = getProperty(key);
053        if (valueString != null) {
054            return Integer.parseInt(valueString);
055        } else {
056            return def;
057        }
058    }
059
060    /**
061     * Returns a {@link Collection} of {@link Integer} property values in this
062     * {@link IntProperties}.
063     *
064     * <p>
065     * Ignores the defaults.
066     *
067     * @return a collection of integer values in this instance of
068     * {@link IntProperties}.
069     */
070    public Collection<Integer> intPropertyValues() {
071        Collection<Integer> intValues = CollectionUtils.collect(values(), IntegerValueTransformer.integerValueTransformer());
072        // TODO?: Collect values from `defaults` recursively
073        return intValues;
074    }
075}