org.geotools.util
Class FrequencySortedSet<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<E>
          extended by org.geotools.util.FrequencySortedSet<E>
Type Parameters:
E - The type of elements in the set.
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Comparator<E>, java.util.Set<E>, java.util.SortedSet<E>

public class FrequencySortedSet<E>
extends java.util.AbstractSet<E>
implements java.util.SortedSet<E>, java.util.Comparator<E>, java.io.Serializable

A set with elements ordered by the amount of time they were added. Less frequently added elements are first, and most frequently added ones are last. If some elements were added the same amount of time, then the iterator will traverse them in their insertion order.

An optional boolean argument in the constructor allows the construction of set in reversed order (most frequently added elements first, less frequently added last). This is similar but not identical to creating a defaut FrequencySortedSet and iterating through it in reverse order. The difference is that elements added the same amount of time will still be traversed in their insertion order.

This class is not thread-safe. Synchronizations (if wanted) are user's reponsability.

Since:
2.5
Version:
$Id: FrequencySortedSet.java 30792 2008-06-23 19:19:58Z desruisseaux $
Author:
Martin Desruisseaux
See Also:
Serialized Form

Constructor Summary
FrequencySortedSet()
          Creates an initially empty set with less frequent elements first.
FrequencySortedSet(boolean reversed)
          Creates an initially empty set with the default initial capacity.
FrequencySortedSet(int initialCapacity, boolean reversed)
          Creates an initially empty set with the specified initial capacity.
 
Method Summary
 boolean add(E element)
          Adds the specified element to this set.
 boolean add(E element, int occurence)
          Adds the specified element to this set.
 void clear()
          Removes all elements from this set.
 java.util.Comparator<E> comparator()
          Returns the comparator used to order the elements in this set.
 int compare(E o1, E o2)
          Compares the specified elements for frequency.
 boolean contains(java.lang.Object element)
          Returns true if this set contains the specified element.
 E first()
          Returns the first element in this set.
 int[] frequencies()
          Returns the frequency of each element in this set, in iteration order.
 int frequency(E element)
          Returns the frequency of the specified element in this set.
 java.util.SortedSet<E> headSet(E toElement)
           
 boolean isEmpty()
          Returns true if this set is empty.
 java.util.Iterator<E> iterator()
          Returns an iterator over the elements in this set in frequency order.
 E last()
          Returns the last element in this set.
 boolean remove(java.lang.Object element)
          Removes the specified element from this set, no matter how many time it has been added.
 int size()
          Returns the number of elements in this set.
 java.util.SortedSet<E> subSet(E fromElement, E toElement)
           
 java.util.SortedSet<E> tailSet(E fromElement)
           
 java.lang.Object[] toArray()
          Returns the content of this set as an array.
<T> T[]
toArray(T[] array)
          Returns the content of this set as an array.
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, containsAll, equals, hashCode, removeAll, retainAll
 
Methods inherited from interface java.util.Comparator
equals
 

Constructor Detail

FrequencySortedSet

public FrequencySortedSet()
Creates an initially empty set with less frequent elements first.


FrequencySortedSet

public FrequencySortedSet(boolean reversed)
Creates an initially empty set with the default initial capacity.

Parameters:
reversed - true if the elements should be sorted in reverse order (most frequent element first, less frequent last).

FrequencySortedSet

public FrequencySortedSet(int initialCapacity,
                          boolean reversed)
Creates an initially empty set with the specified initial capacity.

Parameters:
initialCapacity - The initial capacity.
reversed - true if the elements should be sorted in reverse order (most frequent element first, less frequent last).
Method Detail

size

public int size()
Returns the number of elements in this set.

Specified by:
size in interface java.util.Collection<E>
Specified by:
size in interface java.util.Set<E>
Specified by:
size in class java.util.AbstractCollection<E>

isEmpty

public boolean isEmpty()
Returns true if this set is empty.

Specified by:
isEmpty in interface java.util.Collection<E>
Specified by:
isEmpty in interface java.util.Set<E>
Overrides:
isEmpty in class java.util.AbstractCollection<E>

add

public boolean add(E element,
                   int occurence)
            throws java.lang.IllegalArgumentException
Adds the specified element to this set. Returns true if this set changed as a result of this operation. Changes in element order are not notified by the returned value.

Parameters:
element - The element to add.
occurence - The number of time to add the given elements. The default value is 1.
Returns:
true if this set changed as a result of this operation.
Throws:
java.lang.IllegalArgumentException - If occurence is negative.

add

public boolean add(E element)
Adds the specified element to this set. Returns true if this set changed as a result of this operation. Changes in element order are not notified by the returned value.

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.Set<E>
Overrides:
add in class java.util.AbstractCollection<E>
Parameters:
element - The element to add.
Returns:
true if this set changed as a result of this operation.

contains

public boolean contains(java.lang.Object element)
Returns true if this set contains the specified element.

Specified by:
contains in interface java.util.Collection<E>
Specified by:
contains in interface java.util.Set<E>
Overrides:
contains in class java.util.AbstractCollection<E>
Parameters:
element - The element whose presence in this set is to be tested.
Returns:
true if this set contains the specified element.

remove

public boolean remove(java.lang.Object element)
Removes the specified element from this set, no matter how many time it has been added. Returns true if this set changed as a result of this operation.

Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface java.util.Set<E>
Overrides:
remove in class java.util.AbstractCollection<E>
Parameters:
element - The element to remove.
Returns:
true if this set changed as a result of this operation.

clear

public void clear()
Removes all elements from this set.

Specified by:
clear in interface java.util.Collection<E>
Specified by:
clear in interface java.util.Set<E>
Overrides:
clear in class java.util.AbstractCollection<E>

iterator

public java.util.Iterator<E> iterator()
Returns an iterator over the elements in this set in frequency order.

Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface java.util.Collection<E>
Specified by:
iterator in interface java.util.Set<E>
Specified by:
iterator in class java.util.AbstractCollection<E>

headSet

public java.util.SortedSet<E> headSet(E toElement)
Specified by:
headSet in interface java.util.SortedSet<E>

tailSet

public java.util.SortedSet<E> tailSet(E fromElement)
Specified by:
tailSet in interface java.util.SortedSet<E>

subSet

public java.util.SortedSet<E> subSet(E fromElement,
                                     E toElement)
Specified by:
subSet in interface java.util.SortedSet<E>

first

public E first()
        throws java.util.NoSuchElementException
Returns the first element in this set.

Specified by:
first in interface java.util.SortedSet<E>
Throws:
java.util.NoSuchElementException - if this set is empty.

last

public E last()
       throws java.util.NoSuchElementException
Returns the last element in this set.

Specified by:
last in interface java.util.SortedSet<E>
Throws:
java.util.NoSuchElementException - if this set is empty.

comparator

public final java.util.Comparator<E> comparator()
Returns the comparator used to order the elements in this set. For a FrequencySortedSet, the comparator is always this.

This method is final because the FrequencySortedSet implementation makes assumptions on the comparator that would not hold if this method were overrided.

Specified by:
comparator in interface java.util.SortedSet<E>

compare

public final int compare(E o1,
                         E o2)
Compares the specified elements for frequency. For FrequencySortedSet with default ordering, this method returns a positive number if o1 has been added more frequently to this set than o2, a negative number if o1 has been added less frequently than o2, and 0 otherwise. For FrequencySortedSet with reverse ordering, this is the converse.

This method is final because the FrequencySortedSet implementation makes assumptions on the comparator that would not hold if this method were overrided.

Specified by:
compare in interface java.util.Comparator<E>

frequency

public int frequency(E element)
Returns the frequency of the specified element in this set.

Parameters:
element - The element whose frequency is to be obtained.
Returns:
The frequency of the given element, or 0 if it doesn't occur in this set.

frequencies

public int[] frequencies()
Returns the frequency of each element in this set, in iteration order.

Returns:
The frequency of each element in this set.

toArray

public java.lang.Object[] toArray()
Returns the content of this set as an array.

Specified by:
toArray in interface java.util.Collection<E>
Specified by:
toArray in interface java.util.Set<E>
Overrides:
toArray in class java.util.AbstractCollection<E>

toArray

public <T> T[] toArray(T[] array)
Returns the content of this set as an array.

Specified by:
toArray in interface java.util.Collection<E>
Specified by:
toArray in interface java.util.Set<E>
Overrides:
toArray in class java.util.AbstractCollection<E>
Type Parameters:
T - The type of the array elements.
Parameters:
array - The array where to copy the elements.
Returns:
The elements in the given array, or in a new array if the given array doesn't have a sufficient capacity.


Copyright © 1996-2010 Geotools. All Rights Reserved.