1 package org.jacorb.notification.util; 2 3 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 39 40 public class CollectionsWrapper 41 { 42 private static Method singletonListMethod; 43 44 static 45 { 46 try 47 { 48 singletonListMethod = Collections .class.getMethod("singletonList", 49 new Class [] { Object .class }); 50 } catch (Exception e) 51 { 52 singletonListMethod = null; 53 } 54 } 55 56 public static List singletonList(Object o) 57 { 58 if (singletonListMethod != null) 59 { 60 try 61 { 62 return (List ) (singletonListMethod.invoke(null, new Object [] { o })); 63 } catch (Exception e) 64 { 65 } 67 } 68 return new SingletonList(o); 69 } 70 71 private static class SingletonList extends AbstractList implements Serializable 72 { 73 private final Object singletonElement_; 74 75 SingletonList(Object element) 76 { 77 singletonElement_ = element; 78 } 79 80 public int size() 81 { 82 return 1; 83 } 84 85 public boolean contains(Object 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 get(int index) 100 { 101 if (index != 0) 102 { 103 throw new IndexOutOfBoundsException ("Index: " + index); 104 } 105 106 return singletonElement_; 107 } 108 } 109 } | Popular Tags |