KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > extended > 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.extended;
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.1 2003/12/12 14:11:34 benedikta Exp $
19  */

20 public class CharacterInterval
21 {
22   private SingleCharacter first = null;
23   private SingleCharacter last = null;
24   private String JavaDoc location = null;
25
26   /**
27    * Creates a interval of characters.
28    */

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

36   public void setFirstCharacter(SingleCharacter first)
37   {
38     if ((this.last==null) || (this.last.getCharacter()<first.getCharacter()))
39       this.last = first;
40
41     this.first = first;
42   }
43
44   /**
45    * Returns the first character for the interval.
46    *
47    * @return First character.
48    */

49   public SingleCharacter getFirstCharacter()
50   {
51     return this.first;
52   }
53
54   /**
55    * Sets the last character for the interval.
56    *
57    * @param last Last character.
58    */

59   public void setLastCharacter(SingleCharacter last)
60   {
61     if ((this.first==null) || (this.first.getCharacter()>last.getCharacter()))
62       this.first = last;
63
64     this.last = last;
65   }
66
67   /**
68    * Returns the last character for the interval.
69    *
70    * @return Last character.
71    */

72   public SingleCharacter getLastCharacter()
73   {
74     return this.last;
75   }
76
77   public void addSingleCharacter(SingleCharacter character)
78   {
79     if (character==null)
80       return;
81
82     if (getLastCharacter()!=null)
83       setFirstCharacter(getLastCharacter());
84
85     setLastCharacter(character);
86   }
87
88   public SingleCharacter[] getSingleCharacters()
89   {
90     return new SingleCharacter[]{first, last};
91   }
92
93   public char[] getLimits()
94   {
95     return new char[]{first.getCharacter(), last.getCharacter()};
96   }
97
98   public boolean contains(char minimum, char maximum)
99   {
100     return (first.getCharacter()<=minimum) && (minimum<=last.getCharacter()) &&
101            (first.getCharacter()<=maximum) && (maximum<=last.getCharacter());
102   }
103
104   public boolean contains(char c)
105   {
106     return (first.getCharacter()<=c) && (c<=last.getCharacter());
107   }
108
109   /**
110    * Set the location from the input source.
111    *
112    * @param location Location in the input source.
113    */

114   public void setLocation(String JavaDoc location)
115   {
116     this.location = location;
117   }
118
119   /**
120    * Returns the location from the input source.
121    *
122    * @return Location in the input source.
123    */

124   public String JavaDoc getLocation()
125   {
126     return location;
127   }
128
129   /**
130    * Return a string representation of this pattern
131    *
132    * @return String representation of the pattern.
133    */

134   public String JavaDoc toString()
135   {
136     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
137
138     buffer.append(Decoder.decode(this.first.getCharacter(), Decoder.CLASS));
139     buffer.append("-");
140     buffer.append(Decoder.decode(this.last.getCharacter(), Decoder.CLASS));
141     return buffer.toString();
142   }
143
144   /**
145    * Create a clone of this pattern.
146    *
147    * @return Clone of this pattern.
148    *
149    * @throws CloneNotSupportedException If an exception occurs during the cloning.
150    */

151   public Object JavaDoc clone()
152   {
153     CharacterInterval clone = new CharacterInterval();
154
155     clone.setFirstCharacter(getFirstCharacter());
156     clone.setLastCharacter(getLastCharacter());
157
158     return clone;
159   }
160
161   /**
162    * Validates this pattern.
163    *
164    * @return Return a list of violations, if this pattern isn't valid.
165    */

166   public Violations validate()
167   {
168     Violations violations = new Violations();
169
170     if ((first==null) || (last==null))
171       violations.addViolation("Interval is incomplete", getLocation());
172
173     if (first.getCharacter()>last.getCharacter())
174       violations.addViolation("First is greater than the last", getLocation());
175
176     if (first.getCharacter()==last.getCharacter())
177       violations.addViolation("First is equal than the last", getLocation());
178
179     return violations;
180   }
181 }
182
Popular Tags