01: package org.jacorb.notification.util;
02:
03: /*
04: * JacORB - a free Java ORB
05: *
06: * Copyright (C) 1999-2004 Gerald Brose
07: *
08: * This library is free software; you can redistribute it and/or
09: * 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;
25: import java.lang.reflect.Method;
26: import java.util.AbstractList;
27: import java.util.Collections;
28: import java.util.List;
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.2 2005/08/21 13:38:40 alphonse.bendt Exp $
38: */
39:
40: public class CollectionsWrapper {
41: private static Method singletonListMethod;
42:
43: static {
44: try {
45: singletonListMethod = Collections.class.getMethod(
46: "singletonList", new Class[] { Object.class });
47: } catch (Exception e) {
48: singletonListMethod = null;
49: }
50: }
51:
52: public static List singletonList(Object o) {
53: if (singletonListMethod != null) {
54: try {
55: return (List) (singletonListMethod.invoke(null,
56: new Object[] { o }));
57: } catch (Exception e) {
58: // ignore. return out implementation. should not happen.
59: }
60: }
61: return new SingletonList(o);
62: }
63:
64: private static class SingletonList extends AbstractList implements
65: Serializable {
66: private static final long serialVersionUID = 1L;
67: private final Object singletonElement_;
68:
69: SingletonList(Object element) {
70: singletonElement_ = element;
71: }
72:
73: public int size() {
74: return 1;
75: }
76:
77: public boolean contains(Object object) {
78: if (singletonElement_ == null) {
79: if (object == null) {
80: return true;
81: }
82: return false;
83: }
84:
85: return object.equals(singletonElement_);
86: }
87:
88: public Object get(int index) {
89: if (index != 0) {
90: throw new IndexOutOfBoundsException("Index: " + index);
91: }
92:
93: return singletonElement_;
94: }
95: }
96: }
|