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.LinkedHashMap;
21  import java.util.Map;
22  
23  import javax.annotation.concurrent.NotThreadSafe;
24  
25  import de.smartics.properties.api.config.domain.Property;
26  import de.smartics.properties.api.config.domain.PropertyCollection;
27  import de.smartics.util.lang.Arg;
28  
29  /**
30   * Implementation of {@link PropertyCollection} based on a {@link Map}.
31   */
32  @NotThreadSafe
33  public final class NativePropertyCollection implements PropertyCollection
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    // --- members --------------------------------------------------------------
40  
41    /**
42     * The properties to iterate over.
43     */
44    private final Map<String, Property> properties =
45        new LinkedHashMap<String, Property>();
46  
47    // ****************************** Initializer *******************************
48  
49    // ****************************** Constructors ******************************
50  
51    /**
52     * Constructor to add properties via a collection.
53     * <p>
54     * The contents of the {@code properties} are added. The client may use the
55     * {@code properties} continuously for other purposes since no reference is
56     * held to any of the {@code properties} after the call.
57     * </p>
58     *
59     * @param properties the properties to iterate over.
60     */
61    public NativePropertyCollection(
62        final Collection<Map<String, Property>> properties)
63    {
64      Arg.checkNotNull("properties", properties); // NOPMD
65  
66      for (final Map<String, Property> element : properties)
67      {
68        this.properties.putAll(element);
69      }
70    }
71  
72    /**
73     * Constructor to add properties.
74     * <p>
75     * The contents of the {@code properties} is added. The client may use the
76     * {@code properties} continuously for other purposes since no reference is
77     * held to the {@code properties} after the call.
78     * </p>
79     *
80     * @param properties the properties to iterate over.
81     */
82    public NativePropertyCollection(final Map<String, Property> properties)
83    {
84      Arg.checkNotNull("properties", properties);
85  
86      this.properties.putAll(properties);
87    }
88  
89    /**
90     * Constructor to add properties.
91     * <p>
92     * The contents of the {@code properties} are added. The client may use the
93     * {@code properties} continuously for other purposes since no reference is
94     * held to any of the {@code properties} after the call.
95     * </p>
96     *
97     * @param properties the properties to iterate over.
98     */
99    public NativePropertyCollection(final Map<String, Property>... properties)
100   {
101     Arg.checkNotNull("properties", properties);
102 
103     for (final Map<String, Property> element : properties)
104     {
105       this.properties.putAll(element);
106     }
107   }
108 
109   // ****************************** Inner Classes *****************************
110 
111   // ********************************* Methods ********************************
112 
113   // --- init -----------------------------------------------------------------
114 
115   // --- get&set --------------------------------------------------------------
116 
117   // --- business -------------------------------------------------------------
118 
119   /**
120    * {@inheritDoc}
121    *
122    * @see java.lang.Iterable#iterator()
123    */
124   @Override
125   public Iterator<Property> iterator()
126   {
127     final Collection<Property> entrySet = properties.values();
128     final Iterator<Property> i = entrySet.iterator();
129     return new Iterator<Property>()
130     {
131       @Override
132       public boolean hasNext()
133       {
134         return i.hasNext();
135       }
136 
137       @Override
138       public Property next()
139       {
140         return i.next();
141       }
142 
143       @Override
144       public void remove()
145       {
146         throw new UnsupportedOperationException("Remove not supported.");
147       }
148     };
149   }
150 
151   @Override
152   public void close()
153   {
154   }
155 
156   // --- object basics --------------------------------------------------------
157 
158 }