KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > bnf > RuleOptional


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.bnf;
6
7 import java.util.HashMap JavaDoc;
8
9 public class RuleOptional implements Rule {
10     private Rule rule;
11     private boolean mapSet;
12     
13     RuleOptional(Rule rule, boolean repeat) {
14         this.rule = rule;
15     }
16
17     public String JavaDoc name() {
18         return null;
19     }
20
21     public String JavaDoc random(Bnf config, int level) {
22         if(level > 10 ? config.getRandom().nextInt(level) == 1 : config.getRandom().nextBoolean()) {
23             return rule.random(config, level+1);
24         } else {
25             return "";
26         }
27     }
28
29     public Rule last() {
30         return this;
31     }
32
33     public void setLinks(HashMap JavaDoc ruleMap) {
34         if(!mapSet) {
35             rule.setLinks(ruleMap);
36             mapSet = true;
37         }
38     }
39
40     public String JavaDoc matchRemove(String JavaDoc query, Sentence sentence) {
41         if(sentence.stop()) {
42             return null;
43         }
44         if(query.length()==0) {
45             return query;
46         }
47         String JavaDoc s = rule.matchRemove(query, sentence);
48         if(s == null) {
49             return query;
50         }
51         return s;
52     }
53     
54
55     public void addNextTokenList(String JavaDoc query, Sentence sentence) {
56         if(sentence.stop()) {
57             return;
58         }
59         rule.addNextTokenList(query, sentence);
60     }
61     
62 }
63
Popular Tags