1 package JSci.maths; 2 3 import java.util.Collections ; 4 import java.util.Set ; 5 import java.util.HashSet ; 6 7 14 public final class FiniteSet extends Object implements MathSet { 15 private final Set elements; 16 20 public FiniteSet(Set set) { 21 elements = set; 22 } 23 26 public boolean equals(Object s) { 27 return (s != null) && (s instanceof FiniteSet) && elements.equals(((FiniteSet)s).elements); 28 } 29 public int hashCode() { 30 return elements.hashCode(); 31 } 32 35 public String toString() { 36 return elements.toString(); 37 } 38 41 public Set getElements() { 42 return Collections.unmodifiableSet(elements); 43 } 44 47 public int cardinality() { 48 return elements.size(); 49 } 50 55 public MathSet union(MathSet set) { 56 Set union = new HashSet (elements); 57 union.addAll(((FiniteSet)set).elements); 58 return new FiniteSet(union); 59 } 60 65 public MathSet intersect(MathSet set) { 66 Set intersection = new HashSet (elements); 67 intersection.retainAll(((FiniteSet)set).elements); 68 return new FiniteSet(intersection); 69 } 70 } 71 72 | Popular Tags |