AbstractListIteratorDecorator.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.ListIterator;
  19. import java.util.Objects;

  20. /**
  21.  * Provides basic behavior for decorating a list iterator with extra functionality.
  22.  * <p>
  23.  * All methods are forwarded to the decorated list iterator.
  24.  * </p>
  25.  *
  26.  * @param <E> the type of elements in this iterator.
  27.  * @since 3.0
  28.  */
  29. public class AbstractListIteratorDecorator<E> implements ListIterator<E> {

  30.     /** The iterator being decorated */
  31.     private final ListIterator<E> iterator;

  32.     /**
  33.      * Constructor that decorates the specified iterator.
  34.      *
  35.      * @param iterator  the iterator to decorate, must not be null
  36.      * @throws NullPointerException if the iterator is null
  37.      */
  38.     public AbstractListIteratorDecorator(final ListIterator<E> iterator) {
  39.         this.iterator = Objects.requireNonNull(iterator, "iterator");
  40.     }

  41.     /** {@inheritDoc} */
  42.     @Override
  43.     public void add(final E obj) {
  44.         iterator.add(obj);
  45.     }

  46.     /**
  47.      * Gets the iterator being decorated.
  48.      *
  49.      * @return the decorated iterator
  50.      */
  51.     protected ListIterator<E> getListIterator() {
  52.         return iterator;
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public boolean hasNext() {
  57.         return iterator.hasNext();
  58.     }

  59.     /** {@inheritDoc} */
  60.     @Override
  61.     public boolean hasPrevious() {
  62.         return iterator.hasPrevious();
  63.     }

  64.     /** {@inheritDoc} */
  65.     @Override
  66.     public E next() {
  67.         return iterator.next();
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public int nextIndex() {
  72.         return iterator.nextIndex();
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public E previous() {
  77.         return iterator.previous();
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public int previousIndex() {
  82.         return iterator.previousIndex();
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public void remove() {
  87.         iterator.remove();
  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public void set(final E obj) {
  92.         iterator.set(obj);
  93.     }

  94. }