CriterionList.java revision 10353ed766fc48a0af6bd33d934439e695c03e32
1package annotator.specification;
2
3import java.util.HashSet;
4import java.util.Set;
5
6import annotator.find.Criteria;
7import annotator.find.Criterion;
8
9/**
10 * A CriterionList is a singly-linked list of Criterion meant to be treated
11 * as a stack.  It is very useful for creating base criteria and passing
12 * independent copies to different parts of a specification that creates
13 * all the criterion.  A CriterionList is immutable, and so copies
14 * created by the add() function can safely be passed anywhere.
15 * It is supposed to be easier to manipulate than a Criteria.
16 */
17public class CriterionList {
18  // This really is a simple data structure to facilitate creation
19  //  of specifications.  TODO: make it a private class?
20  private Criterion current;
21  private CriterionList next;
22
23  /**
24   * Creates a new CriterionList with no criterion.
25   */
26  public CriterionList() {
27    next = null;
28    current = null;
29  }
30
31  /**
32   * Creates a new CriterionList containing just the given Criterion.
33   *
34   * @param c the sole criterion the list contains at the moment
35   */
36  public CriterionList(Criterion c) {
37    current = c;
38    next = null;
39  }
40
41  private CriterionList(Criterion c, CriterionList n) {
42    current = c;
43    next = n;
44  }
45
46  /**
47   * Adds the given criterion to the present list and returns a newly
48   * allocated list containing the result.  Note that this will not lead to
49   * a modification of the list this is called on.
50   *
51   * @param c the criterion to add
52   * @return a new list containing the given criterion and the rest of the
53   *  criterion already in this list
54   */
55  public CriterionList add(Criterion c) {
56    return new CriterionList(c, this);
57  }
58
59  /**
60   * Creates a Criteria object representing all the criterion in this list.
61   *
62   * @return a Criteria that contains all the criterion in this list
63   */
64  public Criteria criteria() {
65    Criteria criteria = new Criteria();
66
67    CriterionList c = this;
68    Set<Criterion> criterion = new HashSet<Criterion>();
69    while (c != null && c.current != null) {
70      criteria.add(c.current);
71      c = c.next;
72    }
73
74    return criteria;
75  }
76
77}
78