View Javadoc
1   /* 
2    * Copyright (C) 2016 Hobrasoft s.r.o.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package cz.hobrasoft.pdfmu;
18  
19  import java.util.Collection;
20  import java.util.Properties;
21  import org.apache.commons.collections4.CollectionUtils;
22  
23  /**
24   * Properties with integer values
25   *
26   * @author <a href="mailto:filip.bartek@hobrasoft.cz">Filip Bartek</a>
27   */
28  public class IntProperties extends Properties {
29  
30      private final int def;
31  
32      /**
33       * Creates an empty property list with a default value.
34       *
35       * @param def the default property value.
36       */
37      public IntProperties(int def) {
38          super();
39          this.def = def;
40      }
41  
42      /**
43       * Returns the integer value of the property with the specified key in this
44       * property list. The underlying {@link String} value is converted to an
45       * integer using {@link Integer#parseInt(String)}.
46       *
47       * @param key the property key.
48       * @return the value associated with the specified key, or the default value
49       * if the property is not found.
50       */
51      public int getIntProperty(String key) {
52          String valueString = getProperty(key);
53          if (valueString != null) {
54              return Integer.parseInt(valueString);
55          } else {
56              return def;
57          }
58      }
59  
60      /**
61       * Returns a {@link Collection} of {@link Integer} property values in this
62       * {@link IntProperties}.
63       *
64       * <p>
65       * Ignores the defaults.
66       *
67       * @return a collection of integer values in this instance of
68       * {@link IntProperties}.
69       */
70      public Collection<Integer> intPropertyValues() {
71          Collection<Integer> intValues = CollectionUtils.collect(values(), IntegerValueTransformer.integerValueTransformer());
72          // TODO?: Collect values from `defaults` recursively
73          return intValues;
74      }
75  }