AbstractEmptyIterator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.iterators;

  18. import java.util.NoSuchElementException;

  19. import org.apache.commons.collections4.ResettableIterator;

  20. /**
  21.  * Provides an abstract implementation of an empty iterator.
  22.  *
  23.  * @since 3.1
  24.  */
  25. abstract class AbstractEmptyIterator<E> implements ResettableIterator<E> {

  26.     /**
  27.      * Constructs a new instance.
  28.      */
  29.     protected AbstractEmptyIterator() {
  30.     }

  31.     /**
  32.      * Always throws UnsupportedOperationException.
  33.      *
  34.      * @param ignored ignore.
  35.      * @throws UnsupportedOperationException Always thrown.
  36.      * @deprecated Will be removed in 5.0 without replacement.
  37.      */
  38.     @Deprecated
  39.     public void add(final E ignored) {
  40.         throw new UnsupportedOperationException("add() not supported for empty Iterator");
  41.     }

  42.     /**
  43.      * Always returns false, this iterator contains no elements.
  44.      *
  45.      * @return Always false.
  46.      */
  47.     @Override
  48.     public boolean hasNext() {
  49.         return false;
  50.     }

  51.     /**
  52.      * Always returns false, this iterator contains no elements.
  53.      *
  54.      * @return Always false.
  55.      */
  56.     public boolean hasPrevious() {
  57.         return false;
  58.     }

  59.     /**
  60.      * Always throws IllegalStateException, this iterator contains no elements.
  61.      *
  62.      * @return Always throws IllegalStateException.
  63.      * @throws IllegalStateException Always thrown.
  64.      */
  65.     @Override
  66.     public E next() {
  67.         throw new NoSuchElementException("Iterator contains no elements");
  68.     }

  69.     /**
  70.      * Always returns 0, this iterator contains no elements.
  71.      *
  72.      * @return Always returns 0.
  73.      */
  74.     public int nextIndex() {
  75.         return 0;
  76.     }

  77.     /**
  78.      * Always throws IllegalStateException, this iterator contains no elements.
  79.      *
  80.      * @return Always throws IllegalStateException.
  81.      * @throws IllegalStateException Always thrown.
  82.      */
  83.     public E previous() {
  84.         throw new NoSuchElementException("Iterator contains no elements");
  85.     }

  86.     /**
  87.      * Always returns -1, this iterator contains no elements.
  88.      *
  89.      * @return Always returns -1.
  90.      */
  91.     public int previousIndex() {
  92.         return -1;
  93.     }

  94.     /**
  95.      * Always throws IllegalStateException, this iterator contains no elements.
  96.      *
  97.      * @throws IllegalStateException Always thrown.
  98.      */
  99.     @Override
  100.     public void remove() {
  101.         throw new IllegalStateException("Iterator contains no elements");
  102.     }

  103.     @Override
  104.     public void reset() {
  105.         // do nothing
  106.     }

  107.     /**
  108.      * Always throws IllegalStateException, this iterator contains no elements.
  109.      *
  110.      * @param ignored ignored.
  111.      * @throws IllegalStateException Always thrown.
  112.      */
  113.     public void set(final E ignored) {
  114.         throw new IllegalStateException("Iterator contains no elements");
  115.     }

  116. }