Returns an unmodifiable view of iterator.
(final Iterator<? extends T> iterator)
| 161 | |
| 162 | |
| 163 | public static <T> UnmodifiableIterator<T> unmodifiableIterator(final Iterator<? extends T> iterator) { |
| 164 | checkNotNull(iterator); |
| 165 | if (iterator instanceof UnmodifiableIterator) { |
| 166 | @SuppressWarnings("unchecked") // Since it's unmodifiable, the covariant cast is safe |
| 167 | UnmodifiableIterator<T> result = (UnmodifiableIterator<T>) iterator; |
| 168 | return result; |
| 169 | } |
| 170 | return new UnmodifiableIterator<T>() { |
| 171 | @Override |
| 172 | public boolean hasNext() { |
| 173 | return iterator.hasNext(); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public T next() { |
| 178 | return iterator.next(); |
| 179 | } |
| 180 | }; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Simply returns its argument. |
no test coverage detected