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

com.gs.collections.impl.lazy.parallel.ParallelDistinctIterable Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 7.0.3
Show newest version
/*
 * 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 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 predicate)
    {
        return this.delegate.anySatisfy(new DistinctAndPredicate(predicate));
    }

    public boolean allSatisfy(Predicate predicate)
    {
        return this.delegate.allSatisfy(new DistinctOrPredicate(predicate));
    }

    public T detect(Predicate 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 function)
    {
        // TODO: Implement in parallel
        return this.delegate.toSet().groupBy(function, new UnifiedSetMultimap());
    }

    @Override
    public  UnsortedSetMultimap groupByEach(Function> function)
    {
        // TODO: Implement in parallel
        return this.delegate.toSet().groupByEach(function, new UnifiedSetMultimap());
    }

    @Override
    public  MapIterable groupByUniqueKey(Function 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 predicate;

        private DistinctAndPredicate(Predicate 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 predicate;

        private DistinctOrPredicate(Predicate 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