1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections.list;
18
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.ListIterator;
22
23 import org.apache.commons.collections.collection.SynchronizedCollection;
24
25 /**
26 * Decorates another <code>List</code> to synchronize its behaviour
27 * for a multi-threaded environment.
28 * <p>
29 * Methods are synchronized, then forwarded to the decorated list.
30 * <p>
31 * This class is Serializable from Commons Collections 3.1.
32 *
33 * @since 3.0
34 * @version $Id: SynchronizedList.java 1429905 2013-01-07 17:15:14Z ggregory $
35 */
36 public class SynchronizedList<E> extends SynchronizedCollection<E> implements List<E> {
37
38 /** Serialization version */
39 private static final long serialVersionUID = -1403835447328619437L;
40
41 /**
42 * Factory method to create a synchronized list.
43 *
44 * @param <T> the type of the elements in the list
45 * @param list the list to decorate, must not be null
46 * @return a new synchronized list
47 * @throws IllegalArgumentException if list is null
48 */
49 public static <T> SynchronizedList<T> synchronizedList(final List<T> list) {
50 return new SynchronizedList<T>(list);
51 }
52
53 //-----------------------------------------------------------------------
54 /**
55 * Constructor that wraps (not copies).
56 *
57 * @param list the list to decorate, must not be null
58 * @throws IllegalArgumentException if list is null
59 */
60 protected SynchronizedList(final List<E> list) {
61 super(list);
62 }
63
64 /**
65 * Constructor that wraps (not copies).
66 *
67 * @param list the list to decorate, must not be null
68 * @param lock the lock to use, must not be null
69 * @throws IllegalArgumentException if list is null
70 */
71 protected SynchronizedList(final List<E> list, final Object lock) {
72 super(list, lock);
73 }
74
75 /**
76 * Gets the decorated list.
77 *
78 * @return the decorated list
79 */
80 protected List<E> getList() {
81 return (List<E>) collection;
82 }
83
84 //-----------------------------------------------------------------------
85
86 public void add(final int index, final E object) {
87 synchronized (lock) {
88 getList().add(index, object);
89 }
90 }
91
92 public boolean addAll(final int index, final Collection<? extends E> coll) {
93 synchronized (lock) {
94 return getList().addAll(index, coll);
95 }
96 }
97
98 public E get(final int index) {
99 synchronized (lock) {
100 return getList().get(index);
101 }
102 }
103
104 public int indexOf(final Object object) {
105 synchronized (lock) {
106 return getList().indexOf(object);
107 }
108 }
109
110 public int lastIndexOf(final Object object) {
111 synchronized (lock) {
112 return getList().lastIndexOf(object);
113 }
114 }
115
116 /**
117 * Iterators must be manually synchronized.
118 * <pre>
119 * synchronized (coll) {
120 * ListIterator it = coll.listIterator();
121 * // do stuff with iterator
122 * }
123 *
124 * @return an iterator that must be manually synchronized on the collection
125 */
126 public ListIterator<E> listIterator() {
127 return getList().listIterator();
128 }
129
130 /**
131 * Iterators must be manually synchronized.
132 * <pre>
133 * synchronized (coll) {
134 * ListIterator it = coll.listIterator(3);
135 * // do stuff with iterator
136 * }
137 *
138 * @param index index of first element to be returned by this list iterator
139 * @return an iterator that must be manually synchronized on the collection
140 */
141 public ListIterator<E> listIterator(final int index) {
142 return getList().listIterator(index);
143 }
144
145 public E remove(final int index) {
146 synchronized (lock) {
147 return getList().remove(index);
148 }
149 }
150
151 public E set(final int index, final E object) {
152 synchronized (lock) {
153 return getList().set(index, object);
154 }
155 }
156
157 public List<E> subList(final int fromIndex, final int toIndex) {
158 synchronized (lock) {
159 final List<E> list = getList().subList(fromIndex, toIndex);
160 // the lock is passed into the constructor here to ensure that the sublist is
161 // synchronized on the same lock as the parent list
162 return new SynchronizedList<E>(list, lock);
163 }
164 }
165
166 }