org.ceryle.util
Class SetModel

java.lang.Object
  extended by javax.swing.AbstractListModel
      extended by javax.swing.DefaultComboBoxModel
          extended by org.ceryle.util.SetModel
All Implemented Interfaces:
Serializable, Iterable, Collection, List, Set, ComboBoxModel, ListModel, MutableComboBoxModel
Direct Known Subclasses:
SelectionManager

public class SetModel
extends DefaultComboBoxModel
implements Set, List, ListModel, MutableComboBoxModel, Serializable

Extends the DefaultComboBoxModel modified to treat its store as a Set (ie., checking uniqueness of entries upon adding) insuring that items in the list are unique. While logically this is a Set and is located in the Utilities package, functionally it is also a List, and is generally used to build ComboBoxes and in other GUI applications. This eliminates all of the fussy checking over values typical of Lists and JComboBoxes, and can be used in most places where a DefaultListModel is used as well, since it implements almost everything in List except the two ListIterator-related and the subList() methods, which will here throw UnsupportedOperationExceptions.

Legacy Note

Prior to May 2007 this class extended DefaultComboBoxModel by maintaining its own internal Vector, but this had some serious problems. We've instead dropped support for the more subList feature since we weren't using it anyway, but there may be a few remaining issues lurking due to that change that haven't yet surfaced, most likely as regards listeners and event firings.

Since:
JDK1.2
Version:
$Id: SetModel.java,v 3.10 2007-06-20 04:41:47 altheim Exp $
Author:
Murray Altheim
See Also:
DefaultComboBoxModel, DefaultListModel, Serialized Form

Field Summary
static org.ceryle.util.SetModel.EmptySetModel EMPTY_SET
          The empty set (immutable).
protected  int modCount
          The number of times this list has been structurally modified.
 
Fields inherited from class javax.swing.AbstractListModel
listenerList
 
Constructor Summary
SetModel()
          Constructs an empty SetModel object.
SetModel(Collection collection)
          Constructs a SetModel object initialized with a Collection.
SetModel(Iterator iterator)
          Constructs a SetModel object initialized with an Iterator.
SetModel(ListModel items)
          Constructs a SetModel object initialized with a ListModel.
SetModel(Object[] items)
          Constructs a SetModel object initialized with an array of Objects.
SetModel(Vector vector)
          Constructs a SetModel object initialized with a Vector.
 
Method Summary
 void add(int index, Object o)
          A synonym for DefaultComboBoxModel.insertElementAt(Object,int).
 boolean add(Object o)
          Adds the Object o to the end of this SetModel, returning true if the addition was successful.
 boolean addAll(Collection collection)
          Adds all unique elements of the Collection collection to this SetModel, returning true if any were successfully added.
 boolean addAll(int index, Collection collection)
          Adds all unique elements of the Collection collection to this SetModel starting at position index, returning true if all were successfully added.
 boolean addAll(Object[] array)
          Adds all unique elements of the Object array array to this SetModel, returning true if any were successfully added.
 void addElement(Object o)
          Implements javax.swing.MutableComboBoxModel.
 void addElements(Collection collection)
          Adds all unique elements of the Collection collection to this SetModel.
 void changedElementAt(int index)
          Notifies any listeners that the element at index has changed.
 void clear()
          Removes all the elements of this SetModel.
 boolean contains(Object o)
          Returns true if this SetModel contains the specified element.
 boolean containsAll(Collection c)
          Returns true if this SetModel contains all of the elements of the provided Collection.
 Object get(int index)
          Implements javax.swing.AbstractList.
 Object getFirstElement()
          Returns the first Element of the model, null if empty.
 Object getLastElement()
          Returns the last Element of the model, null if empty.
 Object getSelectedItem()
          Implements javax.swing.ComboBoxModel.
 int indexOf(Object o)
          Returns the index of Object o within the List.
 boolean isEmpty()
          Returns true if the contents of this SetModel are empty.
 Iterator iterator()
          Returns an iterator over the contents of this SetModel.
 int lastIndexOf(Object o)
          Returns the last index of Object o within the List.
 ListIterator listIterator()
          Returns a ListIterator over the list in proper sequence.
 ListIterator listIterator(int index)
          Returns a ListIterator over the list in proper sequence, starting at the specified position in the List.
 boolean moveToBeginning(Object o)
          Moves the Object o to the first element position in the model.
 Object remove(int index)
          Implementation method of java.util.List.
 boolean remove(Object o)
          Removes the Object from the list.
 boolean removeAll(Collection c)
          Removes all the elements of this SetModel that are contained within the provided Collection.
 boolean retainAll(Collection c)
          Retains only the elements in this SetModel that are contained within the provided Collection.
 Object set(int index, Object o)
          Replaces the element at index with Object o.
 void setSelectedItem(Object o)
          Implements javax.swing.ComboBoxModel.
 int size()
          Implements javax.swing.ListModel.
 void sort()
          Sorts the values of the Set.
 List subList(int fromIndex, int toIndex)
          Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.
 Object[] toArray()
          Returns an array containing all the elements in their proper sequence.
 Object[] toArray(Object[] a)
          Returns an array containing all the elements in their proper sequence, with the runtime type of the returned array that of the provided array.
 
Methods inherited from class javax.swing.DefaultComboBoxModel
getElementAt, getIndexOf, getSize, insertElementAt, removeAllElements, removeElement, removeElementAt
 
Methods inherited from class javax.swing.AbstractListModel
addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Set
equals, hashCode
 
Methods inherited from interface java.util.List
equals, hashCode
 
Methods inherited from interface javax.swing.MutableComboBoxModel
insertElementAt, removeElement, removeElementAt
 

Field Detail

EMPTY_SET

public static final org.ceryle.util.SetModel.EmptySetModel EMPTY_SET
The empty set (immutable).


modCount

protected transient int modCount
The number of times this list has been structurally modified. Structural modifications are those that change the size of the list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.

See Also:
AbstractList.modCount
Constructor Detail

SetModel

public SetModel()
Constructs an empty SetModel object.


SetModel

public SetModel(Object[] items)
Constructs a SetModel object initialized with an array of Objects.

Parameters:
items - an array of Objects

SetModel

public SetModel(ListModel items)
Constructs a SetModel object initialized with a ListModel.

Parameters:
items - the ListModel containing the list of items

SetModel

public SetModel(Vector vector)
Constructs a SetModel object initialized with a Vector. Redundant Objects in the Vector will not be retained.

Parameters:
vector - the Vector object

SetModel

public SetModel(Collection collection)
Constructs a SetModel object initialized with a Collection. Redundant Objects in the Collection will not be retained.

Parameters:
collection - the Collection object

SetModel

public SetModel(Iterator iterator)
Constructs a SetModel object initialized with an Iterator. Redundant Objects returned by the Iterator will not be duplicated in the resultant SetModel.

Parameters:
iterator - the Iterator object
Method Detail

setSelectedItem

public void setSelectedItem(Object o)
Implements javax.swing.ComboBoxModel. If the Object is not currently contained within the SetModel, nothing happens.

Specified by:
setSelectedItem in interface ComboBoxModel
Overrides:
setSelectedItem in class DefaultComboBoxModel

getSelectedItem

public Object getSelectedItem()
Implements javax.swing.ComboBoxModel.

Specified by:
getSelectedItem in interface ComboBoxModel
Overrides:
getSelectedItem in class DefaultComboBoxModel

size

public int size()
Implements javax.swing.ListModel.

Specified by:
size in interface Collection
Specified by:
size in interface List
Specified by:
size in interface Set

changedElementAt

public void changedElementAt(int index)
Notifies any listeners that the element at index has changed.


getFirstElement

public Object getFirstElement()
Returns the first Element of the model, null if empty.


getLastElement

public Object getLastElement()
Returns the last Element of the model, null if empty.


moveToBeginning

public boolean moveToBeginning(Object o)
Moves the Object o to the first element position in the model. If the model doesn't already contain the Object, it is added. Returns true if the object was new to the list. If the item was not in the list previously, a message is fired to the listener.


get

public Object get(int index)
Implements javax.swing.AbstractList.

Specified by:
get in interface List

add

public boolean add(Object o)
Adds the Object o to the end of this SetModel, returning true if the addition was successful. If the object already exists in the set, nothing happens, returns false. This is identical to the method addElement(Object) except for the return type.

Specified by:
add in interface Collection
Specified by:
add in interface List
Specified by:
add in interface Set

addElement

public void addElement(Object o)
Implements javax.swing.MutableComboBoxModel. If the object already exists in the Set, nothing happens.

Specified by:
addElement in interface MutableComboBoxModel
Overrides:
addElement in class DefaultComboBoxModel

addAll

public boolean addAll(Collection collection)
Adds all unique elements of the Collection collection to this SetModel, returning true if any were successfully added. If an object already exists in the Set, it is not added again (and the method returns false, even if some were added). If the provided collection is empty, returns true. This is identical to the method addElements(Collection) except for the return type.

Specified by:
addAll in interface Collection
Specified by:
addAll in interface List
Specified by:
addAll in interface Set

addAll

public boolean addAll(Object[] array)
Adds all unique elements of the Object array array to this SetModel, returning true if any were successfully added. If an object already exists in the Set, it is not added again (and the method returns false, even if some were added). If the provided array is empty, returns true.


addAll

public boolean addAll(int index,
                      Collection collection)
Adds all unique elements of the Collection collection to this SetModel starting at position index, returning true if all were successfully added. If an object already exists in the Set, it is not added again (and the method returns false, even if some were added). If the provided collection is empty, returns true.

Current unimplemented.

Specified by:
addAll in interface List

addElements

public void addElements(Collection collection)
Adds all unique elements of the Collection collection to this SetModel. If an object already exists in the Set, it is not added again.


set

public Object set(int index,
                  Object o)
Replaces the element at index with Object o. This will fire both a removed and insert message. If the SetModel already contains the Object, nothing happens and the parameter is returned. Implements java.util.List.

Specified by:
set in interface List

add

public void add(int index,
                Object o)
A synonym for DefaultComboBoxModel.insertElementAt(Object,int). If the SetModel already contains the Object, nothing happens. Implements java.util.List.

Specified by:
add in interface List

indexOf

public int indexOf(Object o)
Returns the index of Object o within the List. Implements java.util.List.

Specified by:
indexOf in interface List

lastIndexOf

public int lastIndexOf(Object o)
Returns the last index of Object o within the List. Implements java.util.List.

Specified by:
lastIndexOf in interface List

listIterator

public ListIterator listIterator()
Returns a ListIterator over the list in proper sequence. Implements java.util.List.

Specified by:
listIterator in interface List
See Also:
ListIterator

listIterator

public ListIterator listIterator(int index)
Returns a ListIterator over the list in proper sequence, starting at the specified position in the List. The specified index indicates the first element returned by a call to ListIterator.next(). Implements java.util.List.

Specified by:
listIterator in interface List
See Also:
ListIterator

subList

public List subList(int fromIndex,
                    int toIndex)
Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned List is empty.) The returned List is backed by this List, so changes in the returned List are reflected in this List, and vice-versa. The returned List supports all of the optional List operations supported by this List. Implements java.util.List.

Specified by:
subList in interface List

remove

public boolean remove(Object o)
Removes the Object from the list. If the element does not exist in the SetModel, nothing happens. Implements java.util.List.

Specified by:
remove in interface Collection
Specified by:
remove in interface List
Specified by:
remove in interface Set

remove

public Object remove(int index)
Implementation method of java.util.List.

Specified by:
remove in interface List
Returns:
the removed Object

iterator

public Iterator iterator()
Returns an iterator over the contents of this SetModel.

Specified by:
iterator in interface Iterable
Specified by:
iterator in interface Collection
Specified by:
iterator in interface List
Specified by:
iterator in interface Set

isEmpty

public boolean isEmpty()
Returns true if the contents of this SetModel are empty.

Specified by:
isEmpty in interface Collection
Specified by:
isEmpty in interface List
Specified by:
isEmpty in interface Set

contains

public boolean contains(Object o)
Returns true if this SetModel contains the specified element. More formally, returns true if and only if this SetModel contains at least one element e such that (o==null ? e==null : o.equals(e)).

This implementation iterates over the elements in the SetModel, checking each element in turn for equality with the specified element.

Specified by:
contains in interface Collection
Specified by:
contains in interface List
Specified by:
contains in interface Set
Parameters:
o - the Object to be checked for containment in this SetModel.
Returns:
true if this SetModel contains the specified element.

containsAll

public boolean containsAll(Collection c)
Returns true if this SetModel contains all of the elements of the provided Collection.

Specified by:
containsAll in interface Collection
Specified by:
containsAll in interface List
Specified by:
containsAll in interface Set

retainAll

public boolean retainAll(Collection c)
Retains only the elements in this SetModel that are contained within the provided Collection.

Specified by:
retainAll in interface Collection
Specified by:
retainAll in interface List
Specified by:
retainAll in interface Set
Returns:
true if this SetModel contained all of the elements within the provided Collection

removeAll

public boolean removeAll(Collection c)
Removes all the elements of this SetModel that are contained within the provided Collection.

Specified by:
removeAll in interface Collection
Specified by:
removeAll in interface List
Specified by:
removeAll in interface Set
Returns:
true if this SetModel contained all of the elements within the provided Collection

clear

public void clear()
Removes all the elements of this SetModel. This is a synonym for DefaultComboBoxModel.removeAllElements().

Specified by:
clear in interface Collection
Specified by:
clear in interface List
Specified by:
clear in interface Set

sort

public void sort()
Sorts the values of the Set.


toArray

public Object[] toArray()
Returns an array containing all the elements in their proper sequence.

Specified by:
toArray in interface Collection
Specified by:
toArray in interface List
Specified by:
toArray in interface Set

toArray

public Object[] toArray(Object[] a)
Returns an array containing all the elements in their proper sequence, with the runtime type of the returned array that of the provided array.

Specified by:
toArray in interface Collection
Specified by:
toArray in interface List
Specified by:
toArray in interface Set


The Ceryle Project. Copyright ©2001-2007 Murray Altheim, All Rights Reserved. See LICENSE included with distribution.