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.ArrayList;
19  import java.util.Iterator;
20  import java.util.LinkedList;
21  import java.util.List;
22  import java.util.Queue;
23  
24  import com.google.common.collect.AbstractIterator;
25  
26  import de.smartics.properties.api.config.domain.Property;
27  import de.smartics.properties.api.config.domain.PropertyCollection;
28  import de.smartics.util.lang.Arg;
29  
30  /**
31   * Provides access to a set of property iterators.
32   */
33  public final class MultiPropertyCollection implements PropertyCollection
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    // --- members --------------------------------------------------------------
40  
41    /**
42     * The list of property collection instances.
43     */
44    private final List<PropertyCollection> collections =
45        new ArrayList<PropertyCollection>();
46  
47    // ****************************** Initializer *******************************
48  
49    // ****************************** Constructors ******************************
50  
51    // ****************************** Inner Classes *****************************
52  
53    // ********************************* Methods ********************************
54  
55    // --- init -----------------------------------------------------------------
56  
57    // --- get&set --------------------------------------------------------------
58  
59    // --- business -------------------------------------------------------------
60  
61    /**
62     * Adds the given collection to this set.
63     *
64     * @param collection the collection to add.
65     * @throws NullPointerException if {@code collection} is <code>null</code>.
66     */
67    public void add(final PropertyCollection collection)
68      throws NullPointerException
69    {
70      collections.add(Arg.checkNotNull("collection", collection));
71    }
72  
73    /**
74     * {@inheritDoc}
75     * <p>
76     * Closes each collection in the set.
77     * </p>
78     */
79    @Override
80    public void close()
81    {
82      for (final PropertyCollection collection : collections)
83      {
84        collection.close();
85      }
86    }
87  
88    /**
89     * {@inheritDoc}
90     * <p>
91     * Returns an iterator on each collection in the set.
92     * </p>
93     */
94    @Override
95    public Iterator<Property> iterator()
96    {
97      final Queue<Iterator<Property>> queue =
98          new LinkedList<Iterator<Property>>();
99      for (final PropertyCollection collection : collections)
100     {
101       final Iterator<Property> iterator = collection.iterator();
102       queue.add(iterator);
103     }
104 
105     return new AbstractIterator<Property>()
106     {
107       @Override
108       protected Property computeNext()
109       {
110         while (!queue.isEmpty())
111         {
112           final Iterator<Property> topIter = queue.poll();
113           if (topIter.hasNext())
114           {
115             final Property result = topIter.next();
116             queue.offer(topIter);
117             return result;
118           }
119         }
120         return endOfData();
121       }
122     };
123   }
124 
125   // --- object basics --------------------------------------------------------
126 
127 }