001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.internal;
014
015import static org.apache.juneau.internal.ThrowableUtils.*;
016
017import java.util.*;
018
019/**
020 * Encapsulates multiple collections so they can be iterated over as if they were all part of the same collection.
021 *
022 * @param <E> The object type of this set.
023 */
024public class MultiSet<E> extends AbstractSet<E> {
025
026   /** Inner collections. */
027   final List<Collection<E>> l = new ArrayList<>();
028
029   /**
030    * Create a new Set that consists as a coalesced set of the specified collections.
031    *
032    * @param c Zero or more collections to add to this set.
033    */
034   @SafeVarargs
035   public MultiSet(Collection<E>...c) {
036      for (Collection<E> cc : c)
037         append(cc);
038   }
039
040   /**
041    * Appends the specified collection to this set of collections.
042    *
043    * @param c The collection to append to this set of collections.
044    * @return This object (for method chaining).
045    */
046   public MultiSet<E> append(Collection<E> c) {
047      assertFieldNotNull(c, "c");
048      l.add(c);
049      return this;
050   }
051
052   /**
053    * Iterates over all entries in all collections.
054    */
055   @Override /* Set */
056   public Iterator<E> iterator() {
057      return new Iterator<E>() {
058         int i = 0;
059         Iterator<E> i2 = (l.size() > 0 ? l.get(i++).iterator() : null);
060
061         @Override /* Iterator */
062         public boolean hasNext() {
063            if (i2 == null)
064               return false;
065            if (i2.hasNext())
066               return true;
067            for (int j = i; j < l.size(); j++)
068               if (l.get(j).size() > 0)
069                  return true;
070            return false;
071         }
072
073         @Override /* Iterator */
074         public E next() {
075            if (i2 == null)
076               throw new NoSuchElementException();
077            while (! i2.hasNext()) {
078               if (i >= l.size())
079                  throw new NoSuchElementException();
080               i2 = l.get(i++).iterator();
081            }
082            return i2.next();
083         }
084
085         @Override /* Iterator */
086         public void remove() {
087            if (i2 == null)
088               throw new NoSuchElementException();
089            i2.remove();
090         }
091      };
092   }
093
094   /**
095    * Enumerates over all entries in all collections.
096    *
097    * @return An enumeration wrapper around this set.
098    */
099   public Enumeration<E> enumerator() {
100      return Collections.enumeration(this);
101   }
102
103   @Override /* Set */
104   public int size() {
105      int i = 0;
106      for (Collection<E> c : l)
107         i += c.size();
108      return i;
109   }
110}