KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > collections > LifoSet


1 /*
2  * $Id: LifoSet.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util.collections;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.AbstractSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.EmptyStackException JavaDoc;
31
32 /**
33  * LifoSet - Set interface wrapper around a LinkedList
34  *
35  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
36  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
37  * @version $Rev: 5462 $
38  * @since 3.1
39  */

40 public class LifoSet extends AbstractSet JavaDoc implements Serializable JavaDoc {
41
42     // This set's back LinkedList
43
private LinkedList JavaDoc backedList = new LinkedList JavaDoc();
44     private int maxCapacity = 10;
45
46     /**
47      * Constructs a set containing the elements of the specified
48      * collection, in the order they are returned by the collection's
49      * iterator.
50      */

51     public LifoSet() {}
52
53     /**
54      * Constructs a set containing the elements of the specified
55      * collection, in the order they are returned by the collection's
56      * iterator.
57      *
58      * @param capacity the collection whose elements are to be placed into this set.
59      */

60     public LifoSet(int capacity) {
61         maxCapacity = capacity;
62     }
63
64     /**
65      * Sets the max capacity for this LifoSet
66      * @param capacity Max Size (as integer)
67      */

68     public void setCapactity(int capacity) {
69         this.maxCapacity = capacity;
70     }
71
72     /**
73      * @see java.util.Collection#size()
74      */

75     public int size() {
76         return backedList.size();
77     }
78
79     /**
80      * @see java.util.Collection#add(java.lang.Object)
81      */

82     public boolean add(Object JavaDoc obj) {
83         int index = backedList.indexOf(obj);
84
85         if (index == -1) {
86             backedList.addFirst(obj);
87             while (size() > maxCapacity)
88                 backedList.removeLast();
89         } else {
90             backedList.remove(index);
91             backedList.addFirst(obj);
92         }
93         return true;
94     }
95
96     /**
97      * @see java.util.Collection#iterator()
98      */

99     public Iterator JavaDoc iterator() {
100         return backedList.iterator();
101     }
102
103     // Stack Implementation (implements all Stack methods as per the java.util.Stack object
104

105     /**
106      * @see java.util.Stack#empty()
107      *
108      * @return true if and only if this stack contains no items; false otherwise
109      */

110     public boolean empty() {
111         if (this.size() == 0) {
112             return true;
113         }
114         return false;
115     }
116
117     /**
118      * @see java.util.Stack#push(java.lang.Object)
119      *
120      * @param item The item to be pushed onto this stack
121      */

122     public void push(Object JavaDoc item) {
123         this.add(item);
124     }
125
126     /**
127      * @see java.util.Stack#pop()
128      *
129      * @return The object at the top of this stack
130      * @throws EmptyStackException If this stack is empty
131      */

132     public Object JavaDoc pop() throws EmptyStackException JavaDoc {
133         if (this.size() > 0) {
134             return backedList.removeFirst();
135         }
136         throw new EmptyStackException JavaDoc();
137     }
138
139     /**
140      * @see java.util.Stack#peek()
141      *
142      * @return The object at the top of this stack
143      * @throws EmptyStackException If this stack is empty
144      */

145     public Object JavaDoc peek() throws EmptyStackException JavaDoc {
146         if (this.size() > 0) {
147             return backedList.getFirst();
148         }
149         throw new EmptyStackException JavaDoc();
150     }
151
152     /**
153      * @see java.util.Stack#search(java.lang.Object)
154      *
155      * @param item The desired object
156      * @return The 1-based position from the top of the stack where the object is located;
157      * the return value -1 indicates that the object is not on the stack
158      */

159     public int search(Object JavaDoc item) {
160         int index = backedList.indexOf(item);
161         if (index > -1) {
162             return index + 1; // this method is 1 based (per java.util.Stack)
163
}
164         return -1;
165     }
166 }
167
168
Popular Tags