checkpoint
[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 Element implements Topology<T> {
12
13     protected abstract Topology<T> top();
14     public    abstract String toString();
15
16     // Topology Thunks //////////////////////////////////////////////////////////////////////////////
17
18     public Topology<T>       unwrap()                   { return top().unwrap(); }
19     public Topology<T>       empty()                    { return top().empty(); }
20     public boolean           contains(T v)              { return top().contains(v); }
21     public Topology<T>       intersect(Topology<T> t)   { return top().intersect(t); }
22     public Topology<T>       minus(Topology<T> t)       { return top().minus(t); }
23     public Topology<T>       union(Topology<T> t)       { return top().union(t); }
24     public Topology<T>       complement()               { return top().complement(); }
25     public boolean           disjoint(Topology<T> t)    { return top().disjoint(t); }
26     public boolean           containsAll(Topology<T> t) { return top().containsAll(t); }
27     public int               hashCode()                 { return top().hashCode(); }
28     public boolean           equals(Object o)           { return o != null && o instanceof Atom && ((Atom)o).top().equals(top()); }
29
30     // Subclasses //////////////////////////////////////////////////////////////////////////////
31
32     public static class Infer<T extends Input> extends Atom<T> {
33         private final Element e;
34         public Infer(Element e) { this.e = e; }
35         public Topology<T> top() { return (Topology<T>)toAtom(e); }
36         public String toString() { return e.toString(); /* FIXME should be toAtom() */ }
37     }
38     
39     public static class Invert<T extends Input> extends Atom<T> {
40         private final Atom<T> a;
41         public Invert(Atom<T> a) { this.a = a; }
42         public Topology<T> top() { return ((Topology<T>)a.top()).complement(); }
43         public String toString() { return "~"+a; }
44     }
45
46     static Topology toAtom(Element e) {
47         if (e instanceof Atom) return (Atom)e;
48         if (e instanceof Sequence) return ((Sequence)e).toAtom();
49         Topology ret = null;
50         for(Sequence s : (Union)e)
51             ret = ret==null ? toAtom(s) : ret.union(s.toAtom());
52         return ret;
53     }
54 }
55