fcmodeler.jsomap
Class WeightedSet

java.lang.Object
  |
  +--java.util.AbstractCollection
        |
        +--java.util.AbstractSet
              |
              +--fcmodeler.jsomap.WeightedSet
All Implemented Interfaces:
java.util.Collection, java.util.Set

public class WeightedSet
extends java.util.AbstractSet

A set in which each element has a corresponding real-valued weight. This class implements the java.util.Set interface and supports all of the normal set operations. Additionally, each element in this set has a corresponding weight, of type double. The weights are obtained using the get method, and altered using the set method. Adding an element(s) to this set using the add or addAll methods adds the element(s) and assignes a weight of 1.0d to it/them. Additionally, if the collection passed to the addAll method is of type WeightedSet, that set is effectively copied into this set, including its weights.

Version:
 
Author:
default

Constructor Summary
WeightedSet()
          Creates a new empty WeightedSet.
WeightedSet(java.util.Collection collection)
          Creates a new WeightedSet containing the elements in the specified collection.
 
Method Summary
 boolean add(java.lang.Object obj)
          Adds the specified object to this set if it is not already present and assigns it a weight of 1.0d.
 boolean addAll(java.util.Collection collection)
          Adds all of the elements in the specified collection to this set if they're not already present.
 void clear()
          Removes all elements from this set.
 boolean contains(java.lang.Object obj)
          Returns true if this set contains the specified object.
 boolean equals(java.lang.Object obj)
          Compares the specified object with this set for equality.
 double get(java.lang.Object obj)
          Returns the weight associated with the specified object.
 int hashCode()
          Returns the hash code value for this set.
 java.util.Iterator iterator()
          Returns an iterator over the elements in this set.
 boolean remove(java.lang.Object obj)
          Removes the specified element from this set if it is present.
 void set(java.lang.Object obj, double weight)
          Sets the weight of the specified object to the specified value.
 int size()
          Returns the number of elements in this set.
 java.lang.Object[] toArray()
          Returns an array containing all of the elements in this set.
 java.lang.Object[] toArray(java.lang.Object[] obj)
          Returns an array containing all of the elements in this set whose runtime type is that of the specified array.
 java.lang.String toString()
          Returns the string representation of this set.
 
Methods inherited from class java.util.AbstractSet
removeAll
 
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, retainAll
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
containsAll, isEmpty, retainAll
 

Constructor Detail

WeightedSet

public WeightedSet()
Creates a new empty WeightedSet.

WeightedSet

public WeightedSet(java.util.Collection collection)
Creates a new WeightedSet containing the elements in the specified collection. If the collection is of type WeightedSet, all elements are assigned to their corresponding weights; otherwise, all elements are assigned a weight of 1.0d.
Parameters:
collection - the collection whose elements are to be placed in this set.
Method Detail

add

public boolean add(java.lang.Object obj)
Adds the specified object to this set if it is not already present and assigns it a weight of 1.0d.
Overrides:
add in class java.util.AbstractCollection
Parameters:
obj - the object to add to this set.
Returns:
true if this set did not already contain the object.

addAll

public boolean addAll(java.util.Collection collection)
Adds all of the elements in the specified collection to this set if they're not already present. If the collection is of type WeightedSet the elements are assigned their corresponding weights; if not, they are assigned the weight 1.0d.
Overrides:
addAll in class java.util.AbstractCollection
Parameters:
collection - the collection whose elements are to be added to this set.
Returns:
true if this set changed as a result of this call.

clear

public void clear()
Removes all elements from this set.
Overrides:
clear in class java.util.AbstractCollection

contains

public boolean contains(java.lang.Object obj)
Returns true if this set contains the specified object.
Overrides:
contains in class java.util.AbstractCollection
Parameters:
obj - the object to test for containment.
Returns:
true if this set contains the specified object.

equals

public boolean equals(java.lang.Object obj)
Compares the specified object with this set for equality. Returns true if the specified object is also a weighted set, the two sets have the same size, and every member of the specified set is contained in this set and has the same weight.
Overrides:
equals in class java.util.AbstractSet
Parameters:
obj - the object to be compared for equality with this set.
Returns:
true if the specified object is equal to this set.

get

public double get(java.lang.Object obj)
Returns the weight associated with the specified object. Returns the value 0.0d if the specified object is not contained in this set.
Parameters:
obj - the object whose weight is returned.
Returns:
the weight of the specified object.

hashCode

public int hashCode()

Returns the hash code value for this set. The hash code of a weighted set is defined to be the sum of the hash codes of each element and it's associated weight. Specifically, the hash code of each element and its weight are combined as follows:

 long bits = Double.doubleToLongBits(get(obj));
 int temp = (int) (bits ^ (bits >>> 32));
 result += obj.hashCode() ^ temp;
 

This is similar to the hash code used by java.util.Map.Entry and uses ideas from Bloch's book entitled Effective Java. It ensures that two weighted sets have the same hash value if they contain the same elements with the same weights.

Overrides:
hashCode in class java.util.AbstractSet
Returns:
the hash code for this set.

iterator

public java.util.Iterator iterator()
Returns an iterator over the elements in this set.
Overrides:
iterator in class java.util.AbstractCollection
Returns:
an iterator over the elements in this set.

remove

public boolean remove(java.lang.Object obj)
Removes the specified element from this set if it is present.
Overrides:
remove in class java.util.AbstractCollection
Parameters:
obj - the object to be removed from this set.
Returns:
true if this set contained the specified object.

set

public void set(java.lang.Object obj,
                double weight)
Sets the weight of the specified object to the specified value. If this set does not contain the specified object, it is added to this set with the specified weight. If the weight is equal to 0.0d, the specified object is removed from this set unless this set is being iterated over (i.e. the iterator method was called). If this set is being iterated over, the object's weight is immediately set to 0.0 and the object is then removed from this set once the iteration is finished (i.e. the Iterator.hasNext method returns false).
Parameters:
obj - the object whose weight is to be adjusted.
weight - the new weight of the specified object.

size

public int size()
Returns the number of elements in this set.
Overrides:
size in class java.util.AbstractCollection
Returns:
the number of elements in this set.

toArray

public java.lang.Object[] toArray()
Returns an array containing all of the elements in this set.
Overrides:
toArray in class java.util.AbstractCollection
Returns:
an array containing all of the elements in this set.

toArray

public java.lang.Object[] toArray(java.lang.Object[] obj)
Returns an array containing all of the elements in this set whose runtime type is that of the specified array.
Overrides:
toArray in class java.util.AbstractCollection
Parameters:
obj - the array into which the elements of this set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of this set.

toString

public java.lang.String toString()
Returns the string representation of this set.
Overrides:
toString in class java.util.AbstractCollection
Returns:
the string representation of this set.