View Javadoc

1   package com.atlassian.plugin.util.collect;
2   
3   import static com.atlassian.plugin.util.Assertions.notNull;
4   
5   import java.util.Iterator;
6   
7   /**
8    * {@link Iterator} implementation that decorates another {@link Iterator} who
9    * contains values of type I and uses a {@link Function} that converts that I 
10   * into a V.
11   * <p>
12   * This implementation is unmodifiable.
13   *
14   * @param <I> the value in the underlying iterator
15   * @param <E> the value it is converted to
16   */
17  class TransformingIterator<I, E> implements Iterator<E>
18  {
19      private final Iterator<? extends I> iterator;
20      private final Function<I, E> decorator;
21  
22      TransformingIterator(final Iterator<? extends I> iterator, final Function<I, E> decorator)
23      {
24          this.iterator = notNull("iterator", iterator);
25          this.decorator = notNull("decorator", decorator);
26      }
27  
28      public boolean hasNext()
29      {
30          return iterator.hasNext();
31      }
32  
33      public E next()
34      {
35          return decorator.get(iterator.next());
36      }
37  
38      public void remove()
39      {
40          iterator.remove();
41      }
42  }