KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > extended > Optional


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.model.extended;
10
11 import net.sourceforge.chaperon.model.Violations;
12
13 /**
14  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
15  * @version CVS $Id: Optional.java,v 1.1 2003/12/12 14:11:34 benedikta Exp $
16  */

17 public class Optional extends PatternList
18 {
19   public Optional() {}
20
21   public boolean isNullable()
22   {
23     return true;
24   }
25
26   /**
27    * Return a string representation of this pattern
28    *
29    * @return String representation of the pattern.
30    */

31   public String JavaDoc toString()
32   {
33     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
34
35     if (getPatternCount()>0)
36     {
37       buffer.append(super.toString());
38       buffer.append("?");
39     }
40
41     return buffer.toString();
42   }
43
44   public String JavaDoc toString(PatternSet previous, PatternSet next)
45   {
46     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
47
48     if (getPatternCount()>0)
49     {
50       buffer.append(super.toString(previous, next));
51       buffer.append("?");
52     }
53
54     return buffer.toString();
55   }
56
57   /**
58    * Create a clone this pattern.
59    *
60    * @return Clone of this pattern.
61    *
62    * @throws CloneNotSupportedException If an exception occurs during the cloning.
63    */

64   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
65   {
66     Optional clone = new Optional();
67
68     for (int i = 0; i<getPatternCount(); i++)
69       clone.addPattern((Pattern)getPattern(i).clone());
70
71     return clone;
72   }
73
74   /**
75    * Validates this pattern.
76    *
77    * @return Return a list of violations, if this pattern isn't valid.
78    */

79   public Violations validate()
80   {
81     Violations violations = new Violations();
82
83     /*if (pattern==null)
84       violations.addViolation("Multiplier contains no pattern",
85                               getLocation());*/

86     for (int i = 0; i<getPatternCount(); i++)
87       violations.addViolations(getPattern(i).validate());
88
89     return violations;
90   }
91 }
92
Popular Tags