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

org.eclipse.collections.impl.multimap.AbstractSynchronizedMultimap Maven / Gradle / Ivy

There is a newer version: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2016 Shotaro Sano.
 * 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.multimap;

import java.util.Collection;
import java.util.Set;

import net.jcip.annotations.GuardedBy;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.multimap.Multimap;
import org.eclipse.collections.api.multimap.MutableMultimap;
import org.eclipse.collections.api.set.SetIterable;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.bag.mutable.SynchronizedBag;
import org.eclipse.collections.impl.map.mutable.SynchronizedMutableMap;
import org.eclipse.collections.impl.set.mutable.SynchronizedMutableSet;
import org.eclipse.collections.impl.set.mutable.UnmodifiableMutableSet;
import org.eclipse.collections.impl.utility.LazyIterate;

public abstract class AbstractSynchronizedMultimap implements MutableMultimap
{
    @GuardedBy("this.lock")
    protected final MutableMultimap delegate;
    private final Object lock;

    protected AbstractSynchronizedMultimap(MutableMultimap multimap, Object newLock)
    {
        if (multimap == null)
        {
            throw new IllegalArgumentException("Cannot create a AbstractSynchronizedMultimap on a null multimap");
        }

        this.delegate = multimap;
        this.lock = newLock == null ? this : newLock;
    }

    protected AbstractSynchronizedMultimap(MutableMultimap multimap)
    {
        this(multimap, null);
    }

    protected MutableMultimap getDelegate()
    {
        return this.delegate;
    }

    protected Object getLock()
    {
        return this.lock;
    }

    protected Object writeReplace()
    {
        return new SynchronizedMultimapSerializationProxy<>(this.getDelegate());
    }

    @Override
    public boolean equals(Object obj)
    {
        synchronized (this.lock)
        {
            return this.delegate.equals(obj);
        }
    }

    @Override
    public int hashCode()
    {
        synchronized (this.lock)
        {
            return this.delegate.hashCode();
        }
    }

    @Override
    public String toString()
    {
        synchronized (this.lock)
        {
            return this.delegate.toString();
        }
    }

    @Override
    public boolean put(K key, V value)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().put(key, value);
        }
    }

    @Override
    public boolean add(Pair keyValuePair)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().add(keyValuePair);
        }
    }

    @Override
    public boolean remove(Object key, Object value)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().remove(key, value);
        }
    }

    @Override
    public boolean putAllPairs(Pair... pairs)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().putAllPairs(pairs);
        }
    }

    @Override
    public boolean putAllPairs(Iterable> pairs)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().putAllPairs(pairs);
        }
    }

    @Override
    public boolean putAll(K key, Iterable values)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().putAll(key, values);
        }
    }

    @Override
    public  boolean putAll(Multimap multimap)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().putAll(multimap);
        }
    }

    @Override
    public void clear()
    {
        synchronized (this.lock)
        {
            this.getDelegate().clear();
        }
    }

    @Override
    public boolean isEmpty()
    {
        synchronized (this.lock)
        {
            return this.delegate.isEmpty();
        }
    }

    @Override
    public boolean notEmpty()
    {
        synchronized (this.lock)
        {
            return this.delegate.notEmpty();
        }
    }

    @Override
    public void forEachValue(Procedure procedure)
    {
        synchronized (this.lock)
        {
            this.delegate.forEachValue(procedure);
        }
    }

    @Override
    public void forEachKey(Procedure procedure)
    {
        synchronized (this.lock)
        {
            this.delegate.forEachKey(procedure);
        }
    }

    @Override
    public void forEachKeyValue(Procedure2 procedure)
    {
        synchronized (this.lock)
        {
            this.delegate.forEachKeyValue(procedure);
        }
    }

    @Override
    public void forEachKeyMultiValues(Procedure2> procedure)
    {
        synchronized (this.lock)
        {
            this.delegate.forEachKeyMultiValues(procedure);
        }
    }

    @Override
    public int size()
    {
        synchronized (this.lock)
        {
            return this.delegate.size();
        }
    }

    @Override
    public int sizeDistinct()
    {
        synchronized (this.lock)
        {
            return this.delegate.sizeDistinct();
        }
    }

    @Override
    public boolean containsKey(Object key)
    {
        synchronized (this.lock)
        {
            return this.delegate.containsKey(key);
        }
    }

    @Override
    public boolean containsValue(Object value)
    {
        synchronized (this.lock)
        {
            return this.delegate.containsValue(value);
        }
    }

    @Override
    public boolean containsKeyAndValue(Object key, Object value)
    {
        synchronized (this.lock)
        {
            return this.delegate.containsKeyAndValue(key, value);
        }
    }

    @Override
    public RichIterable keysView()
    {
        return LazyIterate.adapt(this.keySet());
    }

    @Override
    public SetIterable keySet()
    {
        synchronized (this.lock)
        {
            return SynchronizedMutableSet.of((Set) this.getDelegate().keySet(), this.lock);
        }
    }

    @Override
    public Bag keyBag()
    {
        synchronized (this.lock)
        {
            return SynchronizedBag.of(this.getDelegate().keyBag().toBag(), this.lock);
        }
    }

    @Override
    public RichIterable> multiValuesView()
    {
        synchronized (this.lock)
        {
            return LazyIterate.adapt(this.getDelegate().multiValuesView());
        }
    }

    @Override
    public RichIterable valuesView()
    {
        synchronized (this.lock)
        {
            return LazyIterate.adapt(this.getDelegate().valuesView());
        }
    }

    @Override
    public RichIterable>> keyMultiValuePairsView()
    {
        synchronized (this.lock)
        {
            return LazyIterate.adapt(this.getDelegate().keyMultiValuePairsView());
        }
    }

    @Override
    public RichIterable> keyValuePairsView()
    {
        synchronized (this.lock)
        {
            return LazyIterate.adapt(this.getDelegate().keyValuePairsView());
        }
    }

    @Override
    public MutableMap> toMap()
    {
        synchronized (this.lock)
        {
            return SynchronizedMutableMap.of(this.getDelegate().toMap(), this.lock);
        }
    }

    @Override
    public > MutableMap toMap(Function0 collectionFactory)
    {
        synchronized (this.lock)
        {
            return SynchronizedMutableMap.of(this.getDelegate().toMap(collectionFactory), this.lock);
        }
    }

    @Override
    public > R selectKeysValues(Predicate2 predicate, R target)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().selectKeysValues(predicate, target);
        }
    }

    @Override
    public > R rejectKeysValues(Predicate2 predicate, R target)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().rejectKeysValues(predicate, target);
        }
    }

    @Override
    public > R selectKeysMultiValues(Predicate2> predicate, R target)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().selectKeysMultiValues(predicate, target);
        }
    }

    @Override
    public > R rejectKeysMultiValues(Predicate2> predicate, R target)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().rejectKeysMultiValues(predicate, target);
        }
    }

    @Override
    public > R collectKeysValues(Function2> function, R target)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().collectKeysValues(function, target);
        }
    }

    @Override
    public > R collectValues(Function function, R target)
    {
        synchronized (this.lock)
        {
            return this.getDelegate().collectValues(function, target);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy