KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > LifoSet


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 import java.util.AbstractSet JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 /**
37  * Stack (lifo) ordered set, used by the JCA code so recent
38  * connections are used first.
39  */

40 public class LifoSet<E> extends AbstractSet JavaDoc<E> {
41   private final ArrayList JavaDoc<E> _list = new ArrayList JavaDoc<E>();
42
43   /**
44    * Returns the number of elements in the set.
45    */

46   public int size()
47   {
48     return _list.size();
49   }
50
51   /**
52    * Returns true if empty.
53    */

54   public boolean isEmpty()
55   {
56     return _list.isEmpty();
57   }
58
59   /**
60    * Adds an element.
61    */

62   public boolean add(E o)
63   {
64     if (! _list.contains(o)) {
65       _list.add(0, o);
66       return true;
67     }
68     else
69       return false;
70   }
71
72   /**
73    * Clears the set.
74    */

75   public void clear()
76   {
77     _list.clear();
78   }
79
80   /**
81    * Returns true if the item is in the set.
82    */

83   public boolean contains(Object JavaDoc o)
84   {
85     return _list.contains(o);
86   }
87
88   /**
89    * Returns true if the item is in the set.
90    */

91   public boolean containsAll(Collection JavaDoc<?> c)
92   {
93     return _list.containsAll(c);
94   }
95
96   /**
97    * Returns an iterator to the set.
98    */

99   public Iterator JavaDoc<E> iterator()
100   {
101     return _list.iterator();
102   }
103
104   /**
105    * Removes an element of the set.
106    */

107   public boolean remove(Object JavaDoc o)
108   {
109     return _list.remove(o);
110   }
111
112   /**
113    * Removes an element of the set.
114    */

115   public boolean removeAll(Collection JavaDoc<?> c)
116   {
117     return _list.removeAll(c);
118   }
119
120   /**
121    * Removes an element of the set.
122    */

123   public boolean retainAll(Collection JavaDoc<?> c)
124   {
125     return _list.retainAll(c);
126   }
127
128   /**
129    * Returns an array of the elements in the set.
130    */

131   public Object JavaDoc []toArray()
132   {
133     return _list.toArray();
134   }
135
136   /**
137    * Returns an array of the elements in the set.
138    */

139   public <T> T[] toArray(T[] a)
140   {
141     return _list.toArray(a);
142   }
143
144   /**
145    * Returns the hash code.
146    */

147   public int hashCode()
148   {
149     return _list.hashCode();
150   }
151
152   /**
153    * Test for equality
154    */

155   public boolean equals(Object JavaDoc o)
156   {
157     if (this == o)
158       return true;
159     else if (! (o instanceof LifoSet))
160       return false;
161
162     LifoSet set = (LifoSet) o;
163
164     return _list.equals(set._list);
165   }
166 }
167
Popular Tags