File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\bnf\RuleList.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.bnf;
8
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import org.h2.util.New;
12
13 /**
14 * Represents a sequence of BNF rules, or a list of alternative rules.
15 */
16 class RuleList implements Rule {
17
18 private final boolean or;
19 private final ArrayList<Rule> list;
20 private boolean mapSet;
21
22 RuleList(Rule first, Rule next, boolean or) {
23 list = New.arrayList();
24 if (first instanceof RuleList && ((RuleList) first).or == or) {
25 list.addAll(((RuleList) first).list);
26 } else {
27 list.add(first);
28 }
29 if (next instanceof RuleList && ((RuleList) next).or == or) {
30 list.addAll(((RuleList) next).list);
31 } else {
32 list.add(next);
33 }
34 this.or = or;
35 }
36
37 public void accept(BnfVisitor visitor) {
38 visitor.visitRuleList(or, list);
39 }
40
41 public void setLinks(HashMap<String, RuleHead> ruleMap) {
42 if (!mapSet) {
43 for (Rule r : list) {
44 r.setLinks(ruleMap);
45 }
46 mapSet = true;
47 }
48 }
49
50 public boolean autoComplete(Sentence sentence) {
51 sentence.stopIfRequired();
52 String old = sentence.getQuery();
53 if (or) {
54 for (Rule r : list) {
55 sentence.setQuery(old);
56 if (r.autoComplete(sentence)) {
57 return true;
58 }
59 }
60 return false;
61 }
62 for (Rule r : list) {
63 if (!r.autoComplete(sentence)) {
64 sentence.setQuery(old);
65 return false;
66 }
67 }
68 return true;
69 }
70
71 }
72