refactored Topology to make it a value (immutable) class
[sbp.git] / src / edu / berkeley / sbp / Atom.java
1 package edu.berkeley.sbp;
2 import java.io.*;
3 import java.util.*;
4 import java.lang.reflect.*;
5 import java.lang.ref.*;
6 import edu.berkeley.sbp.util.*;
7 import edu.berkeley.sbp.*;
8 import edu.berkeley.sbp.*;
9
10 /** an element which matches exactly one input token */
11 public abstract class Atom<T extends Token> extends Element implements Topology<T> {
12
13     protected abstract Topology<T> top();
14
15     public Topology toAtom() { return this; }
16
17     /** equality is based on the underlying <tt>Topology</tt> */
18     public int hashCode() { return top().hashCode(); }
19     
20     /** equality is based on the underlying <tt>Topology</tt> */
21     public boolean equals(Object o) { return o != null && o instanceof Atom && ((Atom)o).top().equals(top()); }
22
23     /** declared abstract to force subclasses to override it in a useful manner */
24     public abstract String toString();
25
26     // Topology Thunks //////////////////////////////////////////////////////////////////////////////
27
28     public Topology<T>       unwrap() { return top().unwrap(); }
29     public Topology<T>       empty()                    { return top().empty(); }
30     public boolean           contains(T v)              { return top().contains(v); }
31     public Topology<T>       intersect(Topology<T> t)   { return top().intersect(t); }
32     public Topology<T>       minus(Topology<T> t)       { return top().minus(t); }
33     public Topology<T>       union(Topology<T> t)       { return top().union(t); }
34     public Topology<T>       complement()               { return top().complement(); }
35     public boolean           disjoint(Topology<T> t)    { return top().disjoint(t); }
36     public boolean           containsAll(Topology<T> t) { return top().containsAll(t); }
37
38     // Subclasses //////////////////////////////////////////////////////////////////////////////
39
40     public static class Infer<T extends Token> extends Atom<T> {
41         private final Element e;
42         public Infer(Element e) { this.e = e; }
43         public Topology<T> top() { return (Topology<T>)e.toAtom(); }
44         public String toString() { return e.toString(); }
45     }
46     
47     public static class Invert<T extends Token> extends Atom<T> {
48         private final Atom<T> a;
49         public Invert(Atom<T> a) { this.a = a; }
50         public Topology<T> top() { return ((Topology<T>)a.top()).complement(); }
51         public String toString() { return "~"+a; }
52     }
53     
54 }
55