refactored Topology to make it a value (immutable) class
[sbp.git] / src / edu / berkeley / sbp / util / DiscreteTopology.java
index f78bdae..0fb75eb 100644 (file)
@@ -9,14 +9,15 @@ import edu.berkeley.sbp.*;
 /** implementation of <tt>Topology</tt> for any class via equals()/hashCode() */
 public class DiscreteTopology<V> implements Topology<V> {
 
 /** implementation of <tt>Topology</tt> for any class via equals()/hashCode() */
 public class DiscreteTopology<V> implements Topology<V> {
 
+    private static final DiscreteTopology empty = new DiscreteTopology();
+    
     HashSet<V> hs = new HashSet<V>();
 
     public DiscreteTopology()              { }
     public DiscreteTopology(V v)           { hs.add(v); }
     HashSet<V> hs = new HashSet<V>();
 
     public DiscreteTopology()              { }
     public DiscreteTopology(V v)           { hs.add(v); }
-    DiscreteTopology(HashSet<V> hs) { this.hs.addAll(hs); }
+    DiscreteTopology(HashSet<V> hs)        { this.hs.addAll(hs); }
 
 
-    public Topology<V> fresh()             { return new DiscreteTopology<V>();   }
-    public Topology<V> dup()               { return new DiscreteTopology<V>(this.hs); }
+    public Topology<V> empty()             { return (Topology<V>)empty; }
 
     public HashSet<V> hs() {
         HashSet<V> ret = new HashSet<V>();
 
     public HashSet<V> hs() {
         HashSet<V> ret = new HashSet<V>();
@@ -24,18 +25,15 @@ public class DiscreteTopology<V> implements Topology<V> {
         return ret;
     }
         
         return ret;
     }
         
-    public void             add(Topology<V> t)         { hs.addAll(((DiscreteTopology<V>)t).hs); }
-    public void             remove(Topology<V> t)      { hs.removeAll(((DiscreteTopology<V>)t).hs); }
-    public void             add(V v)                   { hs.add(v); }
-    public void             remove(V v)                { hs.remove(v); }
     public boolean          contains(V v)              { return hs.contains(v); }
 
     public boolean          contains(V v)              { return hs.contains(v); }
 
-    public Topology<V> complement()                    { /*return dup(hs.complement());*/ throw new Error(); }
-    public Topology<V> intersect(Topology<V> t)        { return new DiscreteTopology(hs().retainAll(((DiscreteTopology<V>)t).hs)); }
-    public Topology<V> minus(Topology<V> t)            { return new DiscreteTopology(hs().removeAll(((DiscreteTopology<V>)t).hs)); }
-    public Topology<V> union(Topology<V> t)            { return new DiscreteTopology(hs().addAll(((DiscreteTopology<V>)t).hs)); }
-    public boolean          disjoint(Topology<V> t)    { return ((DiscreteTopology)intersect(t)).size()==0; }
-    public boolean          containsAll(Topology<V> t) { return hs.containsAll(((DiscreteTopology<V>)t).hs); }
+    public Topology<V> unwrap() { return this; }
+    public Topology<V> complement()                    { throw new Error(); }
+    public Topology<V> intersect(Topology<V> t)        { return new DiscreteTopology(hs().retainAll(((DiscreteTopology<V>)t.unwrap()).hs)); }
+    public Topology<V> minus(Topology<V> t)            { return new DiscreteTopology(hs().removeAll(((DiscreteTopology<V>)t.unwrap()).hs)); }
+    public Topology<V> union(Topology<V> t)            { return new DiscreteTopology(hs().addAll(((DiscreteTopology<V>)t.unwrap()).hs)); }
+    public boolean          disjoint(Topology<V> t)    { return ((DiscreteTopology)intersect(t).unwrap()).size()==0; }
+    public boolean          containsAll(Topology<V> t) { return hs.containsAll(((DiscreteTopology<V>)t.unwrap()).hs); }
 
     public int     hashCode()                          { return hs.hashCode(); }
     public boolean equals(Object o)                    { return o!=null && o instanceof DiscreteTopology && ((DiscreteTopology<V>)o).hs.equals(hs); }
 
     public int     hashCode()                          { return hs.hashCode(); }
     public boolean equals(Object o)                    { return o!=null && o instanceof DiscreteTopology && ((DiscreteTopology<V>)o).hs.equals(hs); }