~smaioli/azureus/ubuntu-experimental

« back to all changes in this revision

Viewing changes to com/aelitis/azureus/core/util/CopyOnWriteList.java

MergedĀ VuzeĀ 4.2.0.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import org.gudy.azureus2.core3.util.*;
29
29
 
30
30
public class 
31
 
CopyOnWriteList 
 
31
CopyOnWriteList<T> 
 
32
implements Iterable<T>
32
33
{
33
34
        private static final boolean LOG_STATS = false;
34
35
        
35
36
        //private int mutation_count = 0;
36
37
        
37
 
        private List    list = Collections.EMPTY_LIST;
 
38
        private List<T> list = Collections.emptyList();
38
39
        
39
40
        private boolean visible = false;
40
41
        
95
96
 
96
97
        public void
97
98
        add(
98
 
                Object  obj )
 
99
                T       obj )
99
100
        {
100
101
                synchronized( this ){
101
102
                        
102
103
                        if ( visible ){
103
104
                                
104
 
                                List    new_list = new ArrayList( list );
 
105
                                List<T> new_list = new ArrayList<T>( list );
105
106
                                
106
107
                                //mutated();
107
108
                                
112
113
                                visible = false;
113
114
                                
114
115
                        }else{
115
 
                                if (list == Collections.EMPTY_LIST) {
116
 
                                        list = new ArrayList(initialCapacity);
 
116
                                if (list == Collections.emptyList()) {
 
117
                                        list = new ArrayList<T>(initialCapacity);
117
118
                                }
118
119
                                
119
120
                                list.add( obj );
123
124
        
124
125
        public boolean
125
126
        remove(
126
 
                Object  obj )
 
127
                T       obj )
127
128
        {
128
129
                synchronized( this ){
129
130
                        
130
131
                        if ( visible ){
131
132
 
132
 
                                List    new_list = new ArrayList( list );
 
133
                                List<T> new_list = new ArrayList<T>( list );
133
134
                                
134
135
                                //mutated();
135
136
                                
153
154
        {
154
155
                synchronized( this ){
155
156
                                                                
156
 
                        list    = Collections.EMPTY_LIST;
 
157
                        list    = Collections.emptyList();
157
158
                        
158
159
                        visible = false;
159
160
                }
161
162
        
162
163
        public boolean
163
164
        contains(
164
 
                Object  obj )
 
165
                T       obj )
165
166
        {
166
167
                synchronized( this ){
167
168
 
169
170
                }
170
171
        }
171
172
        
172
 
        public Iterator
 
173
        public Iterator<T>
173
174
        iterator()
174
175
        {
175
176
                synchronized( this ){
180
181
                }
181
182
        }
182
183
        
183
 
        public List
 
184
        public List<T>
184
185
        getList()
185
186
        {
186
187
                        // TODO: we need to either make this a read-only-list or obey the copy-on-write semantics correctly...
220
221
                }
221
222
        }
222
223
        
223
 
        public Object[]
 
224
        public T[]
224
225
        toArray(
225
 
                Object[]         x )
 
226
                T[]      x )
226
227
        {
227
228
                synchronized( this ){
228
229
 
245
246
        
246
247
        private class
247
248
        CopyOnWriteListIterator
248
 
                implements Iterator
 
249
                implements Iterator<T>
249
250
        {
250
 
                private Iterator        it;
251
 
                private Object          last;
 
251
                private Iterator<T>     it;
 
252
                private T                       last;
252
253
                
253
254
                protected
254
255
                CopyOnWriteListIterator(
255
 
                        Iterator                _it )
 
256
                        Iterator<T>             _it )
256
257
                {
257
258
                        it              = _it;
258
259
                }
263
264
                        return( it.hasNext());
264
265
                }
265
266
                
266
 
                public Object
 
267
                public T
267
268
                next()
268
269
                {
269
270
                        last    = it.next();