KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > util > CollectionsWrapper


1 package org.jacorb.notification.util;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import java.io.Serializable JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.util.AbstractList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * provides a simple wrapper around java.util.Collections. Notification Service uses the Method
32  * Collections.singletonList. This method is not available in a pre 1.3 JDK.
33  *
34  * @author Alphonse Bendt
35  * @author Marc Heide
36  *
37  * @version $Id: CollectionsWrapper.java,v 1.1 2005/02/14 00:13:05 alphonse.bendt Exp $
38  */

39
40 public class CollectionsWrapper
41 {
42     private static Method JavaDoc singletonListMethod;
43
44     static
45     {
46         try
47         {
48             singletonListMethod = Collections JavaDoc.class.getMethod("singletonList",
49                     new Class JavaDoc[] { Object JavaDoc.class });
50         } catch (Exception JavaDoc e)
51         {
52             singletonListMethod = null;
53         }
54     }
55
56     public static List JavaDoc singletonList(Object JavaDoc o)
57     {
58         if (singletonListMethod != null)
59         {
60             try
61             {
62                 return (List JavaDoc) (singletonListMethod.invoke(null, new Object JavaDoc[] { o }));
63             } catch (Exception JavaDoc e)
64             {
65                 // ignore. return out implementation. should not happen.
66
}
67         }
68         return new SingletonList(o);
69     }
70
71     private static class SingletonList extends AbstractList JavaDoc implements Serializable JavaDoc
72     {
73         private final Object JavaDoc singletonElement_;
74
75         SingletonList(Object JavaDoc element)
76         {
77             singletonElement_ = element;
78         }
79
80         public int size()
81         {
82             return 1;
83         }
84
85         public boolean contains(Object JavaDoc object)
86         {
87             if (singletonElement_ == null)
88             {
89                 if (object == null)
90                 {
91                     return true;
92                 }
93                 return false;
94             }
95
96             return object.equals(singletonElement_);
97         }
98
99         public Object JavaDoc get(int index)
100         {
101             if (index != 0)
102             {
103                 throw new IndexOutOfBoundsException JavaDoc("Index: " + index);
104             }
105
106             return singletonElement_;
107         }
108     }
109 }
Popular Tags