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 *      http://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.geometry.core.internal;
018
019import java.util.Collection;
020import java.util.Deque;
021import java.util.Iterator;
022import java.util.LinkedList;
023import java.util.NoSuchElementException;
024
025/** Class that wraps another iterator, converting each input iterator value into
026 * one or more output iterator values.
027 * @param <I> Input iterator type
028 * @param <T> Output iterator type
029 */
030public abstract class IteratorTransform<I, T> implements Iterator<T> {
031
032    /** Input iterator instance that supplies the input values for this instance. */
033    private final Iterator<I> inputIterator;
034
035    /** Output value queue. */
036    private final Deque<T> outputQueue = new LinkedList<>();
037
038    /** Create a new instance that uses the given iterator as the input source.
039     * @param inputIterator iterator supplying input values
040     */
041    protected IteratorTransform(final Iterator<I> inputIterator) {
042        this.inputIterator = inputIterator;
043    }
044
045    /** {@inheritDoc} */
046    @Override
047    public boolean hasNext() {
048        return loadNextOutput();
049    }
050
051    /** {@inheritDoc} */
052    @Override
053    public T next() {
054        if (outputQueue.isEmpty()) {
055            throw new NoSuchElementException();
056        }
057
058        return outputQueue.removeFirst();
059    }
060
061    /** Load the next output values into the output queue. Returns true if the output queue
062     * contains more entries.
063     * @return true if more output values are available
064     */
065    private boolean loadNextOutput() {
066        while (outputQueue.isEmpty() && inputIterator.hasNext()) {
067            acceptInput(inputIterator.next());
068        }
069
070        return !outputQueue.isEmpty();
071    }
072
073    /** Add a value to the output queue.
074     * @param value value to add to the output queue
075     */
076    protected void addOutput(final T value) {
077        outputQueue.add(value);
078    }
079
080    /** Add multiple values to the output queue.
081     * @param values values to add to the output queue
082     */
083    protected void addAllOutput(final Collection<T> values) {
084        outputQueue.addAll(values);
085    }
086
087    /** Accept a value from the input iterator. This method should take
088     * the input value and add one or more values to the output queue.
089     * @param input value from the input iterator
090     */
091    protected abstract void acceptInput(I input);
092}