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

org.eclipse.collections.impl.lazy.ReverseIterable Maven / Gradle / Ivy

There is a newer version: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2015 Goldman Sachs.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

package org.eclipse.collections.impl.lazy;

import java.util.Iterator;
import java.util.ListIterator;

import net.jcip.annotations.Immutable;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.list.ListIterable;
import org.eclipse.collections.impl.block.factory.Procedures;

/**
 * A ReverseIterable is an iterable that wraps another iterable and iterates in reverse order.
 */
@Immutable
public class ReverseIterable
        extends AbstractLazyIterable
{
    private final ListIterable adapted;

    public ReverseIterable(ListIterable newAdapted)
    {
        this.adapted = newAdapted;
    }

    public static  ReverseIterable adapt(ListIterable listIterable)
    {
        return new ReverseIterable(listIterable);
    }

    public void each(Procedure procedure)
    {
        this.adapted.reverseForEach(procedure);
    }

    @Override
    public void forEachWithIndex(ObjectIntProcedure objectIntProcedure)
    {
        this.adapted.reverseForEach(Procedures.fromObjectIntProcedure(objectIntProcedure));
    }

    @Override
    public 

void forEachWith(Procedure2 procedure, P parameter) { this.adapted.reverseForEach(Procedures.bind(procedure, parameter)); } @Override public int size() { return this.adapted.size(); } @Override public T getFirst() { return this.adapted.getLast(); } @Override public T getLast() { return this.adapted.getFirst(); } @Override public boolean isEmpty() { return this.adapted.isEmpty(); } public Iterator iterator() { ListIterator listIterator = this.adapted.listIterator(this.adapted.size()); return new ReverseIterator(listIterator); } private static final class ReverseIterator implements Iterator { private final ListIterator listIterator; private ReverseIterator(ListIterator listIterator) { this.listIterator = listIterator; } public boolean hasNext() { return this.listIterator.hasPrevious(); } public T next() { return this.listIterator.previous(); } public void remove() { throw new UnsupportedOperationException("Cannot call remove() on " + this.getClass().getSimpleName()); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy