001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.multiset; 018 019import java.util.Set; 020 021import org.apache.commons.collections4.MultiSet; 022import org.apache.commons.collections4.collection.SynchronizedCollection; 023 024/** 025 * Decorates another {@link MultiSet} to synchronize its behavior 026 * for a multithreaded environment. 027 * <p> 028 * Methods are synchronized, then forwarded to the decorated multiset. 029 * Iterators must be separately synchronized around the loop. 030 * </p> 031 * 032 * @param <E> the type held in the multiset 033 * @since 4.1 034 */ 035public class SynchronizedMultiSet<E> extends SynchronizedCollection<E> implements MultiSet<E> { 036 037 /** 038 * Synchronized Set for the MultiSet class. 039 */ 040 static class SynchronizedSet<T> extends SynchronizedCollection<T> implements Set<T> { 041 /** Serialization version */ 042 private static final long serialVersionUID = 20150629L; 043 044 /** 045 * Constructs a new instance. 046 * @param set the set to decorate 047 * @param lock the lock to use, shared with the multiset 048 */ 049 SynchronizedSet(final Set<T> set, final Object lock) { 050 super(set, lock); 051 } 052 } 053 054 /** Serialization version */ 055 private static final long serialVersionUID = 20150629L; 056 057 /** 058 * Factory method to create a synchronized multiset. 059 * 060 * @param <E> the type of the elements in the multiset 061 * @param multiset the multiset to decorate, must not be null 062 * @return a new synchronized MultiSet 063 * @throws NullPointerException if multiset is null 064 */ 065 public static <E> SynchronizedMultiSet<E> synchronizedMultiSet(final MultiSet<E> multiset) { 066 return new SynchronizedMultiSet<>(multiset); 067 } 068 069 /** 070 * Constructor that wraps (not copies). 071 * 072 * @param multiset the multiset to decorate, must not be null 073 * @throws NullPointerException if multiset is null 074 */ 075 protected SynchronizedMultiSet(final MultiSet<E> multiset) { 076 super(multiset); 077 } 078 079 /** 080 * Constructor that wraps (not copies). 081 * 082 * @param multiset the multiset to decorate, must not be null 083 * @param lock the lock to use, must not be null 084 * @throws NullPointerException if multiset or lock is null 085 */ 086 protected SynchronizedMultiSet(final MultiSet<E> multiset, final Object lock) { 087 super(multiset, lock); 088 } 089 090 @Override 091 public int add(final E object, final int count) { 092 synchronized (lock) { 093 return decorated().add(object, count); 094 } 095 } 096 097 /** 098 * Gets the multiset being decorated. 099 * 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}