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