[COLLECTIONS-351] Removed Synchronized[List, Set, SortedSet] and replaced with calls to Collections.synchronizedXXX.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1457511 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-03-17 18:39:57 +00:00
parent 3466da1a67
commit c340a74c7e
17 changed files with 11 additions and 575 deletions

View File

@ -28,7 +28,6 @@ import org.apache.commons.collections.bag.HashBag;
import org.apache.commons.collections.list.FixedSizeList; import org.apache.commons.collections.list.FixedSizeList;
import org.apache.commons.collections.list.LazyList; import org.apache.commons.collections.list.LazyList;
import org.apache.commons.collections.list.PredicatedList; import org.apache.commons.collections.list.PredicatedList;
import org.apache.commons.collections.list.SynchronizedList;
import org.apache.commons.collections.list.TransformedList; import org.apache.commons.collections.list.TransformedList;
import org.apache.commons.collections.list.UnmodifiableList; import org.apache.commons.collections.list.UnmodifiableList;
@ -353,7 +352,7 @@ public class ListUtils {
/** /**
* Returns a synchronized list backed by the given list. * Returns a synchronized list backed by the given list.
* <p> * <p>
* You must manually synchronize on the returned buffer's iterator to * You must manually synchronize on the returned list's iterator to
* avoid non-deterministic behavior: * avoid non-deterministic behavior:
* *
* <pre> * <pre>
@ -366,7 +365,7 @@ public class ListUtils {
* } * }
* </pre> * </pre>
* *
* This method uses the implementation in the decorators subpackage. * This method is just a wrapper for {@link Collections#synchronizedList(List)}.
* *
* @param <E> the element type * @param <E> the element type
* @param list the list to synchronize, must not be null * @param list the list to synchronize, must not be null
@ -374,7 +373,7 @@ public class ListUtils {
* @throws IllegalArgumentException if the list is null * @throws IllegalArgumentException if the list is null
*/ */
public static <E> List<E> synchronizedList(final List<E> list) { public static <E> List<E> synchronizedList(final List<E> list) {
return SynchronizedList.synchronizedList(list); return Collections.synchronizedList(list);
} }
/** /**

View File

@ -25,8 +25,6 @@ import java.util.TreeSet;
import org.apache.commons.collections.set.ListOrderedSet; import org.apache.commons.collections.set.ListOrderedSet;
import org.apache.commons.collections.set.PredicatedSet; import org.apache.commons.collections.set.PredicatedSet;
import org.apache.commons.collections.set.PredicatedSortedSet; import org.apache.commons.collections.set.PredicatedSortedSet;
import org.apache.commons.collections.set.SynchronizedSet;
import org.apache.commons.collections.set.SynchronizedSortedSet;
import org.apache.commons.collections.set.TransformedSet; import org.apache.commons.collections.set.TransformedSet;
import org.apache.commons.collections.set.TransformedSortedSet; import org.apache.commons.collections.set.TransformedSortedSet;
import org.apache.commons.collections.set.UnmodifiableSet; import org.apache.commons.collections.set.UnmodifiableSet;
@ -163,7 +161,7 @@ public class SetUtils {
/** /**
* Returns a synchronized set backed by the given set. * Returns a synchronized set backed by the given set.
* <p> * <p>
* You must manually synchronize on the returned buffer's iterator to * You must manually synchronize on the returned set's iterator to
* avoid non-deterministic behavior: * avoid non-deterministic behavior:
* *
* <pre> * <pre>
@ -176,7 +174,7 @@ public class SetUtils {
* } * }
* </pre> * </pre>
* *
* This method uses the implementation in the decorators subpackage. * This method is just a wrapper for {@link Collections#synchronizedSet(Set)}.
* *
* @param <E> the element type * @param <E> the element type
* @param set the set to synchronize, must not be null * @param set the set to synchronize, must not be null
@ -184,7 +182,7 @@ public class SetUtils {
* @throws IllegalArgumentException if the set is null * @throws IllegalArgumentException if the set is null
*/ */
public static <E> Set<E> synchronizedSet(final Set<E> set) { public static <E> Set<E> synchronizedSet(final Set<E> set) {
return SynchronizedSet.synchronizedSet(set); return Collections.synchronizedSet(set);
} }
/** /**
@ -259,7 +257,7 @@ public class SetUtils {
/** /**
* Returns a synchronized sorted set backed by the given sorted set. * Returns a synchronized sorted set backed by the given sorted set.
* <p> * <p>
* You must manually synchronize on the returned buffer's iterator to * You must manually synchronize on the returned set's iterator to
* avoid non-deterministic behavior: * avoid non-deterministic behavior:
* *
* <pre> * <pre>
@ -272,7 +270,7 @@ public class SetUtils {
* } * }
* </pre> * </pre>
* *
* This method uses the implementation in the decorators subpackage. * This method is just a wrapper for {@link Collections#synchronizedSortedSet(SortedSet)}.
* *
* @param <E> the element type * @param <E> the element type
* @param set the sorted set to synchronize, must not be null * @param set the sorted set to synchronize, must not be null
@ -280,7 +278,7 @@ public class SetUtils {
* @throws IllegalArgumentException if the set is null * @throws IllegalArgumentException if the set is null
*/ */
public static <E> SortedSet<E> synchronizedSortedSet(final SortedSet<E> set) { public static <E> SortedSet<E> synchronizedSortedSet(final SortedSet<E> set) {
return SynchronizedSortedSet.synchronizedSortedSet(set); return Collections.synchronizedSortedSet(set);
} }
/** /**

View File

@ -1,166 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.commons.collections.list;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
import org.apache.commons.collections.collection.SynchronizedCollection;
/**
* Decorates another <code>List</code> to synchronize its behaviour
* for a multi-threaded environment.
* <p>
* Methods are synchronized, then forwarded to the decorated list.
* <p>
* This class is Serializable from Commons Collections 3.1.
*
* @since 3.0
* @version $Id$
*/
public class SynchronizedList<E> extends SynchronizedCollection<E> implements List<E> {
/** Serialization version */
private static final long serialVersionUID = -1403835447328619437L;
/**
* Factory method to create a synchronized list.
*
* @param <T> the type of the elements in the list
* @param list the list to decorate, must not be null
* @return a new synchronized list
* @throws IllegalArgumentException if list is null
*/
public static <T> SynchronizedList<T> synchronizedList(final List<T> list) {
return new SynchronizedList<T>(list);
}
//-----------------------------------------------------------------------
/**
* Constructor that wraps (not copies).
*
* @param list the list to decorate, must not be null
* @throws IllegalArgumentException if list is null
*/
protected SynchronizedList(final List<E> list) {
super(list);
}
/**
* Constructor that wraps (not copies).
*
* @param list the list to decorate, must not be null
* @param lock the lock to use, must not be null
* @throws IllegalArgumentException if list is null
*/
protected SynchronizedList(final List<E> list, final Object lock) {
super(list, lock);
}
/**
* Gets the decorated list.
*
* @return the decorated list
*/
protected List<E> getList() {
return (List<E>) collection;
}
//-----------------------------------------------------------------------
public void add(final int index, final E object) {
synchronized (lock) {
getList().add(index, object);
}
}
public boolean addAll(final int index, final Collection<? extends E> coll) {
synchronized (lock) {
return getList().addAll(index, coll);
}
}
public E get(final int index) {
synchronized (lock) {
return getList().get(index);
}
}
public int indexOf(final Object object) {
synchronized (lock) {
return getList().indexOf(object);
}
}
public int lastIndexOf(final Object object) {
synchronized (lock) {
return getList().lastIndexOf(object);
}
}
/**
* Iterators must be manually synchronized.
* <pre>
* synchronized (coll) {
* ListIterator it = coll.listIterator();
* // do stuff with iterator
* }
*
* @return an iterator that must be manually synchronized on the collection
*/
public ListIterator<E> listIterator() {
return getList().listIterator();
}
/**
* Iterators must be manually synchronized.
* <pre>
* synchronized (coll) {
* ListIterator it = coll.listIterator(3);
* // do stuff with iterator
* }
*
* @param index index of first element to be returned by this list iterator
* @return an iterator that must be manually synchronized on the collection
*/
public ListIterator<E> listIterator(final int index) {
return getList().listIterator(index);
}
public E remove(final int index) {
synchronized (lock) {
return getList().remove(index);
}
}
public E set(final int index, final E object) {
synchronized (lock) {
return getList().set(index, object);
}
}
public List<E> subList(final int fromIndex, final int toIndex) {
synchronized (lock) {
final List<E> list = getList().subList(fromIndex, toIndex);
// the lock is passed into the constructor here to ensure that the sublist is
// synchronized on the same lock as the parent list
return new SynchronizedList<E>(list, lock);
}
}
}

View File

@ -26,7 +26,6 @@
* <p> * <p>
* The following decorators are provided in the package: * The following decorators are provided in the package:
* <ul> * <ul>
* <li>Synchronized - synchronizes method access for multi-threaded environments</li>
* <li>Unmodifiable - ensures the collection cannot be altered</li> * <li>Unmodifiable - ensures the collection cannot be altered</li>
* <li>Predicated - ensures that only elements that are valid according to a predicate can be added</li> * <li>Predicated - ensures that only elements that are valid according to a predicate can be added</li>
* <li>Transformed - transforms each element added</li> * <li>Transformed - transforms each element added</li>

View File

@ -1,82 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.commons.collections.set;
import java.util.Set;
import org.apache.commons.collections.collection.SynchronizedCollection;
/**
* Decorates another <code>Set</code> to synchronize its behaviour for a
* multi-threaded environment.
* <p>
* Methods are synchronized, then forwarded to the decorated set.
* <p>
* This class is Serializable from Commons Collections 3.1.
*
* @since 3.0
* @version $Id$
*/
public class SynchronizedSet<E> extends SynchronizedCollection<E> implements Set<E> {
/** Serialization version */
private static final long serialVersionUID = -8304417378626543635L;
/**
* Factory method to create a synchronized set.
*
* @param <E> the element type
* @param set the set to decorate, must not be null
* @return a new synchronized set
* @throws IllegalArgumentException if set is null
*/
public static <E> SynchronizedSet<E> synchronizedSet(final Set<E> set) {
return new SynchronizedSet<E>(set);
}
//-----------------------------------------------------------------------
/**
* Constructor that wraps (not copies).
*
* @param set the set to decorate, must not be null
* @throws IllegalArgumentException if set is null
*/
protected SynchronizedSet(final Set<E> set) {
super(set);
}
/**
* Constructor that wraps (not copies).
*
* @param set the set to decorate, must not be null
* @param lock the lock object to use, must not be null
* @throws IllegalArgumentException if set is null
*/
protected SynchronizedSet(final Set<E> set, final Object lock) {
super(set, lock);
}
/**
* Gets the decorated set.
*
* @return the decorated set
*/
protected Set<E> getSet() {
return (Set<E>) collection;
}
}

View File

@ -1,129 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.commons.collections.set;
import java.util.Comparator;
import java.util.SortedSet;
import org.apache.commons.collections.collection.SynchronizedCollection;
/**
* Decorates another <code>SortedSet</code> to synchronize its behaviour
* for a multi-threaded environment.
* <p>
* Methods are synchronized, then forwarded to the decorated set.
* <p>
* This class is Serializable from Commons Collections 3.1.
*
* @since 3.0
* @version $Id$
*/
public class SynchronizedSortedSet<E> extends SynchronizedCollection<E> implements SortedSet<E> {
/** Serialization version */
private static final long serialVersionUID = 2775582861954500111L;
/**
* Factory method to create a synchronized set.
*
* @param <E> the element type
* @param set the set to decorate, must not be null
* @return a new synchronized sorted set
* @throws IllegalArgumentException if set is null
*/
public static <E> SynchronizedSortedSet<E> synchronizedSortedSet(final SortedSet<E> set) {
return new SynchronizedSortedSet<E>(set);
}
//-----------------------------------------------------------------------
/**
* Constructor that wraps (not copies).
*
* @param set the set to decorate, must not be null
* @throws IllegalArgumentException if set is null
*/
protected SynchronizedSortedSet(final SortedSet<E> set) {
super(set);
}
/**
* Constructor that wraps (not copies).
*
* @param set the set to decorate, must not be null
* @param lock the lock object to use, must not be null
* @throws IllegalArgumentException if set is null
*/
protected SynchronizedSortedSet(final SortedSet<E> set, final Object lock) {
super(set, lock);
}
/**
* Gets the decorated set.
*
* @return the decorated set
*/
protected SortedSet<E> getSortedSet() {
return (SortedSet<E>) collection;
}
//-----------------------------------------------------------------------
public SortedSet<E> subSet(final E fromElement, final E toElement) {
synchronized (lock) {
final SortedSet<E> set = getSortedSet().subSet(fromElement, toElement);
// the lock is passed into the constructor here to ensure that the
// subset is synchronized on the same lock as the parent
return new SynchronizedSortedSet<E>(set, lock);
}
}
public SortedSet<E> headSet(final E toElement) {
synchronized (lock) {
final SortedSet<E> set = getSortedSet().headSet(toElement);
// the lock is passed into the constructor here to ensure that the
// headset is synchronized on the same lock as the parent
return new SynchronizedSortedSet<E>(set, lock);
}
}
public SortedSet<E> tailSet(final E fromElement) {
synchronized (lock) {
final SortedSet<E> set = getSortedSet().tailSet(fromElement);
// the lock is passed into the constructor here to ensure that the
// tailset is synchronized on the same lock as the parent
return new SynchronizedSortedSet<E>(set, lock);
}
}
public E first() {
synchronized (lock) {
return getSortedSet().first();
}
}
public E last() {
synchronized (lock) {
return getSortedSet().last();
}
}
public Comparator<? super E> comparator() {
synchronized (lock) {
return getSortedSet().comparator();
}
}
}

View File

@ -28,7 +28,6 @@
* </ul> * </ul>
* The following decorators are provided in the package: * The following decorators are provided in the package:
* <ul> * <ul>
* <li>Synchronized - synchronizes method access for multi-threaded environments
* <li>Unmodifiable - ensures the collection cannot be altered * <li>Unmodifiable - ensures the collection cannot be altered
* <li>Predicated - ensures that only elements that are valid according to a predicate can be added * <li>Predicated - ensures that only elements that are valid according to a predicate can be added
* <li>Transformed - transforms each element added * <li>Transformed - transforms each element added

View File

@ -26,7 +26,6 @@ import java.util.SortedMap;
import org.apache.commons.collections.Trie; import org.apache.commons.collections.Trie;
import org.apache.commons.collections.collection.SynchronizedCollection; import org.apache.commons.collections.collection.SynchronizedCollection;
import org.apache.commons.collections.set.SynchronizedSet;
/** /**
* A synchronized {@link Trie}. * A synchronized {@link Trie}.
@ -88,11 +87,11 @@ public class SynchronizedTrie<K, V> implements Trie<K, V>, Serializable {
} }
public synchronized Set<Entry<K, V>> entrySet() { public synchronized Set<Entry<K, V>> entrySet() {
return SynchronizedSet.synchronizedSet(delegate.entrySet()); return Collections.synchronizedSet(delegate.entrySet());
} }
public synchronized Set<K> keySet() { public synchronized Set<K> keySet() {
return SynchronizedSet.synchronizedSet(delegate.keySet()); return Collections.synchronizedSet(delegate.keySet());
} }
public synchronized Collection<V> values() { public synchronized Collection<V> values() {

View File

@ -1,59 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.commons.collections.list;
import java.util.ArrayList;
import java.util.List;
/**
* Extension of {@link AbstractListTest} for exercising the {@link SynchronizedList}
* implementation.
*
* @since Commons Collections 3.1
* @version $Revision$
*
* @author Stephen Colebourne
*/
public class SynchronizedListTest<E> extends AbstractListTest<E> {
public SynchronizedListTest(final String testName) {
super(testName);
}
@Override
public List<E> makeConfirmedCollection() {
return new ArrayList<E>();
}
@Override
public List<E> makeObject() {
return SynchronizedList.synchronizedList(new ArrayList<E>());
}
@Override
public String getCompatibilityVersion() {
return "3.1";
}
// public void testCreate() throws Exception {
// resetEmpty();
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/SynchronizedList.emptyCollection.version3.1.obj");
// resetFull();
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/SynchronizedList.fullCollection.version3.1.obj");
// }
}

View File

@ -1,61 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.commons.collections.set;
import java.util.HashSet;
import java.util.Set;
import junit.framework.Test;
import org.apache.commons.collections.BulkTest;
/**
* Extension of {@link AbstractSetTest} for exercising the
* {@link SynchronizedSet} implementation.
*
* @since 3.1
* @version $Id$
*/
public class SynchronizedSetTest<E> extends AbstractSetTest<E> {
public SynchronizedSetTest(final String testName) {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(SynchronizedSetTest.class);
}
//-------------------------------------------------------------------
@Override
public Set<E> makeObject() {
return SynchronizedSet.synchronizedSet(new HashSet<E>());
}
@Override
public String getCompatibilityVersion() {
return "3.1";
}
// public void testCreate() throws Exception {
// resetEmpty();
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/SynchronizedSet.emptyCollection.version3.1.obj");
// resetFull();
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/SynchronizedSet.fullCollection.version3.1.obj");
// }
}

View File

@ -1,61 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.commons.collections.set;
import java.util.SortedSet;
import java.util.TreeSet;
import junit.framework.Test;
import org.apache.commons.collections.BulkTest;
/**
* Extension of {@link AbstractSetTest} for exercising the
* {@link SynchronizedSortedSet} implementation.
*
* @since 3.1
* @version $Id$
*/
public class SynchronizedSortedSetTest<E> extends AbstractSortedSetTest<E> {
public SynchronizedSortedSetTest(final String testName) {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(SynchronizedSortedSetTest.class);
}
//-------------------------------------------------------------------
@Override
public SortedSet<E> makeObject() {
return SynchronizedSortedSet.synchronizedSortedSet(new TreeSet<E>());
}
@Override
public String getCompatibilityVersion() {
return "3.1";
}
// public void testCreate() throws Exception {
// resetEmpty();
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/SynchronizedSortedSet.emptyCollection.version3.1.obj");
// resetFull();
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/SynchronizedSortedSet.fullCollection.version3.1.obj");
// }
}