View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.bag;
18  
19  import java.util.Set;
20  
21  import org.apache.commons.collections4.Bag;
22  import org.apache.commons.collections4.collection.SynchronizedCollection;
23  import org.apache.commons.collections4.multiset.SynchronizedMultiSet;
24  
25  /**
26   * Decorates another {@link Bag} to synchronize its behavior
27   * for a multithreaded environment.
28   * <p>
29   * Methods are synchronized, then forwarded to the decorated bag.
30   * Iterators must be separately synchronized around the loop.
31   * </p>
32   * <p>
33   * This class is Serializable from Commons Collections 3.1.
34   * </p>
35   *
36   * @param <E> The type of elements in this bag
37   * @since 3.0
38   * @deprecated Since 4.6.0, use {@link SynchronizedMultiSet} instead.
39   */
40  @Deprecated
41  public class SynchronizedBag<E> extends SynchronizedCollection<E> implements Bag<E> {
42  
43      /**
44       * Synchronized Set for the Bag class.
45       */
46      final class SynchronizedBagSet extends SynchronizedCollection<E> implements Set<E> {
47  
48          /** Serialization version */
49          private static final long serialVersionUID = 2990565892366827855L;
50  
51          /**
52           * Constructs a new instance.
53           *
54           * @param set  The set to decorate
55           * @param lock  The lock to use, shared with the bag
56           */
57          SynchronizedBagSet(final Set<E> set, final Object lock) {
58              super(set, lock);
59          }
60      }
61  
62      /** Serialization version */
63      private static final long serialVersionUID = 8084674570753837109L;
64  
65      /**
66       * Factory method to create a synchronized bag.
67       *
68       * @param <E> The type of the elements in the bag
69       * @param bag  The bag to decorate, must not be null
70       * @return A new synchronized Bag
71       * @throws NullPointerException if bag is null
72       * @since 4.0
73       */
74      public static <E> SynchronizedBag<E> synchronizedBag(final Bag<E> bag) {
75          return new SynchronizedBag<>(bag);
76      }
77  
78      /**
79       * Constructor that wraps (not copies).
80       *
81       * @param bag  The bag to decorate, must not be null
82       * @throws NullPointerException if bag is null
83       */
84      protected SynchronizedBag(final Bag<E> bag) {
85          super(bag);
86      }
87  
88      /**
89       * Constructor that wraps (not copies).
90       *
91       * @param bag  The bag to decorate, must not be null
92       * @param lock  The lock to use, must not be null
93       * @throws NullPointerException if bag or lock is null
94       */
95      protected SynchronizedBag(final Bag<E> bag, final Object lock) {
96          super(bag, lock);
97      }
98  
99      @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 }