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.multiset;
18  
19  import java.util.Set;
20  
21  import org.apache.commons.collections4.MultiSet;
22  import org.apache.commons.collections4.collection.SynchronizedCollection;
23  
24  /**
25   * Decorates another {@link MultiSet} to synchronize its behavior
26   * for a multithreaded environment.
27   * <p>
28   * Methods are synchronized, then forwarded to the decorated multiset.
29   * Iterators must be separately synchronized around the loop.
30   * </p>
31   *
32   * @param <E> The type held in the multiset.
33   * @since 4.1
34   */
35  public class SynchronizedMultiSet<E> extends SynchronizedCollection<E> implements MultiSet<E> {
36  
37      /**
38       * Synchronized Set for the MultiSet class.
39       *
40       * @param <T> The type held in this Set.
41       */
42      static class SynchronizedSet<T> extends SynchronizedCollection<T> implements Set<T> {
43  
44          /** Serialization version */
45          private static final long serialVersionUID = 20150629L;
46  
47          /**
48           * Constructs a new instance.
49           *
50           * @param set  The set to decorate
51           * @param lock  The lock to use, shared with the multiset
52           */
53          SynchronizedSet(final Set<T> set, final Object lock) {
54              super(set, lock);
55          }
56      }
57  
58      /** Serialization version */
59      private static final long serialVersionUID = 20150629L;
60  
61      /**
62       * Factory method to create a synchronized multiset.
63       *
64       * @param <E> The type of the elements in the multiset
65       * @param multiset  The multiset to decorate, must not be null
66       * @return A new synchronized MultiSet
67       * @throws NullPointerException if multiset is null
68       */
69      public static <E> SynchronizedMultiSet<E> synchronizedMultiSet(final MultiSet<E> multiset) {
70          return new SynchronizedMultiSet<>(multiset);
71      }
72  
73      /**
74       * Constructor that wraps (not copies).
75       *
76       * @param multiset  The multiset to decorate, must not be null
77       * @throws NullPointerException if multiset is null
78       */
79      protected SynchronizedMultiSet(final MultiSet<E> multiset) {
80          super(multiset);
81      }
82  
83      /**
84       * Constructor that wraps (not copies).
85       *
86       * @param multiset  The multiset to decorate, must not be null
87       * @param lock  The lock to use, must not be null
88       * @throws NullPointerException if multiset or lock is null
89       */
90      protected SynchronizedMultiSet(final MultiSet<E> multiset, final Object lock) {
91          super(multiset, lock);
92      }
93  
94      @Override
95      public int add(final E object, final int count) {
96          synchronized (lock) {
97              return decorated().add(object, count);
98          }
99      }
100 
101     /**
102      * Gets the multiset being decorated.
103      *
104      * @return The decorated multiset
105      */
106     @Override
107     protected MultiSet<E> decorated() {
108         return (MultiSet<E>) super.decorated();
109     }
110 
111     @Override
112     public Set<Entry<E>> entrySet() {
113         synchronized (lock) {
114             final Set<MultiSet.Entry<E>> set = decorated().entrySet();
115             return new SynchronizedSet<>(set, lock);
116         }
117     }
118 
119     @Override
120     public boolean equals(final Object object) {
121         if (object == this) {
122             return true;
123         }
124         synchronized (lock) {
125             return decorated().equals(object);
126         }
127     }
128 
129     @Override
130     public int getCount(final Object object) {
131         synchronized (lock) {
132             return decorated().getCount(object);
133         }
134     }
135 
136     @Override
137     public int hashCode() {
138         synchronized (lock) {
139             return decorated().hashCode();
140         }
141     }
142 
143     @Override
144     public int remove(final Object object, final int count) {
145         synchronized (lock) {
146             return decorated().remove(object, count);
147         }
148     }
149 
150     @Override
151     public int setCount(final E object, final int count) {
152         synchronized (lock) {
153             return decorated().setCount(object, count);
154         }
155     }
156 
157     @Override
158     public Set<E> uniqueSet() {
159         synchronized (lock) {
160             final Set<E> set = decorated().uniqueSet();
161             return new SynchronizedSet<>(set, lock);
162         }
163     }
164 
165 }