View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.spi.config.support;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  import java.util.Map.Entry;
21  import java.util.Properties;
22  import java.util.Set;
23  
24  import javax.annotation.concurrent.NotThreadSafe;
25  
26  import de.smartics.properties.api.config.domain.Property;
27  import de.smartics.properties.api.config.domain.PropertyCollection;
28  import de.smartics.properties.api.config.domain.PropertyLocation;
29  import de.smartics.util.lang.Arg;
30  
31  /**
32   * Implementation of {@link PropertyCollection} based on properties.
33   */
34  @NotThreadSafe
35  public final class PropertiesPropertyCollection implements PropertyCollection
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    // --- members --------------------------------------------------------------
42  
43    /**
44     * The properties to iterate over.
45     */
46    private final Properties properties = new Properties();
47  
48    // ****************************** Initializer *******************************
49  
50    // ****************************** Constructors ******************************
51  
52    /**
53     * Constructor to add properties via a collection.
54     *
55     * @param properties the properties to iterate over.
56     */
57    public PropertiesPropertyCollection(final Collection<Properties> properties)
58    {
59      Arg.checkNotNull("properties", properties);
60  
61      for (final Properties element : properties)
62      {
63        this.properties.putAll(element);
64      }
65    }
66  
67    /**
68     * Constructor to add properties.
69     *
70     * @param properties the properties to iterate over.
71     */
72    public PropertiesPropertyCollection(final Properties... properties)
73    {
74      Arg.checkNotNull("properties", properties);
75  
76      for (final Properties element : properties)
77      {
78        this.properties.putAll(element);
79      }
80    }
81  
82    // ****************************** Inner Classes *****************************
83  
84    // ********************************* Methods ********************************
85  
86    // --- init -----------------------------------------------------------------
87  
88    // --- get&set --------------------------------------------------------------
89  
90    // --- business -------------------------------------------------------------
91  
92    /**
93     * {@inheritDoc}
94     *
95     * @see java.lang.Iterable#iterator()
96     */
97    @Override
98    public Iterator<Property> iterator()
99    {
100     final Set<Entry<Object, Object>> entrySet = properties.entrySet();
101     final Iterator<Entry<Object, Object>> i = entrySet.iterator();
102     // CHECKSTYLE:OFF
103     return new Iterator<Property>() // NOPMD CHECKSTYLE:ON
104     {
105       @Override
106       public boolean hasNext()
107       {
108         return i.hasNext();
109       }
110 
111       @Override
112       public Property next()
113       {
114         final Entry<Object, Object> entry = i.next();
115         final Object value = entry.getValue();
116 
117         if (value instanceof Property)
118         {
119           return (Property) value;
120         }
121         else
122         {
123           return new Property(new PropertyLocation(getClass().getName()),
124               (String) entry.getKey(), entry.getValue());
125         }
126       }
127 
128       @Override
129       public void remove()
130       {
131         throw new UnsupportedOperationException("Remove not supported.");
132       }
133     };
134   }
135 
136   @Override
137   public void close()
138   {
139   }
140 
141   // --- object basics --------------------------------------------------------
142 
143 }