/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.axis.utils; import java.io.Serializable; import java.util.Collection; import java.util.Collections; import java.util.Map; import java.util.Set; /** *

* Enhancement of {@link Collections#synchronizedMap(Map)} method to provide the * ability to specify the ability to specify the mutex used for * the synchronization. *

* * @see Collections#synchronizedMap(Map) * @author Cyrille Le Clerc */ public class SynchronizedMap implements Map, Serializable { private static final long serialVersionUID = 1L; private Map wrapped; private Object mutex; /** *

* Synchronize the given map against the given * mutex. *

* * @param map * to synchronize * @param mutex * to synchronize the given map */ public SynchronizedMap(Map map, Object mutex) { if (map == null) { throw new NullPointerException("Map can not be null"); } if (mutex == null) { throw new NullPointerException("Mutex can not be null"); } this.wrapped = map; this.mutex = mutex; } public void clear() { wrapped.clear(); } public boolean containsKey(Object key) { synchronized (mutex) { return wrapped.containsKey(key); } } public boolean containsValue(Object value) { synchronized (mutex) { return wrapped.containsValue(value); } } public Set entrySet() { synchronized (mutex) { return wrapped.entrySet(); } } public boolean equals(Object o) { synchronized (mutex) { return wrapped.equals(o); } } public Object get(Object key) { synchronized (mutex) { return wrapped.get(key); } } public int hashCode() { synchronized (mutex) { return wrapped.hashCode(); } } public boolean isEmpty() { synchronized (mutex) { return wrapped.isEmpty(); } } public Set keySet() { synchronized (mutex) { return wrapped.keySet(); } } public Object put(Object key, Object value) { synchronized (mutex) { return wrapped.put(key, value); } } public void putAll(Map map) { synchronized (mutex) { wrapped.putAll(map); } } public Object remove(Object key) { synchronized (mutex) { return wrapped.remove(key); } } public int size() { synchronized (mutex) { return wrapped.size(); } } public Collection values() { synchronized (mutex) { return wrapped.values(); } } }