01: /*
02: * $Id: OrderedSet.java,v 1.1 2003/08/15 20:23:20 ajzeneski Exp $
03: *
04: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
05: *
06: * Permission is hereby granted, free of charge, to any person obtaining a
07: * copy of this software and associated documentation files (the "Software"),
08: * to deal in the Software without restriction, including without limitation
09: * 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;
25:
26: import java.util.AbstractSet;
27: import java.util.Collection;
28: import java.util.Iterator;
29: import java.util.LinkedList;
30: import java.util.List;
31:
32: /**
33: * OrderedSet - Set interface wrapper around a LinkedList
34: *
35: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
36: * @version $Revision: 1.1 $
37: * @since 2.0
38: */
39: public class OrderedSet extends AbstractSet {
40:
41: // This set's back LinkedList
42: private List backedList = new LinkedList();
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: /**
53: * Constructs a set containing the elements of the specified
54: * collection, in the order they are returned by the collection's
55: * iterator.
56: *
57: * @param c the collection whose elements are to be placed into this set.
58: */
59: public OrderedSet(Collection c) {
60: Iterator i = c.iterator();
61:
62: while (i.hasNext())
63: add(i.next());
64: }
65:
66: /**
67: * @see java.util.Collection#iterator()
68: */
69: public Iterator iterator() {
70: return backedList.iterator();
71: }
72:
73: /**
74: * @see java.util.Collection#size()
75: */
76: public int size() {
77: return backedList.size();
78: }
79:
80: /**
81: * @see java.util.Collection#add(java.lang.Object)
82: */
83: public boolean add(Object obj) {
84: int index = backedList.indexOf(obj);
85:
86: if (index == -1)
87: return backedList.add(obj);
88: else {
89: backedList.set(index, obj);
90: return false;
91: }
92: }
93: }
|