001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2004, 2006 Robert Grimm
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * version 2 as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc.parser;
020:
021: import xtc.tree.Node;
022:
023: /**
024: * A character range for a character classs.
025: *
026: * @see CharClass
027: *
028: * @author Robert Grimm
029: * @version $Revision: 1.10 $
030: */
031: public class CharRange extends Node implements Comparable<CharRange> {
032:
033: /** The first character. */
034: public final char first;
035:
036: /** The last character. */
037: public final char last;
038:
039: /**
040: * Create a new single-character range with the specified character.
041: *
042: * @param c The character.
043: */
044: public CharRange(final char c) {
045: this (c, c);
046: }
047:
048: /**
049: * Create a new character range with the specified characters.
050: *
051: * @param first The first character.
052: * @param last The last character.
053: */
054: public CharRange(final char first, final char last) {
055: if (first > last) {
056: this .first = last;
057: this .last = first;
058: } else {
059: this .first = first;
060: this .last = last;
061: }
062: }
063:
064: /**
065: * Determine the number of characters covered by this character
066: * range.
067: *
068: * @return The number of characters for this character range.
069: */
070: public int count() {
071: return (last - first + 1);
072: }
073:
074: /**
075: * Determine whether this character range contains the specified
076: * character.
077: *
078: * @param c The character.
079: * @return <code>true</code> if this character range contains
080: * the specified character.
081: */
082: public boolean contains(final char c) {
083: return ((first <= c) && (c <= last));
084: }
085:
086: public int hashCode() {
087: if (first == last) {
088: return first;
089: } else {
090: return first + last;
091: }
092: }
093:
094: public boolean equals(final Object o) {
095: if (this == o)
096: return true;
097: if (!(o instanceof CharRange))
098: return false;
099: CharRange other = (CharRange) o;
100: if (first != other.first)
101: return false;
102: return (last == other.last);
103: }
104:
105: public int compareTo(final CharRange other) {
106: return this.first - other.first;
107: }
108:
109: }
|