KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > list > SynchronizedList


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.list;
17
18 import java.util.Collection JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.ListIterator JavaDoc;
21
22 import org.apache.commons.collections.collection.SynchronizedCollection;
23
24 /**
25  * Decorates another <code>List</code> to synchronize its behaviour
26  * for a multi-threaded environment.
27  * <p>
28  * Methods are synchronized, then forwarded to the decorated list.
29  * <p>
30  * This class is Serializable from Commons Collections 3.1.
31  *
32  * @since Commons Collections 3.0
33  * @version $Revision: 1.5 $ $Date: 2004/06/03 22:02:13 $
34  *
35  * @author Stephen Colebourne
36  */

37 public class SynchronizedList extends SynchronizedCollection implements List JavaDoc {
38
39     /** Serialization version */
40      private static final long serialVersionUID = -1403835447328619437L;
41
42     /**
43      * Factory method to create a synchronized list.
44      *
45      * @param list the list to decorate, must not be null
46      * @throws IllegalArgumentException if list is null
47      */

48     public static List JavaDoc decorate(List JavaDoc list) {
49         return new SynchronizedList(list);
50     }
51     
52     //-----------------------------------------------------------------------
53
/**
54      * Constructor that wraps (not copies).
55      *
56      * @param list the list to decorate, must not be null
57      * @throws IllegalArgumentException if list is null
58      */

59     protected SynchronizedList(List JavaDoc list) {
60         super(list);
61     }
62
63     /**
64      * Constructor that wraps (not copies).
65      *
66      * @param list the list to decorate, must not be null
67      * @param lock the lock to use, must not be null
68      * @throws IllegalArgumentException if list is null
69      */

70     protected SynchronizedList(List JavaDoc list, Object JavaDoc lock) {
71         super(list, lock);
72     }
73
74     /**
75      * Gets the decorated list.
76      *
77      * @return the decorated list
78      */

79     protected List JavaDoc getList() {
80         return (List JavaDoc) collection;
81     }
82
83     //-----------------------------------------------------------------------
84
public void add(int index, Object JavaDoc object) {
85         synchronized (lock) {
86             getList().add(index, object);
87         }
88     }
89
90     public boolean addAll(int index, Collection JavaDoc coll) {
91         synchronized (lock) {
92             return getList().addAll(index, coll);
93         }
94     }
95
96     public Object JavaDoc get(int index) {
97         synchronized (lock) {
98             return getList().get(index);
99         }
100     }
101
102     public int indexOf(Object JavaDoc object) {
103         synchronized (lock) {
104             return getList().indexOf(object);
105         }
106     }
107
108     public int lastIndexOf(Object JavaDoc object) {
109         synchronized (lock) {
110             return getList().lastIndexOf(object);
111         }
112     }
113
114     /**
115      * Iterators must be manually synchronized.
116      * <pre>
117      * synchronized (coll) {
118      * ListIterator it = coll.listIterator();
119      * // do stuff with iterator
120      * }
121      *
122      * @return an iterator that must be manually synchronized on the collection
123      */

124     public ListIterator JavaDoc listIterator() {
125         return getList().listIterator();
126     }
127
128     /**
129      * Iterators must be manually synchronized.
130      * <pre>
131      * synchronized (coll) {
132      * ListIterator it = coll.listIterator(3);
133      * // do stuff with iterator
134      * }
135      *
136      * @return an iterator that must be manually synchronized on the collection
137      */

138     public ListIterator JavaDoc listIterator(int index) {
139         return getList().listIterator(index);
140     }
141
142     public Object JavaDoc remove(int index) {
143         synchronized (lock) {
144             return getList().remove(index);
145         }
146     }
147
148     public Object JavaDoc set(int index, Object JavaDoc object) {
149         synchronized (lock) {
150             return getList().set(index, object);
151         }
152     }
153
154     public List JavaDoc subList(int fromIndex, int toIndex) {
155         synchronized (lock) {
156             List JavaDoc list = getList().subList(fromIndex, toIndex);
157             // the lock is passed into the constructor here to ensure that the sublist is
158
// synchronized on the same lock as the parent list
159
return new SynchronizedList(list, lock);
160         }
161     }
162
163 }
164
Popular Tags