KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > Interval


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * JFlex 1.4.1 *
3  * Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
4  * All rights reserved. *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License. See the file *
8  * COPYRIGHT for more information. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License along *
16  * with this program; if not, write to the Free Software Foundation, Inc., *
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
18  * *
19  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

20
21 package JFlex;
22
23
24 /**
25  * An intervall of characters with basic operations.
26  *
27  * @author Gerwin Klein
28  * @version JFlex 1.4.1, $Revision: 2.4 $, $Date: 2004/11/06 23:03:30 $
29  */

30 public final class Interval {
31
32   /* start and end of the intervall */
33   public char start, end;
34   
35
36   /**
37    * Constuct a new intervall from <code>start</code> to <code>end</code>.
38    *
39    * @param start first character the intervall should contain
40    * @param end last character the intervall should contain
41    */

42   public Interval(char start, char end) {
43     this.start = start;
44     this.end = end;
45   }
46
47
48   /**
49    * Copy constructor
50    */

51   public Interval(Interval other) {
52     this.start = other.start;
53     this.end = other.end;
54   }
55
56
57   /**
58    * Return <code>true</code> iff <code>point</code> is contained in this intervall.
59    *
60    * @param point the character to check
61    */

62   public boolean contains(char point) {
63     return start <= point && end >= point;
64   }
65
66
67   /**
68    * Return <code>true</code> iff this intervall completely contains the
69    * other one.
70    *
71    * @param other the other intervall
72    */

73   public boolean contains(Interval other) {
74     return this.start <= other.start && this.end >= other.end;
75   }
76   
77
78   /**
79    * Return <code>true</code> if <code>o</code> is an intervall
80    * with the same borders.
81    *
82    * @param o the object to check equality with
83    */

84   public boolean equals(Object JavaDoc o) {
85     if ( o == this ) return true;
86     if ( !(o instanceof Interval) ) return false;
87
88     Interval other = (Interval) o;
89     return other.start == this.start && other.end == this.end;
90   }
91   
92
93   /**
94    * Set a new last character
95    *
96    * @param end the new last character of this intervall
97    */

98   public void setEnd(char end) {
99     this.end = end;
100   }
101
102
103   /**
104    * Set a new first character
105    *
106    * @param start the new first character of this intervall
107    */

108   public void setStart(char start) {
109     this.start = start;
110   }
111   
112   
113   /**
114    * Check wether a character is printable.
115    *
116    * @param c the character to check
117    */

118   private static boolean isPrintable(char c) {
119     // fixme: should make unicode test here
120
return c > 31 && c < 127;
121   }
122
123
124   /**
125    * Get a String representation of this intervall.
126    *
127    * @return a string <code>"[start-end]"</code> or
128    * <code>"[start]"</code> (if there is only one character in
129    * the intervall) where <code>start</code> and
130    * <code>end</code> are either a number (the character code)
131    * or something of the from <code>'a'</code>.
132    */

133   public String JavaDoc toString() {
134     StringBuffer JavaDoc result = new StringBuffer JavaDoc("[");
135
136     if ( isPrintable(start) )
137       result.append("'"+start+"'");
138     else
139       result.append( (int) start );
140
141     if (start != end) {
142       result.append("-");
143
144       if ( isPrintable(end) )
145         result.append("'"+end+"'");
146       else
147         result.append( (int) end );
148     }
149
150     result.append("]");
151     return result.toString();
152   }
153
154
155   /**
156    * Make a copy of this interval.
157    *
158    * @return the copy
159    */

160   public Interval copy() {
161     return new Interval(start,end);
162   }
163 }
164
Popular Tags