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

org.finos.legend.pure.m4.tools.ConcurrentHashSet Maven / Gradle / Ivy

There is a newer version: 5.17.0
Show newest version
// Copyright 2020 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 org.finos.legend.pure.m4.tools;

import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.map.ConcurrentMutableMap;
import org.eclipse.collections.api.set.ParallelUnsortedSetIterable;
import org.eclipse.collections.api.set.UnsortedSetIterable;
import org.eclipse.collections.impl.map.mutable.ConcurrentHashMap;
import org.eclipse.collections.impl.set.mutable.AbstractMutableSet;
import org.eclipse.collections.impl.set.mutable.SetAdapter;

import java.util.Iterator;
import java.util.Spliterator;
import java.util.concurrent.ExecutorService;

public class ConcurrentHashSet extends AbstractMutableSet
{
    private static final Object PRESENT = new Object();

    private final ConcurrentMutableMap map;
    private final UnsortedSetIterable keySet;

    private ConcurrentHashSet(ConcurrentMutableMap map)
    {
        this.map = map;
        this.keySet = SetAdapter.adapt(this.map.keySet());
    }

    public ConcurrentHashSet()
    {
        this(ConcurrentHashMap.newMap());
    }

    public ConcurrentHashSet(int initialSize)
    {
        this(ConcurrentHashMap.newMap(initialSize));
    }

    @Override
    public boolean equals(Object other)
    {
        return (this == other) || this.keySet.equals(other);
    }

    @Override
    public int hashCode()
    {
        return this.keySet.hashCode();
    }

    @Override
    public boolean contains(Object object)
    {
        return this.map.containsKey(object);
    }

    @Override
    public boolean containsAllIterable(Iterable source)
    {
        return this.keySet.containsAllIterable(source);
    }

    @Override
    public boolean add(T element)
    {
        return this.map.putIfAbsent(element, PRESENT) == null;
    }

    @Override
    public boolean remove(Object element)
    {
        return this.map.remove(element) == PRESENT;
    }

    @Override
    public ConcurrentHashSet with(T element)
    {
        add(element);
        return this;
    }

    @Override
    public ConcurrentHashSet without(T element)
    {
        remove(element);
        return this;
    }

    @Override
    public ConcurrentHashSet withAll(Iterable elements)
    {
        addAllIterable(elements);
        return this;
    }

    @Override
    public ConcurrentHashSet withoutAll(Iterable elements)
    {
        removeAllIterable(elements);
        return this;
    }

    @Override
    public ParallelUnsortedSetIterable asParallel(ExecutorService executorService, int batchSize)
    {
        return this.keySet.asParallel(executorService, batchSize);
    }

    @Override
    public int size()
    {
        return this.map.size();
    }

    @Override
    public void clear()
    {
        this.map.clear();
    }

    @Override
    public T getFirst()
    {
        return this.keySet.getFirst();
    }

    @Override
    public T getLast()
    {
        return this.keySet.getLast();
    }

    @Override
    public void each(Procedure procedure)
    {
        this.map.forEachKey(procedure);
    }

    @Override
    public Iterator iterator()
    {
        return this.keySet.iterator();
    }

    @Override
    public Spliterator spliterator()
    {
        return this.keySet.spliterator();
    }

    @Override
    public ConcurrentHashSet clone()
    {
        ConcurrentHashSet clone = (ConcurrentHashSet) super.clone();
        clone.map.putAll(this.map);
        return clone;
    }

    public static  ConcurrentHashSet newSet()
    {
        return new ConcurrentHashSet<>();
    }

    public static  ConcurrentHashSet newSet(int initialSize)
    {
        return new ConcurrentHashSet<>(initialSize);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy