com.gs.collections.impl.lazy.parallel.ParallelDistinctIterable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gs-collections Show documentation
Show all versions of gs-collections Show documentation
GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map
implementations with a rich API and set of utility classes that work with any JDK compatible Collections,
Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.
/*
* Copyright 2014 Goldman Sachs.
*
* 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 com.gs.collections.impl.lazy.parallel;
import java.util.concurrent.ExecutorService;
import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.annotation.Beta;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.procedure.Procedure;
import com.gs.collections.api.map.MapIterable;
import com.gs.collections.api.multimap.set.UnsortedSetMultimap;
import com.gs.collections.api.set.ParallelUnsortedSetIterable;
import com.gs.collections.impl.lazy.parallel.list.DistinctBatch;
import com.gs.collections.impl.lazy.parallel.set.AbstractParallelUnsortedSetIterable;
import com.gs.collections.impl.lazy.parallel.set.UnsortedSetBatch;
import com.gs.collections.impl.map.mutable.ConcurrentHashMap;
import com.gs.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
final ConcurrentHashMap distinct = new ConcurrentHashMap();
return this.delegate.split().collect(new Function, UnsortedSetBatch>()
{
public UnsortedSetBatch valueOf(Batch batch)
{
return new DistinctBatch(batch, distinct);
}
});
}
@Override
public ParallelUnsortedSetIterable asUnique()
{
return this;
}
public void forEach(final Procedure super T> procedure)
{
// TODO: Replace the map with a concurrent set once it's implemented
final ConcurrentHashMap distinct = new ConcurrentHashMap();
this.delegate.forEach(new Procedure()
{
public void value(T each)
{
if (distinct.put(each, true) == null)
{
procedure.value(each);
}
}
});
}
public boolean anySatisfy(Predicate super T> predicate)
{
return this.delegate.anySatisfy(new DistinctAndPredicate(predicate));
}
public boolean allSatisfy(Predicate super T> predicate)
{
return this.delegate.allSatisfy(new DistinctOrPredicate(predicate));
}
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;
}
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;
}
public boolean accept(T each)
{
return this.distinct.put(each, true) != null || this.predicate.accept(each);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy