KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > template > utility > Collections12


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.template.utility;
54
55 import java.io.Serializable JavaDoc;
56 import java.util.AbstractList JavaDoc;
57 import java.util.AbstractMap JavaDoc;
58 import java.util.Collection JavaDoc;
59 import java.util.Collections JavaDoc;
60 import java.util.List JavaDoc;
61 import java.util.Map JavaDoc;
62 import java.util.Set JavaDoc;
63
64 /**
65  * Implementation of missing JDK 1.3 collection features for JDK 1.2
66  * @author Attila Szegedi
67  * @version $Id: Collections12.java,v 1.2 2004/11/27 14:49:57 ddekany Exp $
68  */

69 public class Collections12
70 {
71     public static final Map JavaDoc EMPTY_MAP = new EmptyMap();
72
73     private Collections12()
74     {
75     }
76
77     private static final class EmptyMap
78         extends AbstractMap JavaDoc
79         implements Serializable JavaDoc
80     {
81         public int size()
82         {
83             return 0;
84         }
85
86         public boolean isEmpty()
87         {
88             return true;
89         }
90
91         public boolean containsKey(Object JavaDoc key)
92         {
93             return false;
94         }
95
96         public boolean containsValue(Object JavaDoc value)
97         {
98             return false;
99         }
100
101         public Object JavaDoc get(Object JavaDoc key)
102         {
103             return null;
104         }
105
106         public Set JavaDoc keySet()
107         {
108             return Collections.EMPTY_SET;
109         }
110
111         public Collection JavaDoc values()
112         {
113             return Collections.EMPTY_SET;
114         }
115
116         public Set JavaDoc entrySet()
117         {
118             return Collections.EMPTY_SET;
119         }
120
121         public boolean equals(Object JavaDoc o)
122         {
123             return (o instanceof Map JavaDoc) && ((Map JavaDoc) o).size() == 0;
124         }
125
126         public int hashCode()
127         {
128             return 0;
129         }
130     }
131
132     public static Map JavaDoc singletonMap(Object JavaDoc key, Object JavaDoc value)
133     {
134         return new SingletonMap(key, value);
135     }
136
137     private static class SingletonMap
138         extends AbstractMap JavaDoc
139         implements Serializable JavaDoc
140     {
141         private final Object JavaDoc k, v;
142
143         SingletonMap(Object JavaDoc key, Object JavaDoc value)
144         {
145             k = key;
146             v = value;
147         }
148
149         public int size()
150         {
151             return 1;
152         }
153
154         public boolean isEmpty()
155         {
156             return false;
157         }
158
159         public boolean containsKey(Object JavaDoc key)
160         {
161             return eq(key, k);
162         }
163
164         public boolean containsValue(Object JavaDoc value)
165         {
166             return eq(value, v);
167         }
168
169         public Object JavaDoc get(Object JavaDoc key)
170         {
171             return (eq(key, k) ? v : null);
172         }
173
174         private transient Set JavaDoc keySet = null;
175         private transient Set JavaDoc entrySet = null;
176         private transient Collection JavaDoc values = null;
177
178         public Set JavaDoc keySet()
179         {
180             if (keySet == null)
181                 keySet = Collections.singleton(k);
182             return keySet;
183         }
184
185         public Set JavaDoc entrySet()
186         {
187             if (entrySet == null)
188                 entrySet = Collections.singleton(new ImmutableEntry(k, v));
189             return entrySet;
190         }
191
192         public Collection JavaDoc values()
193         {
194             if (values == null)
195                 values = Collections.singleton(v);
196             return values;
197         }
198
199         private static class ImmutableEntry implements Map.Entry JavaDoc
200         {
201             final Object JavaDoc k;
202             final Object JavaDoc v;
203
204             ImmutableEntry(Object JavaDoc key, Object JavaDoc value)
205             {
206                 k = key;
207                 v = value;
208             }
209
210             public Object JavaDoc getKey()
211             {
212                 return k;
213             }
214
215             public Object JavaDoc getValue()
216             {
217                 return v;
218             }
219
220             public Object JavaDoc setValue(Object JavaDoc value)
221             {
222                 throw new UnsupportedOperationException JavaDoc();
223             }
224
225             public boolean equals(Object JavaDoc o)
226             {
227                 if (!(o instanceof Map.Entry JavaDoc))
228                     return false;
229                 Map.Entry JavaDoc e = (Map.Entry JavaDoc) o;
230                 return eq(e.getKey(), k) && eq(e.getValue(), v);
231             }
232
233             public int hashCode()
234             {
235                 return (
236                     (k == null ? 0 : k.hashCode())
237                         ^ (v == null ? 0 : v.hashCode()));
238             }
239
240             public String JavaDoc toString()
241             {
242                 return k + "=" + v;
243             }
244         }
245     }
246
247     public static List JavaDoc singletonList(Object JavaDoc o)
248     {
249         return new SingletonList(o);
250     }
251
252     private static class SingletonList
253         extends AbstractList JavaDoc
254         implements Serializable JavaDoc
255     {
256         private final Object JavaDoc element;
257
258         SingletonList(Object JavaDoc obj)
259         {
260             element = obj;
261         }
262
263         public int size()
264         {
265             return 1;
266         }
267
268         public boolean contains(Object JavaDoc obj)
269         {
270             return eq(obj, element);
271         }
272
273         public Object JavaDoc get(int index)
274         {
275             if (index != 0)
276                 throw new IndexOutOfBoundsException JavaDoc(
277                     "Index: " + index + ", Size: 1");
278             return element;
279         }
280     }
281
282     private static boolean eq(Object JavaDoc o1, Object JavaDoc o2)
283     {
284         return (o1 == null ? o2 == null : o1.equals(o2));
285     }
286 }
287
Popular Tags