JSci.maths.FiniteSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsci Show documentation
Show all versions of jsci Show documentation
JSci is a set of open source Java packages. The aim is to encapsulate scientific methods/principles in the most natural way possible. As such they should greatly aid the development of scientific based software.
It offers: abstract math interfaces, linear algebra (support for various matrix and vector types), statistics (including probability distributions), wavelets, newtonian mechanics, chart/graph components (AWT and Swing), MathML DOM implementation, ...
Note: some packages, like javax.comm, for the astro and instruments package aren't listed as dependencies (not available).
The newest version!
package JSci.maths;
import java.util.Collections;
import java.util.Set;
import java.util.HashSet;
/**
* A set containing a finite number of elements.
* This class provides a bridge between java.util.Set
* and JSci.maths.MathSet
.
* @version 1.0
* @author Mark Hale
*/
public final class FiniteSet extends Object implements MathSet {
private final Set elements;
/**
* Constructs a finite set.
* @param set a set of elements
*/
public FiniteSet(Set set) {
elements = set;
}
/**
* Compares two sets for equality.
*/
public boolean equals(Object s) {
return (s != null) && (s instanceof FiniteSet) && elements.equals(((FiniteSet)s).elements);
}
public int hashCode() {
return elements.hashCode();
}
/**
* Returns a string representing this set.
*/
public String toString() {
return elements.toString();
}
/**
* Returns the elements of this set.
*/
public Set getElements() {
return Collections.unmodifiableSet(elements);
}
/**
* Returns the cardinality.
*/
public int cardinality() {
return elements.size();
}
/**
* Performs the union of this set with another.
* @param set a set.
* @return the union of the two sets.
*/
public MathSet union(MathSet set) {
Set union = new HashSet(elements);
union.addAll(((FiniteSet)set).elements);
return new FiniteSet(union);
}
/**
* Performs the intersection of this set with another.
* @param set a set.
* @return the intersection of the two sets.
*/
public MathSet intersect(MathSet set) {
Set intersection = new HashSet(elements);
intersection.retainAll(((FiniteSet)set).elements);
return new FiniteSet(intersection);
}
}