001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.iterators;
018
019import java.util.Iterator;
020import java.util.NoSuchElementException;
021
022import org.apache.commons.collections4.ResettableIterator;
023
024/**
025 * {@code SingletonIterator} is an {@link Iterator} over a single
026 * object instance.
027 *
028 * @param <E> The type of elements returned by this iterator.
029 * @since 2.0
030 */
031public class SingletonIterator<E>
032        implements ResettableIterator<E> {
033
034    /** Whether remove is allowed */
035    private final boolean removeAllowed;
036
037    /** Is the cursor before the first element */
038    private boolean beforeFirst = true;
039
040    /** Has the element been removed */
041    private boolean removed;
042
043    /** The object */
044    private E object;
045
046    /**
047     * Constructs a new {@code SingletonIterator} where {@code remove}
048     * is a permitted operation.
049     *
050     * @param object  The single object to return from the iterator
051     */
052    public SingletonIterator(final E object) {
053        this(object, true);
054    }
055
056    /**
057     * Constructs a new {@code SingletonIterator} optionally choosing if
058     * {@code remove} is a permitted operation.
059     *
060     * @param object  The single object to return from the iterator
061     * @param removeAllowed  true if remove is allowed
062     * @since 3.1
063     */
064    public SingletonIterator(final E object, final boolean removeAllowed) {
065        this.object = object;
066        this.removeAllowed = removeAllowed;
067    }
068
069    /**
070     * Is another object available from the iterator?
071     * <p>
072     * This returns true if the single object hasn't been returned yet.
073     *
074     * @return true if the single object hasn't been returned yet
075     */
076    @Override
077    public boolean hasNext() {
078        return beforeFirst && !removed;
079    }
080
081    /**
082     * Gets the next object from the iterator.
083     * <p>
084     * This returns the single object if it hasn't been returned yet.
085     *
086     * @return The single object
087     * @throws NoSuchElementException if the single object has already
088     *    been returned
089     */
090    @Override
091    public E next() {
092        if (!beforeFirst || removed) {
093            throw new NoSuchElementException();
094        }
095        beforeFirst = false;
096        return object;
097    }
098
099    /**
100     * Remove the object from this iterator.
101     *
102     * @throws IllegalStateException if the {@code next} method has not
103     *        yet been called, or the {@code remove} method has already
104     *        been called after the last call to the {@code next}
105     *        method.
106     * @throws UnsupportedOperationException if remove is not supported
107     */
108    @Override
109    public void remove() {
110        if (!removeAllowed) {
111            throw new UnsupportedOperationException();
112        }
113        if (removed || beforeFirst) {
114            throw new IllegalStateException();
115        }
116        object = null;
117        removed = true;
118    }
119
120    /**
121     * Reset the iterator to the start.
122     */
123    @Override
124    public void reset() {
125        beforeFirst = true;
126    }
127
128}