001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.desktop.util;
006:
007: import java.io.Serializable;
008:
009: import java.util.Collection;
010: import java.util.Set;
011: import java.util.List;
012: import java.util.ArrayList;
013: import java.util.Iterator;
014:
015: /**
016: * Simple wrapper class. All operations are delegated to an instance
017: * of ArrayList. However, unlike ArrayList, OrderedSet does not allow
018: * duplicate elements.
019: */
020: public class OrderedSet implements Set, Cloneable, Serializable {
021:
022: private List list = null;
023:
024: public OrderedSet() {
025: list = new ArrayList();
026: }
027:
028: public OrderedSet(Collection c) {
029: list = new ArrayList(c);
030: }
031:
032: public OrderedSet(int initialCapacity) {
033: list = new ArrayList(initialCapacity);
034: }
035:
036: public int size() {
037: return list.size();
038: }
039:
040: public boolean isEmpty() {
041: return list.isEmpty();
042: }
043:
044: public boolean contains(Object o) {
045: return list.contains(o);
046: }
047:
048: public Iterator iterator() {
049: return list.iterator();
050: }
051:
052: public Object[] toArray() {
053: return list.toArray();
054: }
055:
056: public Object[] toArray(Object[] objs) {
057: return list.toArray(objs);
058: }
059:
060: public boolean add(Object o) {
061: //
062: // no duplicates allowed
063: //
064: if (!contains(o)) {
065: return list.add(o);
066: } else {
067: return false;
068: }
069: }
070:
071: public boolean remove(Object o) {
072: return list.remove(o);
073: }
074:
075: public boolean containsAll(Collection c) {
076: return list.containsAll(c);
077: }
078:
079: public boolean addAll(Collection c) {
080: //
081: // no duplicates allowed
082: //
083: boolean changed = false;
084: for (Iterator i = c.iterator(); i.hasNext();) {
085: Object o = i.next();
086: if (!contains(o)) {
087: list.add(o);
088: changed = true;
089: }
090: }
091: return changed;
092: }
093:
094: public boolean retainAll(Collection c) {
095: return list.retainAll(c);
096: }
097:
098: public boolean removeAll(Collection c) {
099: return list.removeAll(c);
100: }
101:
102: public void clear() {
103: list.clear();
104: }
105:
106: public boolean equals(Object o) {
107: return list.equals(o);
108: }
109:
110: public int hashCode() {
111: return list.hashCode();
112: }
113:
114: public String toString() {
115: return list.toString();
116: }
117: }
|