Class PrefixedKeysIterator

java.lang.Object
org.apache.commons.configuration2.PrefixedKeysIterator
All Implemented Interfaces:
Iterator<String>

final class PrefixedKeysIterator extends Object implements Iterator<String>
A specialized iterator implementation used by AbstractConfiguration to return an iteration over all keys starting with a specified prefix.

This class is basically a stripped-down version of the FilterIterator class of Commons Collections

  • Field Details

    • iterator

      private final Iterator<String> iterator
      Stores the wrapped iterator.
    • prefix

      private final String prefix
      Stores the prefix.
    • delimiter

      private final String delimiter
      Stores the prefix delimiter. Default delimiter is "."
    • nextElement

      private String nextElement
      Stores the next element in the iteration.
    • nextElementSet

      private boolean nextElementSet
      A flag whether the next element has been calculated.
  • Constructor Details

    • PrefixedKeysIterator

      public PrefixedKeysIterator(Iterator<String> wrappedIterator, String keyPrefix)
      Creates a new instance of PrefixedKeysIterator and sets the wrapped iterator and the prefix for the accepted keys.
      Parameters:
      wrappedIterator - the wrapped iterator
      keyPrefix - the prefix of the allowed keys
    • PrefixedKeysIterator

      public PrefixedKeysIterator(Iterator<String> wrappedIterator, String keyPrefix, String prefixDelimiter)
      Creates a new instance of PrefixedKeysIterator and sets the wrapped iterator and the prefix as well as the delimiter for the preix for the accepted keys.
      Parameters:
      wrappedIterator - the wrapped iterator
      keyPrefix - the prefix of the allowed keys
      prefixDelimiter - the prefix delimiter
      Since:
      2.10.0
  • Method Details

    • hasNext

      public boolean hasNext()
      Tests whether there are more elements in the iteration.
      Specified by:
      hasNext in interface Iterator<String>
      Returns:
      whether there are more elements in the iteration.
    • next

      public String next()
      Returns the next element in the iteration. This is the next key that matches the specified prefix.
      Specified by:
      next in interface Iterator<String>
      Returns:
      the next element in the iteration
      Throws:
      NoSuchElementException - if there is no next element
    • remove

      public void remove()
      Removes from the underlying collection of the base iterator the last element returned by this iterator. This method can only be called if next() was called, but not after hasNext(), because the hasNext() call changes the base iterator.
      Specified by:
      remove in interface Iterator<String>
      Throws:
      IllegalStateException - if hasNext() has already been called.
    • setNextElement

      private boolean setNextElement()
      Sets the next element in the iteration. The return value indicates whether such an element can be found.
      Returns:
      a flag whether a next element exists
    • toString

      public String toString()
      Overrides:
      toString in class Object