1 30 package com.nightlabs.util; 31 32 import java.lang.ref.Reference ; 33 import java.util.ArrayList ; 34 35 public class ReferenceList extends ArrayList  36 { 37 38 public ReferenceList() 39 { 40 } 41 42 public void add(int index, Object reference) 43 { 44 if (reference == null) 45 throw new NullPointerException ("Param reference must not be null!"); 46 47 if (!(reference instanceof Reference )) 48 throw new IllegalArgumentException ("Can only add descendants of Reference!"); 49 50 super.add(index, reference); 51 } 52 53 public boolean add(Object reference) 54 { 55 if (reference == null) 56 throw new NullPointerException ("Param reference must not be null!"); 57 58 if (!(reference instanceof Reference )) 59 throw new IllegalArgumentException ("Can only add descendants of Reference!"); 60 61 return super.add(reference); 62 } 63 64 protected static boolean referenceEquals(Reference ref, Object other) 65 { 66 if (ref == null) 67 throw new NullPointerException ("Param ref must not be null!"); 68 69 if (other instanceof Reference ) { 70 Reference otherRef = (Reference )other; 71 72 if (ref.get() == null) 73 return otherRef.get() == null; 74 75 return ref.get().equals(otherRef.get()); 76 } 78 if (ref.get() == null) 79 return other == null; 80 81 return ref.get().equals(other); 82 } 83 84 public int indexOf(Object elem) 85 { 86 return indexOf(elem, 0); 87 } 88 89 public int indexOf(Object elem, int index) 90 { 91 if (elem == null) 92 throw new NullPointerException ("Param elem must not be null!"); 93 94 for (int i = index; i < this.size(); ++i) { 95 if (referenceEquals((Reference )this.get(i), elem)) 96 return i; 97 } 99 return -1; 100 } 101 102 103 public int lastIndexOf(Object elem) { 104 return lastIndexOf(elem, this.size()-1); 105 } 106 107 public int lastIndexOf(Object elem, int index) { 108 if (elem == null) 109 throw new NullPointerException ("Param elem must not be null!"); 110 111 for (int i = index; i >= 0; --i) { 112 if (referenceEquals((Reference )this.get(i), elem)) 113 return i; 114 } 116 return -1; 117 } 118 119 public boolean remove(Object o) { 120 return super.remove(o); 121 } 122 123 144 } 145
| Popular Tags
|