org.eclipse.collections.impl.lazy.parallel.ParallelDistinctIterable Maven / Gradle / Ivy
/*
* 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.parallel;
import java.util.concurrent.ExecutorService;
import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.annotation.Beta;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.multimap.set.UnsortedSetMultimap;
import org.eclipse.collections.api.set.ParallelUnsortedSetIterable;
import org.eclipse.collections.impl.lazy.parallel.list.DistinctBatch;
import org.eclipse.collections.impl.lazy.parallel.set.AbstractParallelUnsortedSetIterable;
import org.eclipse.collections.impl.lazy.parallel.set.UnsortedSetBatch;
import org.eclipse.collections.impl.map.mutable.ConcurrentHashMap;
import org.eclipse.collections.impl.multimap.set.UnifiedSetMultimap;
@Beta
public class ParallelDistinctIterable extends AbstractParallelUnsortedSetIterable>
{
private final AbstractParallelIterable> delegate;
public ParallelDistinctIterable(AbstractParallelIterable> delegate)
{
this.delegate = delegate;
}
@Override
public ExecutorService getExecutorService()
{
return this.delegate.getExecutorService();
}
@Override
public int getBatchSize()
{
return this.delegate.getBatchSize();
}
@Override
public LazyIterable> split()
{
// TODO: Replace the map with a concurrent set once it's implemented
ConcurrentHashMap distinct = new ConcurrentHashMap<>();
return this.delegate.split().collect(batch -> new DistinctBatch<>(batch, distinct));
}
@Override
public ParallelUnsortedSetIterable asUnique()
{
return this;
}
@Override
public void forEach(Procedure super T> procedure)
{
// TODO: Replace the map with a concurrent set once it's implemented
ConcurrentHashMap distinct = new ConcurrentHashMap<>();
this.delegate.forEach(each -> {
if (distinct.put(each, true) == null)
{
procedure.value(each);
}
});
}
@Override
public boolean anySatisfy(Predicate super T> predicate)
{
return this.delegate.anySatisfy(new DistinctAndPredicate<>(predicate));
}
@Override
public boolean allSatisfy(Predicate super T> predicate)
{
return this.delegate.allSatisfy(new DistinctOrPredicate<>(predicate));
}
@Override
public T detect(Predicate super T> predicate)
{
return this.delegate.detect(new DistinctAndPredicate<>(predicate));
}
@Override
public Object[] toArray()
{
// TODO: Implement in parallel
return this.delegate.toList().distinct().toArray();
}
@Override
public E[] toArray(E[] array)
{
// TODO: Implement in parallel
return this.delegate.toList().distinct().toArray(array);
}
@Override
public UnsortedSetMultimap groupBy(Function super T, ? extends V> function)
{
// TODO: Implement in parallel
return this.delegate.toSet().groupBy(function, new UnifiedSetMultimap<>());
}
@Override
public UnsortedSetMultimap groupByEach(Function super T, ? extends Iterable> function)
{
// TODO: Implement in parallel
return this.delegate.toSet().groupByEach(function, new UnifiedSetMultimap<>());
}
@Override
public MapIterable groupByUniqueKey(Function super T, ? extends V> function)
{
// TODO: Implement in parallel
return this.delegate.toSet().groupByUniqueKey(function);
}
private static final class DistinctAndPredicate implements Predicate
{
// TODO: Replace the map with a concurrent set once it's implemented
private final ConcurrentHashMap distinct = new ConcurrentHashMap<>();
private final Predicate super T> predicate;
private DistinctAndPredicate(Predicate super T> predicate)
{
this.predicate = predicate;
}
@Override
public boolean accept(T each)
{
return this.distinct.put(each, true) == null && this.predicate.accept(each);
}
}
private static final class DistinctOrPredicate implements Predicate
{
// TODO: Replace the map with a concurrent set once it's implemented
private final ConcurrentHashMap distinct = new ConcurrentHashMap<>();
private final Predicate super T> predicate;
private DistinctOrPredicate(Predicate super T> predicate)
{
this.predicate = predicate;
}
@Override
public boolean accept(T each)
{
return this.distinct.put(each, true) != null || this.predicate.accept(each);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy