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    *      http://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  
24  /**
25   * Decorates another {@link Bag} to synchronize its behavior
26   * for a multithreaded environment.
27   * <p>
28   * Methods are synchronized, then forwarded to the decorated bag.
29   * Iterators must be separately synchronized around the loop.
30   * </p>
31   * <p>
32   * This class is Serializable from Commons Collections 3.1.
33   * </p>
34   *
35   * @param <E> the type of elements in this bag
36   * @since 3.0
37   */
38  public class SynchronizedBag<E> extends SynchronizedCollection<E> implements Bag<E> {
39  
40      /**
41       * Synchronized Set for the Bag class.
42       */
43      final class SynchronizedBagSet extends SynchronizedCollection<E> implements Set<E> {
44          /** Serialization version */
45          private static final long serialVersionUID = 2990565892366827855L;
46  
47          /**
48           * Constructs a new instance.
49           * @param set  the set to decorate
50           * @param lock  the lock to use, shared with the bag
51           */
52          SynchronizedBagSet(final Set<E> set, final Object lock) {
53              super(set, lock);
54          }
55      }
56  
57      /** Serialization version */
58      private static final long serialVersionUID = 8084674570753837109L;
59  
60      /**
61       * Factory method to create a synchronized bag.
62       *
63       * @param <E> the type of the elements in the bag
64       * @param bag  the bag to decorate, must not be null
65       * @return a new synchronized Bag
66       * @throws NullPointerException if bag is null
67       * @since 4.0
68       */
69      public static <E> SynchronizedBag<E> synchronizedBag(final Bag<E> bag) {
70          return new SynchronizedBag<>(bag);
71      }
72  
73      /**
74       * Constructor that wraps (not copies).
75       *
76       * @param bag  the bag to decorate, must not be null
77       * @throws NullPointerException if bag is null
78       */
79      protected SynchronizedBag(final Bag<E> bag) {
80          super(bag);
81      }
82  
83      /**
84       * Constructor that wraps (not copies).
85       *
86       * @param bag  the bag to decorate, must not be null
87       * @param lock  the lock to use, must not be null
88       * @throws NullPointerException if bag or lock is null
89       */
90      protected SynchronizedBag(final Bag<E> bag, final Object lock) {
91          super(bag, lock);
92      }
93  
94      @Override
95      public boolean add(final E object, final int count) {
96          synchronized (lock) {
97              return getBag().add(object, count);
98          }
99      }
100 
101     @Override
102     public boolean equals(final Object object) {
103         if (object == this) {
104             return true;
105         }
106         synchronized (lock) {
107             return getBag().equals(object);
108         }
109     }
110 
111     /**
112      * Gets the bag being decorated.
113      *
114      * @return the decorated bag
115      */
116     protected Bag<E> getBag() {
117         return (Bag<E>) decorated();
118     }
119 
120     @Override
121     public int getCount(final Object object) {
122         synchronized (lock) {
123             return getBag().getCount(object);
124         }
125     }
126 
127     @Override
128     public int hashCode() {
129         synchronized (lock) {
130             return getBag().hashCode();
131         }
132     }
133 
134     @Override
135     public boolean remove(final Object object, final int count) {
136         synchronized (lock) {
137             return getBag().remove(object, count);
138         }
139     }
140 
141     @Override
142     public Set<E> uniqueSet() {
143         synchronized (lock) {
144             final Set<E> set = getBag().uniqueSet();
145             return new SynchronizedBagSet(set, lock);
146         }
147     }
148 
149 }