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.collections.bag;
18
19 import java.util.Set;
20
21 import org.apache.commons.collections.Bag;
22 import org.apache.commons.collections.collection.SynchronizedCollection;
23 import org.apache.commons.collections.set.SynchronizedSet;
24
25 /**
26 * Decorates another {@link Bag} to synchronize its behaviour
27 * for a multi-threaded 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 * This class is Serializable from Commons Collections 3.1.
33 *
34 * @since 3.0
35 * @version $Id: SynchronizedBag.java 1429905 2013-01-07 17:15:14Z ggregory $
36 */
37 public class SynchronizedBag<E> extends SynchronizedCollection<E> implements Bag<E> {
38
39 /** Serialization version */
40 private static final long serialVersionUID = 8084674570753837109L;
41
42 /**
43 * Factory method to create a synchronized bag.
44 *
45 * @param <E> the type of the elements in the bag
46 * @param bag the bag to decorate, must not be null
47 * @return a new synchronized Bag
48 * @throws IllegalArgumentException if bag is null
49 */
50 public static <E> SynchronizedBag<E> synchronizedBag(final Bag<E> bag) {
51 return new SynchronizedBag<E>(bag);
52 }
53
54 //-----------------------------------------------------------------------
55 /**
56 * Constructor that wraps (not copies).
57 *
58 * @param bag the bag to decorate, must not be null
59 * @throws IllegalArgumentException if bag is null
60 */
61 protected SynchronizedBag(final Bag<E> bag) {
62 super(bag);
63 }
64
65 /**
66 * Constructor that wraps (not copies).
67 *
68 * @param bag the bag to decorate, must not be null
69 * @param lock the lock to use, must not be null
70 * @throws IllegalArgumentException if bag is null
71 */
72 protected SynchronizedBag(final Bag<E> bag, final Object lock) {
73 super(bag, lock);
74 }
75
76 /**
77 * Gets the bag being decorated.
78 *
79 * @return the decorated bag
80 */
81 protected Bag<E> getBag() {
82 return (Bag<E>) collection;
83 }
84
85 //-----------------------------------------------------------------------
86
87 public boolean add(final E object, final int count) {
88 synchronized (lock) {
89 return getBag().add(object, count);
90 }
91 }
92
93 public boolean remove(final Object object, final int count) {
94 synchronized (lock) {
95 return getBag().remove(object, count);
96 }
97 }
98
99 public Set<E> uniqueSet() {
100 synchronized (lock) {
101 final Set<E> set = getBag().uniqueSet();
102 return new SynchronizedBagSet(set, lock);
103 }
104 }
105
106 public int getCount(final Object object) {
107 synchronized (lock) {
108 return getBag().getCount(object);
109 }
110 }
111
112 //-----------------------------------------------------------------------
113 /**
114 * Synchronized Set for the Bag class.
115 */
116 class SynchronizedBagSet extends SynchronizedSet<E> {
117 /** Serialization version */
118 private static final long serialVersionUID = 2990565892366827855L;
119
120 /**
121 * Constructor.
122 * @param set the set to decorate
123 * @param lock the lock to use, shared with the bag
124 */
125 SynchronizedBagSet(final Set<E> set, final Object lock) {
126 super(set, lock);
127 }
128 }
129
130 }