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.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      static class SynchronizedSet<T> extends SynchronizedCollection<T> implements Set<T> {
41          /** Serialization version */
42          private static final long serialVersionUID = 20150629L;
43  
44          /**
45           * Constructs a new instance.
46           * @param set  the set to decorate
47           * @param lock  the lock to use, shared with the multiset
48           */
49          SynchronizedSet(final Set<T> set, final Object lock) {
50              super(set, lock);
51          }
52      }
53  
54      /** Serialization version */
55      private static final long serialVersionUID = 20150629L;
56  
57      /**
58       * Factory method to create a synchronized multiset.
59       *
60       * @param <E> the type of the elements in the multiset
61       * @param multiset  the multiset to decorate, must not be null
62       * @return a new synchronized MultiSet
63       * @throws NullPointerException if multiset is null
64       */
65      public static <E> SynchronizedMultiSet<E> synchronizedMultiSet(final MultiSet<E> multiset) {
66          return new SynchronizedMultiSet<>(multiset);
67      }
68  
69      /**
70       * Constructor that wraps (not copies).
71       *
72       * @param multiset  the multiset to decorate, must not be null
73       * @throws NullPointerException if multiset is null
74       */
75      protected SynchronizedMultiSet(final MultiSet<E> multiset) {
76          super(multiset);
77      }
78  
79      /**
80       * Constructor that wraps (not copies).
81       *
82       * @param multiset  the multiset to decorate, must not be null
83       * @param lock  the lock to use, must not be null
84       * @throws NullPointerException if multiset or lock is null
85       */
86      protected SynchronizedMultiSet(final MultiSet<E> multiset, final Object lock) {
87          super(multiset, lock);
88      }
89  
90      @Override
91      public int add(final E object, final int count) {
92          synchronized (lock) {
93              return decorated().add(object, count);
94          }
95      }
96  
97      /**
98       * Gets the multiset being decorated.
99       *
100      * @return the decorated multiset
101      */
102     @Override
103     protected MultiSet<E> decorated() {
104         return (MultiSet<E>) super.decorated();
105     }
106 
107     @Override
108     public Set<Entry<E>> entrySet() {
109         synchronized (lock) {
110             final Set<MultiSet.Entry<E>> set = decorated().entrySet();
111             return new SynchronizedSet<>(set, lock);
112         }
113     }
114 
115     @Override
116     public boolean equals(final Object object) {
117         if (object == this) {
118             return true;
119         }
120         synchronized (lock) {
121             return decorated().equals(object);
122         }
123     }
124 
125     @Override
126     public int getCount(final Object object) {
127         synchronized (lock) {
128             return decorated().getCount(object);
129         }
130     }
131 
132     @Override
133     public int hashCode() {
134         synchronized (lock) {
135             return decorated().hashCode();
136         }
137     }
138 
139     @Override
140     public int remove(final Object object, final int count) {
141         synchronized (lock) {
142             return decorated().remove(object, count);
143         }
144     }
145 
146     @Override
147     public int setCount(final E object, final int count) {
148         synchronized (lock) {
149             return decorated().setCount(object, count);
150         }
151     }
152 
153     @Override
154     public Set<E> uniqueSet() {
155         synchronized (lock) {
156             final Set<E> set = decorated().uniqueSet();
157             return new SynchronizedSet<>(set, lock);
158         }
159     }
160 
161 }