001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.model.extended;
010:
011: import net.sourceforge.chaperon.common.Decoder;
012: import net.sourceforge.chaperon.model.Violations;
013:
014: /**
015: * This class represents a interval of characters within a character class.
016: *
017: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
018: * @version CVS $Id: CharacterInterval.java,v 1.1 2003/12/12 14:11:34 benedikta Exp $
019: */
020: public class CharacterInterval {
021: private SingleCharacter first = null;
022: private SingleCharacter last = null;
023: private String location = null;
024:
025: /**
026: * Creates a interval of characters.
027: */
028: public CharacterInterval() {
029: }
030:
031: /**
032: * Sets the first character for the interval.
033: *
034: * @param first First character.
035: */
036: public void setFirstCharacter(SingleCharacter first) {
037: if ((this .last == null)
038: || (this .last.getCharacter() < first.getCharacter()))
039: this .last = first;
040:
041: this .first = first;
042: }
043:
044: /**
045: * Returns the first character for the interval.
046: *
047: * @return First character.
048: */
049: public SingleCharacter getFirstCharacter() {
050: return this .first;
051: }
052:
053: /**
054: * Sets the last character for the interval.
055: *
056: * @param last Last character.
057: */
058: public void setLastCharacter(SingleCharacter last) {
059: if ((this .first == null)
060: || (this .first.getCharacter() > last.getCharacter()))
061: this .first = last;
062:
063: this .last = last;
064: }
065:
066: /**
067: * Returns the last character for the interval.
068: *
069: * @return Last character.
070: */
071: public SingleCharacter getLastCharacter() {
072: return this .last;
073: }
074:
075: public void addSingleCharacter(SingleCharacter character) {
076: if (character == null)
077: return;
078:
079: if (getLastCharacter() != null)
080: setFirstCharacter(getLastCharacter());
081:
082: setLastCharacter(character);
083: }
084:
085: public SingleCharacter[] getSingleCharacters() {
086: return new SingleCharacter[] { first, last };
087: }
088:
089: public char[] getLimits() {
090: return new char[] { first.getCharacter(), last.getCharacter() };
091: }
092:
093: public boolean contains(char minimum, char maximum) {
094: return (first.getCharacter() <= minimum)
095: && (minimum <= last.getCharacter())
096: && (first.getCharacter() <= maximum)
097: && (maximum <= last.getCharacter());
098: }
099:
100: public boolean contains(char c) {
101: return (first.getCharacter() <= c)
102: && (c <= last.getCharacter());
103: }
104:
105: /**
106: * Set the location from the input source.
107: *
108: * @param location Location in the input source.
109: */
110: public void setLocation(String location) {
111: this .location = location;
112: }
113:
114: /**
115: * Returns the location from the input source.
116: *
117: * @return Location in the input source.
118: */
119: public String getLocation() {
120: return location;
121: }
122:
123: /**
124: * Return a string representation of this pattern
125: *
126: * @return String representation of the pattern.
127: */
128: public String toString() {
129: StringBuffer buffer = new StringBuffer();
130:
131: buffer.append(Decoder.decode(this .first.getCharacter(),
132: Decoder.CLASS));
133: buffer.append("-");
134: buffer.append(Decoder.decode(this .last.getCharacter(),
135: Decoder.CLASS));
136: return buffer.toString();
137: }
138:
139: /**
140: * Create a clone of this pattern.
141: *
142: * @return Clone of this pattern.
143: *
144: * @throws CloneNotSupportedException If an exception occurs during the cloning.
145: */
146: public Object clone() {
147: CharacterInterval clone = new CharacterInterval();
148:
149: clone.setFirstCharacter(getFirstCharacter());
150: clone.setLastCharacter(getLastCharacter());
151:
152: return clone;
153: }
154:
155: /**
156: * Validates this pattern.
157: *
158: * @return Return a list of violations, if this pattern isn't valid.
159: */
160: public Violations validate() {
161: Violations violations = new Violations();
162:
163: if ((first == null) || (last == null))
164: violations.addViolation("Interval is incomplete",
165: getLocation());
166:
167: if (first.getCharacter() > last.getCharacter())
168: violations.addViolation("First is greater than the last",
169: getLocation());
170:
171: if (first.getCharacter() == last.getCharacter())
172: violations.addViolation("First is equal than the last",
173: getLocation());
174:
175: return violations;
176: }
177: }
|