001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.list;
017:
018: import java.util.Collection;
019: import java.util.List;
020: import java.util.ListIterator;
021:
022: import org.apache.commons.collections.collection.SynchronizedCollection;
023:
024: /**
025: * Decorates another <code>List</code> to synchronize its behaviour
026: * for a multi-threaded environment.
027: * <p>
028: * Methods are synchronized, then forwarded to the decorated list.
029: * <p>
030: * This class is Serializable from Commons Collections 3.1.
031: *
032: * @since Commons Collections 3.0
033: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
034: *
035: * @author Stephen Colebourne
036: */
037: public class SynchronizedList extends SynchronizedCollection implements
038: List {
039:
040: /** Serialization version */
041: private static final long serialVersionUID = -1403835447328619437L;
042:
043: /**
044: * Factory method to create a synchronized list.
045: *
046: * @param list the list to decorate, must not be null
047: * @throws IllegalArgumentException if list is null
048: */
049: public static List decorate(List list) {
050: return new SynchronizedList(list);
051: }
052:
053: //-----------------------------------------------------------------------
054: /**
055: * Constructor that wraps (not copies).
056: *
057: * @param list the list to decorate, must not be null
058: * @throws IllegalArgumentException if list is null
059: */
060: protected SynchronizedList(List list) {
061: super (list);
062: }
063:
064: /**
065: * Constructor that wraps (not copies).
066: *
067: * @param list the list to decorate, must not be null
068: * @param lock the lock to use, must not be null
069: * @throws IllegalArgumentException if list is null
070: */
071: protected SynchronizedList(List list, Object lock) {
072: super (list, lock);
073: }
074:
075: /**
076: * Gets the decorated list.
077: *
078: * @return the decorated list
079: */
080: protected List getList() {
081: return (List) collection;
082: }
083:
084: //-----------------------------------------------------------------------
085: public void add(int index, Object object) {
086: synchronized (lock) {
087: getList().add(index, object);
088: }
089: }
090:
091: public boolean addAll(int index, Collection coll) {
092: synchronized (lock) {
093: return getList().addAll(index, coll);
094: }
095: }
096:
097: public Object get(int index) {
098: synchronized (lock) {
099: return getList().get(index);
100: }
101: }
102:
103: public int indexOf(Object object) {
104: synchronized (lock) {
105: return getList().indexOf(object);
106: }
107: }
108:
109: public int lastIndexOf(Object object) {
110: synchronized (lock) {
111: return getList().lastIndexOf(object);
112: }
113: }
114:
115: /**
116: * Iterators must be manually synchronized.
117: * <pre>
118: * synchronized (coll) {
119: * ListIterator it = coll.listIterator();
120: * // do stuff with iterator
121: * }
122: *
123: * @return an iterator that must be manually synchronized on the collection
124: */
125: public ListIterator listIterator() {
126: return getList().listIterator();
127: }
128:
129: /**
130: * Iterators must be manually synchronized.
131: * <pre>
132: * synchronized (coll) {
133: * ListIterator it = coll.listIterator(3);
134: * // do stuff with iterator
135: * }
136: *
137: * @return an iterator that must be manually synchronized on the collection
138: */
139: public ListIterator listIterator(int index) {
140: return getList().listIterator(index);
141: }
142:
143: public Object remove(int index) {
144: synchronized (lock) {
145: return getList().remove(index);
146: }
147: }
148:
149: public Object set(int index, Object object) {
150: synchronized (lock) {
151: return getList().set(index, object);
152: }
153: }
154:
155: public List subList(int fromIndex, int toIndex) {
156: synchronized (lock) {
157: List list = getList().subList(fromIndex, toIndex);
158: // the lock is passed into the constructor here to ensure that the sublist is
159: // synchronized on the same lock as the parent list
160: return new SynchronizedList(list, lock);
161: }
162: }
163:
164: }
|