KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > SynchronizedCharCollection


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2002 Søren Bak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package bak.pcj;
20
21 import bak.pcj.util.Exceptions;
22
23 /**
24  * This class represents synchronized collections of char values. As in
25  * the Java Collections API iterations over the collection must be
26  * synchronized on the collection itself.
27  *
28  * @see java.util.Collections#synchronizedCollection(java.util.Collection)
29  *
30  * @author Søren Bak
31  * @version 1.1 21-08-2003 20:17
32  * @since 1.0
33  */

34 public class SynchronizedCharCollection implements CharCollection {
35
36     /** The collection underlying this synchronized collection. */
37     protected CharCollection collection;
38
39     /** An object on which to synchronize this collection's methods. */
40     protected Object JavaDoc m;
41
42     /**
43      * Creates a new synchronized collection on an existing
44      * collection. The result is a collection whose elements and
45      * behaviour is the same as the existing collection's except
46      * that the new collection's methods are synchronized.
47      *
48      * @param c
49      * the existing collection to make synchronized.
50      *
51      * @throws NullPointerException
52      * if <tt>c</tt> is <tt>null</tt>.
53      */

54     public SynchronizedCharCollection(CharCollection c) {
55         if (c == null)
56             Exceptions.nullArgument("collection");
57         this.collection = c;
58         this.m = this;
59     }
60
61     public boolean add(char v)
62     { synchronized (m) { return collection.add(v); } }
63
64     public boolean addAll(CharCollection c)
65     { synchronized (m) { return collection.addAll(c); } }
66
67     public void clear()
68     { synchronized (m) { collection.clear(); } }
69
70     public boolean contains(char v)
71     { synchronized (m) { return collection.contains(v); } }
72
73     public boolean containsAll(CharCollection c)
74     { synchronized (m) { return collection.containsAll(c); } }
75
76     public boolean equals(Object JavaDoc obj)
77     { synchronized (m) { return collection.equals(obj); } }
78
79     public int hashCode()
80     { synchronized(m) { return collection.hashCode(); } }
81
82     public boolean isEmpty()
83     { synchronized (m) { return collection.isEmpty(); } }
84
85     public CharIterator iterator()
86     { synchronized (m) { return collection.iterator(); } }
87
88     public boolean remove(char v)
89     { synchronized (m) { return collection.remove(v); } }
90
91     public boolean removeAll(CharCollection c)
92     { synchronized (m) { return collection.removeAll(c); } }
93
94     public boolean retainAll(CharCollection c)
95     { synchronized (m) { return collection.retainAll(c); } }
96
97     public int size()
98     { synchronized (m) { return collection.size(); } }
99
100     public char[] toArray()
101     { synchronized (m) { return collection.toArray(); } }
102
103     public char[] toArray(char[] a)
104     { synchronized (m) { return collection.toArray(a); } }
105
106     public void trimToSize()
107     { synchronized (m) { collection.trimToSize(); } }
108
109 }
Popular Tags