KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: OrderedSet.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 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.util.AbstractSet JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31
32 /**
33  * OrderedSet - Set interface wrapper around a LinkedList
34  *
35  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
36  * @version $Rev: 5462 $
37  * @since 2.0
38  */

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

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

58     public OrderedSet(Collection JavaDoc c) {
59         Iterator JavaDoc i = c.iterator();
60
61         while (i.hasNext())
62             add(i.next());
63     }
64
65     /**
66      * @see java.util.Collection#iterator()
67      */

68     public Iterator JavaDoc iterator() {
69         return backedList.iterator();
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             return backedList.add(obj);
87         else {
88             backedList.set(index, obj);
89             return false;
90         }
91     }
92 }
93
Popular Tags