001: /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
002: * JFlex 1.4.1 *
003: * Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
004: * All rights reserved. *
005: * *
006: * This program is free software; you can redistribute it and/or modify *
007: * it under the terms of the GNU General Public License. See the file *
008: * COPYRIGHT for more information. *
009: * *
010: * This program is distributed in the hope that it will be useful, *
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
013: * GNU General Public License for more details. *
014: * *
015: * You should have received a copy of the GNU General Public License along *
016: * with this program; if not, write to the Free Software Foundation, Inc., *
017: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
018: * *
019: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
020:
021: package JFlex;
022:
023: /**
024: * An intervall of characters with basic operations.
025: *
026: * @author Gerwin Klein
027: * @version JFlex 1.4.1, $Revision: 2.4 $, $Date: 2004/11/06 23:03:30 $
028: */
029: public final class Interval {
030:
031: /* start and end of the intervall */
032: public char start, end;
033:
034: /**
035: * Constuct a new intervall from <code>start</code> to <code>end</code>.
036: *
037: * @param start first character the intervall should contain
038: * @param end last character the intervall should contain
039: */
040: public Interval(char start, char end) {
041: this .start = start;
042: this .end = end;
043: }
044:
045: /**
046: * Copy constructor
047: */
048: public Interval(Interval other) {
049: this .start = other.start;
050: this .end = other.end;
051: }
052:
053: /**
054: * Return <code>true</code> iff <code>point</code> is contained in this intervall.
055: *
056: * @param point the character to check
057: */
058: public boolean contains(char point) {
059: return start <= point && end >= point;
060: }
061:
062: /**
063: * Return <code>true</code> iff this intervall completely contains the
064: * other one.
065: *
066: * @param other the other intervall
067: */
068: public boolean contains(Interval other) {
069: return this .start <= other.start && this .end >= other.end;
070: }
071:
072: /**
073: * Return <code>true</code> if <code>o</code> is an intervall
074: * with the same borders.
075: *
076: * @param o the object to check equality with
077: */
078: public boolean equals(Object o) {
079: if (o == this )
080: return true;
081: if (!(o instanceof Interval))
082: return false;
083:
084: Interval other = (Interval) o;
085: return other.start == this .start && other.end == this .end;
086: }
087:
088: /**
089: * Set a new last character
090: *
091: * @param end the new last character of this intervall
092: */
093: public void setEnd(char end) {
094: this .end = end;
095: }
096:
097: /**
098: * Set a new first character
099: *
100: * @param start the new first character of this intervall
101: */
102: public void setStart(char start) {
103: this .start = start;
104: }
105:
106: /**
107: * Check wether a character is printable.
108: *
109: * @param c the character to check
110: */
111: private static boolean isPrintable(char c) {
112: // fixme: should make unicode test here
113: return c > 31 && c < 127;
114: }
115:
116: /**
117: * Get a String representation of this intervall.
118: *
119: * @return a string <code>"[start-end]"</code> or
120: * <code>"[start]"</code> (if there is only one character in
121: * the intervall) where <code>start</code> and
122: * <code>end</code> are either a number (the character code)
123: * or something of the from <code>'a'</code>.
124: */
125: public String toString() {
126: StringBuffer result = new StringBuffer("[");
127:
128: if (isPrintable(start))
129: result.append("'" + start + "'");
130: else
131: result.append((int) start);
132:
133: if (start != end) {
134: result.append("-");
135:
136: if (isPrintable(end))
137: result.append("'" + end + "'");
138: else
139: result.append((int) end);
140: }
141:
142: result.append("]");
143: return result.toString();
144: }
145:
146: /**
147: * Make a copy of this interval.
148: *
149: * @return the copy
150: */
151: public Interval copy() {
152: return new Interval(start, end);
153: }
154: }
|