View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.group;
17  
18  import io.netty.util.internal.ObjectUtil;
19  
20  import java.util.Iterator;
21  import java.util.NoSuchElementException;
22  
23  /**
24   */
25  final class CombinedIterator<E> implements Iterator<E> {
26  
27      private final Iterator<E> i1;
28      private final Iterator<E> i2;
29      private Iterator<E> currentIterator;
30  
31      CombinedIterator(Iterator<E> i1, Iterator<E> i2) {
32          this.i1 = ObjectUtil.checkNotNull(i1, "i1");
33          this.i2 = ObjectUtil.checkNotNull(i2, "i2");
34          this.currentIterator = i1;
35      }
36  
37      @Override
38      public boolean hasNext() {
39          for (;;) {
40              if (currentIterator.hasNext()) {
41                  return true;
42              }
43  
44              if (currentIterator == i1) {
45                  currentIterator = i2;
46              } else {
47                  return false;
48              }
49          }
50      }
51  
52      @Override
53      public E next() {
54          for (;;) {
55              try {
56                  return currentIterator.next();
57              } catch (NoSuchElementException e) {
58                  if (currentIterator == i1) {
59                      currentIterator = i2;
60                  } else {
61                      throw e;
62                  }
63              }
64          }
65      }
66  
67      @Override
68      public void remove() {
69          currentIterator.remove();
70      }
71  
72  }