KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > util > ReferenceList


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30 package com.nightlabs.util;
31
32 import java.lang.ref.Reference JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 public class ReferenceList extends ArrayList JavaDoc
36 {
37
38   public ReferenceList()
39   {
40   }
41
42   public void add(int index, Object JavaDoc reference)
43   {
44     if (reference == null)
45       throw new NullPointerException JavaDoc("Param reference must not be null!");
46
47     if (!(reference instanceof Reference JavaDoc))
48       throw new IllegalArgumentException JavaDoc("Can only add descendants of Reference!");
49
50     super.add(index, reference);
51   }
52
53   public boolean add(Object JavaDoc reference)
54   {
55     if (reference == null)
56       throw new NullPointerException JavaDoc("Param reference must not be null!");
57
58     if (!(reference instanceof Reference JavaDoc))
59       throw new IllegalArgumentException JavaDoc("Can only add descendants of Reference!");
60
61     return super.add(reference);
62   }
63
64   protected static boolean referenceEquals(Reference JavaDoc ref, Object JavaDoc other)
65   {
66     if (ref == null)
67       throw new NullPointerException JavaDoc("Param ref must not be null!");
68
69     if (other instanceof Reference JavaDoc) {
70       Reference JavaDoc otherRef = (Reference JavaDoc)other;
71
72       if (ref.get() == null)
73         return otherRef.get() == null;
74
75       return ref.get().equals(otherRef.get());
76     } // if (other instanceof Reference) {
77

78     if (ref.get() == null)
79       return other == null;
80
81     return ref.get().equals(other);
82   }
83
84   public int indexOf(Object JavaDoc elem)
85   {
86     return indexOf(elem, 0);
87   }
88
89   public int indexOf(Object JavaDoc elem, int index)
90   {
91     if (elem == null)
92       throw new NullPointerException JavaDoc("Param elem must not be null!");
93
94     for (int i = index; i < this.size(); ++i) {
95       if (referenceEquals((Reference JavaDoc)this.get(i), elem))
96         return i;
97     } // for (int i = index; i < elementData.length; ++i) {
98

99     return -1;
100   }
101
102
103   public int lastIndexOf(Object JavaDoc elem) {
104     return lastIndexOf(elem, this.size()-1);
105   }
106
107   public int lastIndexOf(Object JavaDoc elem, int index) {
108     if (elem == null)
109       throw new NullPointerException JavaDoc("Param elem must not be null!");
110
111     for (int i = index; i >= 0; --i) {
112       if (referenceEquals((Reference JavaDoc)this.get(i), elem))
113         return i;
114     } // for (int i = index; i < elementData.length; ++i) {
115

116     return -1;
117   }
118
119   public boolean remove(Object JavaDoc o) {
120     return super.remove(o);
121   }
122
123 // public boolean contains(Object obj)
124
// {
125
// super.contains(obj);
126
// for (int i = size()-1; i >=0; --i) {
127
// Object o = get(i);
128
// if (o == null && obj == null)
129
// remove(i);
130
// else {
131
// if (o.equals(obj))
132
// return true;
133
// } // if (o != null) {
134
// } // for (int i = size()-1; i >=0; --i) {
135
//
136
// return false;
137
// }
138
//
139
// public boolean remove(Object obj)
140
// {
141
// return false;
142
// }
143

144 }
145
Popular Tags