    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.collections4.multiset;
    018
    019import java.util.Set;
    020
    021import org.apache.commons.collections4.MultiSet;
    022import org.apache.commons.collections4.collection.SynchronizedCollection;
    023
    024/**
    025 * Decorates another {@link MultiSet} to synchronize its behavior
    026 * for a multithreaded environment.
    027 * <p>
    028 * Methods are synchronized, then forwarded to the decorated multiset.
    029 * Iterators must be separately synchronized around the loop.
    030 * </p>
    031 *
    032 * @param <E> the type held in the multiset.
    033 * @since 4.1
    034 */
    035public class SynchronizedMultiSet<E> extends SynchronizedCollection<E> implements MultiSet<E> {
    036
    037    /**
    038     * Synchronized Set for the MultiSet class.
    039     *
    040     * @param <T> the type held in this Set.
    041     */
    042    static class SynchronizedSet<T> extends SynchronizedCollection<T> implements Set<T> {
    043        /** Serialization version */
    044        private static final long serialVersionUID = 20150629L;
    045
    046        /**
    047         * Constructs a new instance.
    048         * @param set  the set to decorate
    049         * @param lock  the lock to use, shared with the multiset
    050         */
    051        SynchronizedSet(final Set<T> set, final Object lock) {
    052            super(set, lock);
    053        }
    054    }
    055
    056    /** Serialization version */
    057    private static final long serialVersionUID = 20150629L;
    058
    059    /**
    060     * Factory method to create a synchronized multiset.
    061     *
    062     * @param <E> the type of the elements in the multiset
    063     * @param multiset  the multiset to decorate, must not be null
    064     * @return a new synchronized MultiSet
    065     * @throws NullPointerException if multiset is null
    066     */
    067    public static <E> SynchronizedMultiSet<E> synchronizedMultiSet(final MultiSet<E> multiset) {
    068        return new SynchronizedMultiSet<>(multiset);
    069    }
    070
    071    /**
    072     * Constructor that wraps (not copies).
    073     *
    074     * @param multiset  the multiset to decorate, must not be null
    075     * @throws NullPointerException if multiset is null
    076     */
    077    protected SynchronizedMultiSet(final MultiSet<E> multiset) {
    078        super(multiset);
    079    }
    080
    081    /**
    082     * Constructor that wraps (not copies).
    083     *
    084     * @param multiset  the multiset to decorate, must not be null
    085     * @param lock  the lock to use, must not be null
    086     * @throws NullPointerException if multiset or lock is null
    087     */
    088    protected SynchronizedMultiSet(final MultiSet<E> multiset, final Object lock) {
    089        super(multiset, lock);
    090    }
    091
    092    @Override
    093    public int add(final E object, final int count) {
    094        synchronized (lock) {
    095            return decorated().add(object, count);
    096        }
    097    }
    098
    099    /**
    100     * Gets the multiset being decorated.
    101     *
    102     * @return the decorated multiset
    103     */
    104    @Override
    105    protected MultiSet<E> decorated() {
    106        return (MultiSet<E>) super.decorated();
    107    }
    108
    109    @Override
    110    public Set<Entry<E>> entrySet() {
    111        synchronized (lock) {
    112            final Set<MultiSet.Entry<E>> set = decorated().entrySet();
    113            return new SynchronizedSet<>(set, lock);
    114        }
    115    }
    116
    117    @Override
    118    public boolean equals(final Object object) {
    119        if (object == this) {
    120            return true;
    121        }
    122        synchronized (lock) {
    123            return decorated().equals(object);
    124        }
    125    }
    126
    127    @Override
    128    public int getCount(final Object object) {
    129        synchronized (lock) {
    130            return decorated().getCount(object);
    131        }
    132    }
    133
    134    @Override
    135    public int hashCode() {
    136        synchronized (lock) {
    137            return decorated().hashCode();
    138        }
    139    }
    140
    141    @Override
    142    public int remove(final Object object, final int count) {
    143        synchronized (lock) {
    144            return decorated().remove(object, count);
    145        }
    146    }
    147
    148    @Override
    149    public int setCount(final E object, final int count) {
    150        synchronized (lock) {
    151            return decorated().setCount(object, count);
    152        }
    153    }
    154
    155    @Override
    156    public Set<E> uniqueSet() {
    157        synchronized (lock) {
    158            final Set<E> set = decorated().uniqueSet();
    159            return new SynchronizedSet<>(set, lock);
    160        }
    161    }
    162
    163}