KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > pattern > CharacterInterval


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.pattern;
10
11 import net.sourceforge.chaperon.common.Decoder;
12 import net.sourceforge.chaperon.model.Violations;
13
14 /**
15  * This class represents a interval of characters within a character class.
16  *
17  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
18  * @version CVS $Id: CharacterInterval.java,v 1.5 2003/12/09 19:55:52 benedikta Exp $
19  */

20 public class CharacterInterval implements CharacterClassElement
21 {
22   private char minimum = 'a';
23   private char maximum = 'z';
24   private String JavaDoc location = null;
25
26   /**
27    * Creates a interval of characters.
28    */

29   public CharacterInterval() {}
30
31   /**
32    * Sets the minimum character for the interval.
33    *
34    * @param minimum Minimum character.
35    */

36   public void setMinimum(char minimum)
37   {
38     this.minimum = minimum;
39   }
40
41   /**
42    * Returns the minimum character for the interval.
43    *
44    * @return Minimum character.
45    */

46   public char getMinimum()
47   {
48     return this.minimum;
49   }
50
51   /**
52    * Sets the maximum character for the interval.
53    *
54    * @param maximum Maximum character.
55    */

56   public void setMaximum(char maximum)
57   {
58     this.maximum = maximum;
59   }
60
61   /**
62    * Returns the maximum character for the interval.
63    *
64    * @return Maximum character.
65    */

66   public char getMaximum()
67   {
68     return this.maximum;
69   }
70
71   /**
72    * Set the location from the input source.
73    *
74    * @param location Location in the input source.
75    */

76   public void setLocation(String JavaDoc location)
77   {
78     this.location = location;
79   }
80
81   /**
82    * Returns the location from the input source.
83    *
84    * @return Location in the input source.
85    */

86   public String JavaDoc getLocation()
87   {
88     return location;
89   }
90
91   /**
92    * Return a string representation of this pattern
93    *
94    * @return String representation of the pattern.
95    */

96   public String JavaDoc toString()
97   {
98     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
99
100     buffer.append(Decoder.decode(this.minimum, Decoder.CLASS));
101     buffer.append("-");
102     buffer.append(Decoder.decode(this.maximum, Decoder.CLASS));
103     return buffer.toString();
104   }
105
106   /**
107    * Create a clone of this pattern.
108    *
109    * @return Clone of this pattern.
110    *
111    * @throws CloneNotSupportedException If an exception occurs during the cloning.
112    */

113   public Object JavaDoc clone()
114   {
115     CharacterInterval clone = new CharacterInterval();
116
117     clone.setMinimum(getMinimum());
118     clone.setMaximum(getMaximum());
119
120     return clone;
121   }
122
123   /**
124    * Validates this pattern.
125    *
126    * @return Return a list of violations, if this pattern isn't valid.
127    */

128   public Violations validate()
129   {
130     Violations violations = new Violations();
131
132     if (minimum>maximum)
133       violations.addViolation("Minimum is greater than the maximum", getLocation());
134
135     if (minimum==maximum)
136       violations.addViolation("Minimum is equal than the maximum", getLocation());
137
138     return violations;
139   }
140 }
141
Popular Tags