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

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

There is a newer version: 9.2.0.M1
Show newest version
/*
 * Copyright (c) 2016 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.Optional;

import net.jcip.annotations.Immutable;
import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.impl.block.procedure.AdaptObjectIntProcedureToProcedure;
import org.eclipse.collections.impl.lazy.iterator.DistinctIterator;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.utility.Iterate;

/**
 * A DistinctIterable is an iterable that eliminates duplicates from a source iterable as it iterates.
 *
 * @since 5.0
 */
@Immutable
public class DistinctIterable
        extends AbstractLazyIterable
{
    private final Iterable adapted;

    public DistinctIterable(Iterable newAdapted)
    {
        this.adapted = newAdapted;
    }

    @Override
    public LazyIterable distinct()
    {
        return this;
    }

    @Override
    public void each(Procedure procedure)
    {
        MutableSet seenSoFar = UnifiedSet.newSet();

        Iterate.forEach(this.adapted, each -> {
            if (seenSoFar.add(each))
            {
                procedure.value(each);
            }
        });
    }

    @Override
    public void forEachWithIndex(ObjectIntProcedure objectIntProcedure)
    {
        this.each(new AdaptObjectIntProcedureToProcedure<>(objectIntProcedure));
    }

    @Override
    public boolean anySatisfy(Predicate predicate)
    {
        MutableSet seenSoFar = UnifiedSet.newSet();

        return Iterate.anySatisfy(this.adapted, each -> seenSoFar.add(each) && predicate.accept(each));
    }

    @Override
    public boolean allSatisfy(Predicate predicate)
    {
        MutableSet seenSoFar = UnifiedSet.newSet();

        return Iterate.allSatisfy(this.adapted, each -> !seenSoFar.add(each) || predicate.accept(each));
    }

    @Override
    public boolean noneSatisfy(Predicate predicate)
    {
        MutableSet seenSoFar = UnifiedSet.newSet();

        return Iterate.allSatisfy(this.adapted, each -> !seenSoFar.add(each) || !predicate.accept(each));
    }

    @Override
    public T detect(Predicate predicate)
    {
        MutableSet seenSoFar = UnifiedSet.newSet();

        return Iterate.detect(this.adapted, each -> seenSoFar.add(each) && predicate.accept(each));
    }

    @Override
    public Optional detectOptional(Predicate predicate)
    {
        MutableSet seenSoFar = UnifiedSet.newSet();

        return Iterate.detectOptional(this.adapted, each -> seenSoFar.add(each) && predicate.accept(each));
    }

    @Override
    public Iterator iterator()
    {
        return new DistinctIterator<>(this.adapted);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy