org.biojavax.ga.util
Class WeightedSet

java.lang.Object
  extended by java.util.AbstractCollection
      extended by java.util.AbstractSet
          extended by org.biojavax.ga.util.WeightedSet
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable, java.util.Collection, java.util.Set

public class WeightedSet
extends java.util.AbstractSet
implements java.io.Serializable

Inspred by the BioJava Distribution objects the WeightedSet is a map from a Key to a Weight. Unlike Distributions the Keys do not have to be Symbols. In the GA package the WeightedMap is useful for sampling Organisms according to their fitness.

When Symbols are added or their weights are set then the weights are internally normalized to 1

Since:
1.5
Version:
1.0
Author:
Mark Schreiber
See Also:
Serialized Form

Constructor Summary
WeightedSet()
           
 
Method Summary
 boolean add(java.lang.Object o)
          Adds a new Object with a weight of zero.
 boolean addAll(java.util.Collection c)
           
 java.util.Map asMap()
          Converts the Set to a map from key Objects to Double weights.
 void clear()
           
 boolean contains(java.lang.Object o)
           
 boolean containsAll(java.util.Collection c)
           
protected  double getTotalWeight()
          The total weight that has been added to this Set.
 double getWeight(java.lang.Object o)
          Determines the normalized weight for o
 boolean isEmpty()
           
 java.util.Iterator iterator()
          Returns an unmodifiable iterator over the keys of the set.
 boolean remove(java.lang.Object o)
           
 boolean retainAll(java.util.Collection c)
           
 java.lang.Object sample()
          Randomly samples an Object from the Set according to its weight.
 void setWeight(java.lang.Object o, double w)
          Sets the weight of an Object.
 int size()
           
 java.lang.Object[] toArray()
           
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
toArray
 

Constructor Detail

WeightedSet

public WeightedSet()
Method Detail

asMap

public java.util.Map asMap()
Converts the Set to a map from key Objects to Double weights.

Returns:
a Map with all the key-weight mappings. Weights are not normalized in this map.

sample

public java.lang.Object sample()
Randomly samples an Object from the Set according to its weight.

Returns:
the Object sampled.

getWeight

public double getWeight(java.lang.Object o)
                 throws java.util.NoSuchElementException
Determines the normalized weight for o

Parameters:
o - the Object you want to know the weight of
Returns:
the normalized weight
Throws:
java.util.NoSuchElementException - if o is not found in this set

getTotalWeight

protected double getTotalWeight()
The total weight that has been added to this Set.

Returns:
the total weight (the value that can be used for normalizing)

setWeight

public void setWeight(java.lang.Object o,
                      double w)
Sets the weight of an Object. If the Object is not in this Set then it is added.

Parameters:
o - the Object
w - the weight.
Throws:
java.lang.IllegalArgumentException - if w is < 0.0

contains

public boolean contains(java.lang.Object o)
Specified by:
contains in interface java.util.Collection
Specified by:
contains in interface java.util.Set
Overrides:
contains in class java.util.AbstractCollection

remove

public boolean remove(java.lang.Object o)
Specified by:
remove in interface java.util.Collection
Specified by:
remove in interface java.util.Set
Overrides:
remove in class java.util.AbstractCollection

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Collection
Specified by:
isEmpty in interface java.util.Set
Overrides:
isEmpty in class java.util.AbstractCollection

retainAll

public boolean retainAll(java.util.Collection c)
Specified by:
retainAll in interface java.util.Collection
Specified by:
retainAll in interface java.util.Set
Overrides:
retainAll in class java.util.AbstractCollection

add

public boolean add(java.lang.Object o)
Adds a new Object with a weight of zero. Equivalent to setWeight(o, 0.0);

Specified by:
add in interface java.util.Collection
Specified by:
add in interface java.util.Set
Overrides:
add in class java.util.AbstractCollection
Parameters:
o - the object to add.
Returns:
true if this Object has not been added before.

size

public int size()
Specified by:
size in interface java.util.Collection
Specified by:
size in interface java.util.Set
Specified by:
size in class java.util.AbstractCollection

containsAll

public boolean containsAll(java.util.Collection c)
Specified by:
containsAll in interface java.util.Collection
Specified by:
containsAll in interface java.util.Set
Overrides:
containsAll in class java.util.AbstractCollection

toArray

public java.lang.Object[] toArray()
Specified by:
toArray in interface java.util.Collection
Specified by:
toArray in interface java.util.Set
Overrides:
toArray in class java.util.AbstractCollection

clear

public void clear()
Specified by:
clear in interface java.util.Collection
Specified by:
clear in interface java.util.Set
Overrides:
clear in class java.util.AbstractCollection

iterator

public java.util.Iterator iterator()
Returns an unmodifiable iterator over the keys of the set.

Specified by:
iterator in interface java.lang.Iterable
Specified by:
iterator in interface java.util.Collection
Specified by:
iterator in interface java.util.Set
Specified by:
iterator in class java.util.AbstractCollection
Returns:
an Iterator

addAll

public boolean addAll(java.util.Collection c)
Specified by:
addAll in interface java.util.Collection
Specified by:
addAll in interface java.util.Set
Overrides:
addAll in class java.util.AbstractCollection