PrefixedKeysIterator.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.configuration2;

  18. import java.util.Iterator;
  19. import java.util.NoSuchElementException;

  20. /**
  21.  * A specialized iterator implementation used by {@link AbstractConfiguration} to return an iteration over all keys
  22.  * starting with a specified prefix.
  23.  * <p>
  24.  * This class is basically a stripped-down version of the {@code FilterIterator} class of Commons Collections
  25.  * </p>
  26.  */
  27. final class PrefixedKeysIterator implements Iterator<String> {

  28.     /** Stores the wrapped iterator. */
  29.     private final Iterator<String> iterator;

  30.     /** Stores the prefix. */
  31.     private final String prefix;

  32.     /** Stores the prefix delimiter. Default delimiter is "." */
  33.     private final String delimiter;

  34.     /** Stores the next element in the iteration. */
  35.     private String nextElement;

  36.     /** A flag whether the next element has been calculated. */
  37.     private boolean nextElementSet;

  38.     /**
  39.      * Creates a new instance of {@code PrefixedKeysIterator} and sets the wrapped iterator and the prefix for the accepted
  40.      * keys.
  41.      *
  42.      * @param wrappedIterator the wrapped iterator
  43.      * @param keyPrefix the prefix of the allowed keys
  44.      */
  45.     public PrefixedKeysIterator(final Iterator<String> wrappedIterator, final String keyPrefix) {
  46.         this(wrappedIterator, keyPrefix, AbstractConfiguration.DELIMITER);
  47.     }

  48.     /**
  49.      * Creates a new instance of {@code PrefixedKeysIterator} and sets the wrapped iterator and the prefix as well as the delimiter for the preix for the
  50.      * accepted keys.
  51.      *
  52.      * @param wrappedIterator the wrapped iterator
  53.      * @param keyPrefix       the prefix of the allowed keys
  54.      * @param prefixDelimiter the prefix delimiter
  55.      * @since 2.10.0
  56.      */
  57.     public PrefixedKeysIterator(final Iterator<String> wrappedIterator, final String keyPrefix, final String prefixDelimiter) {
  58.         iterator = wrappedIterator;
  59.         prefix = keyPrefix;
  60.         delimiter = prefixDelimiter;
  61.     }

  62.     /**
  63.      * Tests whether there are more elements in the iteration.
  64.      *
  65.      * @return whether there are more elements in the iteration.
  66.      */
  67.     @Override
  68.     public boolean hasNext() {
  69.         return nextElementSet || setNextElement();
  70.     }

  71.     /**
  72.      * Returns the next element in the iteration. This is the next key that matches the specified prefix.
  73.      *
  74.      * @return the next element in the iteration
  75.      * @throws NoSuchElementException if there is no next element
  76.      */
  77.     @Override
  78.     public String next() {
  79.         if (!nextElementSet && !setNextElement()) {
  80.             throw new NoSuchElementException();
  81.         }
  82.         nextElementSet = false;
  83.         return nextElement;
  84.     }

  85.     /**
  86.      * Removes from the underlying collection of the base iterator the last element returned by this iterator. This method
  87.      * can only be called if {@code next()} was called, but not after {@code hasNext()}, because the {@code hasNext()} call
  88.      * changes the base iterator.
  89.      *
  90.      * @throws IllegalStateException if {@code hasNext()} has already been called.
  91.      */
  92.     @Override
  93.     public void remove() {
  94.         if (nextElementSet) {
  95.             throw new IllegalStateException("remove() cannot be called");
  96.         }
  97.         iterator.remove();
  98.     }

  99.     /**
  100.      * Sets the next element in the iteration. The return value indicates whether such an element can be found.
  101.      *
  102.      * @return a flag whether a next element exists
  103.      */
  104.     private boolean setNextElement() {
  105.         while (iterator.hasNext()) {
  106.             final String key = iterator.next();
  107.             if (key.startsWith(prefix + delimiter) || key.equals(prefix)) {
  108.                 nextElement = key;
  109.                 nextElementSet = true;
  110.                 return true;
  111.             }
  112.         }
  113.         return false;
  114.     }

  115.     @Override
  116.     public String toString() {
  117.         // @formatter:off
  118.         return new StringBuilder()
  119.             .append("PrefixedKeysIterator [prefix=")
  120.             .append(prefix)
  121.             .append(", delimiter=")
  122.             .append(delimiter)
  123.             .append(", nextElement=")
  124.             .append(nextElement)
  125.             .append("]")
  126.             .toString();
  127.         // @formatter:on
  128.     }
  129. }