All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onebusaway.collections.combinations.CombinationIterator Maven / Gradle / Ivy

/*
 * Copyright 2008 Brian Ferris
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.onebusaway.collections.combinations;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

import org.onebusaway.collections.tuple.Pair;
import org.onebusaway.collections.tuple.Tuples;

/***********************************************************************************************************************
 * Internal Classes
 **********************************************************************************************************************/

class CombinationIterator implements Iterator>, Serializable {

    private static final long serialVersionUID = 1L;

    private List _readings;

    private boolean _includeReflexive;

    private int _indexI = 0;

    private int _indexJ = 0;

    private Pair _next = null;

    public CombinationIterator(List readings, boolean includeReflexive) {

        _readings = readings;
        _includeReflexive = includeReflexive;

        _indexI = 0;
        _indexJ = _includeReflexive ? _indexI : _indexI + 1;

        tryNext();
    }

    public boolean hasNext() {
        return _next != null;
    }

    public Pair next() {

        if (!hasNext())
            throw new NoSuchElementException();

        Pair n = _next;
        tryNext();
        return n;
    }

    public void remove() {
        throw new UnsupportedOperationException();
    }

    private void tryNext() {
        if (_indexI < _readings.size() && _indexJ < _readings.size()) {
            _next = Tuples.pair(_readings.get(_indexI), _readings.get(_indexJ));
            _indexJ++;
            if (_indexJ >= _readings.size()) {
                _indexI++;
                _indexJ = _includeReflexive ? _indexI : _indexI + 1;
            }
        } else {
            _next = null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy