    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 *      https://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.bag;
    018
    019import java.util.Set;
    020
    021import org.apache.commons.collections4.Bag;
    022import org.apache.commons.collections4.collection.SynchronizedCollection;
    023import org.apache.commons.collections4.multiset.SynchronizedMultiSet;
    024
    025/**
    026 * Decorates another {@link Bag} to synchronize its behavior
    027 * for a multithreaded environment.
    028 * <p>
    029 * Methods are synchronized, then forwarded to the decorated bag.
    030 * Iterators must be separately synchronized around the loop.
    031 * </p>
    032 * <p>
    033 * This class is Serializable from Commons Collections 3.1.
    034 * </p>
    035 *
    036 * @param <E> The type of elements in this bag
    037 * @since 3.0
    038 * @deprecated Since 4.6.0, use {@link SynchronizedMultiSet} instead.
    039 */
    040@Deprecated
    041public class SynchronizedBag<E> extends SynchronizedCollection<E> implements Bag<E> {
    042
    043    /**
    044     * Synchronized Set for the Bag class.
    045     */
    046    final class SynchronizedBagSet extends SynchronizedCollection<E> implements Set<E> {
    047
    048        /** Serialization version */
    049        private static final long serialVersionUID = 2990565892366827855L;
    050
    051        /**
    052         * Constructs a new instance.
    053         *
    054         * @param set  The set to decorate
    055         * @param lock  The lock to use, shared with the bag
    056         */
    057        SynchronizedBagSet(final Set<E> set, final Object lock) {
    058            super(set, lock);
    059        }
    060    }
    061
    062    /** Serialization version */
    063    private static final long serialVersionUID = 8084674570753837109L;
    064
    065    /**
    066     * Factory method to create a synchronized bag.
    067     *
    068     * @param <E> The type of the elements in the bag
    069     * @param bag  The bag to decorate, must not be null
    070     * @return A new synchronized Bag
    071     * @throws NullPointerException if bag is null
    072     * @since 4.0
    073     */
    074    public static <E> SynchronizedBag<E> synchronizedBag(final Bag<E> bag) {
    075        return new SynchronizedBag<>(bag);
    076    }
    077
    078    /**
    079     * Constructor that wraps (not copies).
    080     *
    081     * @param bag  The bag to decorate, must not be null
    082     * @throws NullPointerException if bag is null
    083     */
    084    protected SynchronizedBag(final Bag<E> bag) {
    085        super(bag);
    086    }
    087
    088    /**
    089     * Constructor that wraps (not copies).
    090     *
    091     * @param bag  The bag to decorate, must not be null
    092     * @param lock  The lock to use, must not be null
    093     * @throws NullPointerException if bag or lock is null
    094     */
    095    protected SynchronizedBag(final Bag<E> bag, final Object lock) {
    096        super(bag, lock);
    097    }
    098
    099    @Override
    100    public boolean add(final E object, final int count) {
    101        synchronized (lock) {
    102            return getBag().add(object, count);
    103        }
    104    }
    105
    106    @Override
    107    public boolean equals(final Object object) {
    108        if (object == this) {
    109            return true;
    110        }
    111        synchronized (lock) {
    112            return getBag().equals(object);
    113        }
    114    }
    115
    116    /**
    117     * Gets the bag being decorated.
    118     *
    119     * @return The decorated bag
    120     */
    121    protected Bag<E> getBag() {
    122        return (Bag<E>) decorated();
    123    }
    124
    125    @Override
    126    public int getCount(final Object object) {
    127        synchronized (lock) {
    128            return getBag().getCount(object);
    129        }
    130    }
    131
    132    @Override
    133    public int hashCode() {
    134        synchronized (lock) {
    135            return getBag().hashCode();
    136        }
    137    }
    138
    139    @Override
    140    public boolean remove(final Object object, final int count) {
    141        synchronized (lock) {
    142            return getBag().remove(object, count);
    143        }
    144    }
    145
    146    @Override
    147    public Set<E> uniqueSet() {
    148        synchronized (lock) {
    149            final Set<E> set = getBag().uniqueSet();
    150            return new SynchronizedBagSet(set, lock);
    151        }
    152    }
    153
    154}