001: package org.jingle.mocquer;
002:
003: /**
004: * This class defines the limitation method call occurrence
005: *
006: * @author JianLu
007: * @version 1.0 2004-10-28
008: * @since 1.0
009: */
010: public class Range {
011: /**
012: *
013: * Range limitation
014: *
015: * @author JianLu
016: * @version 1.0 2004-11-11
017: * @since 1.0
018: */
019: static class RangeLimit {
020: RangeLimit() {
021: }
022: };
023:
024: /**
025: * Special range limitation -- no limit
026: */
027: public static final RangeLimit NO_LIMIT = new RangeLimit();
028:
029: /**
030: * Occurrence happens only once
031: */
032: public static final Range ONE = new Range(1);
033:
034: /**
035: * Occurrence happens one or more times
036: */
037: public static final Range ONE_OR_MORE = new Range(1, NO_LIMIT);
038:
039: /**
040: * Occurrence happens zero or more times
041: */
042: public static final Range ZERO_OR_MORE = new Range(0, NO_LIMIT);
043:
044: /**
045: * The min value of the range
046: */
047: int min;
048:
049: /**
050: * The max value of the range
051: */
052: int max;
053:
054: /**
055: * Constructor
056: * @param min The min value of the range
057: * @param max The RangeLimit stands for the max value of the range
058: */
059: public Range(int min, RangeLimit max) {
060: verify(min, max);
061: this .min = min;
062: this .max = -1;
063: }
064:
065: /**
066: * Constructor
067: * @param num The min and max value of the range
068: */
069: public Range(int num) {
070: this (num, num);
071: }
072:
073: /**
074: * Constructor
075: * @param min The min value of the range
076: * @param max The max value of the range
077: */
078: public Range(int min, int max) {
079: verify(min, max);
080: this .min = min;
081: this .max = max;
082: }
083:
084: /**
085: * Verify the min value and max value
086: * @param min The min value of the range
087: * @param max The max value of the range
088: */
089: protected void verify(int min, int max) {
090: if (min < 0)
091: throw new AssertionFailedError("min times should >= 0");
092: if (max < 1)
093: throw new AssertionFailedError("max times should >= 1");
094: if (max < min)
095: throw new AssertionFailedError(
096: "max times should >= min times");
097: }
098:
099: /**
100: * Verify the min value and max value
101: * @param min The min value of the range
102: * @param max The RangeLimt stands for the max value of the range
103: */
104: protected void verify(int min, RangeLimit max) {
105: if (max != NO_LIMIT)
106: throw new AssertionFailedError(
107: "Invalid argument. RangeLimit needed");
108: if (min < 0)
109: throw new AssertionFailedError("min times should >= 0");
110: }
111:
112: /**
113: * Is the given number same as the max value of the range
114: * @param num The given number
115: * @return true or false
116: */
117: public boolean isMax(int num) {
118: if (max == -1)
119: return false;
120: return (num == max);
121: }
122:
123: /**
124: * Is the given number same as the min value of the range
125: * @param num The given number
126: * @return true or false
127: */
128: public boolean isMin(int num) {
129: return (num == min);
130: }
131:
132: /**
133: * Is the given number greater than the max value of the range
134: * @param num The given number
135: * @return true or false
136: */
137: public boolean greaterThanMax(int num) {
138: if (max == -1)
139: return false;
140: return num > max;
141: }
142:
143: /**
144: * Is the given number lsee than the min value of the range
145: * @param num The given number
146: * @return true or false
147: */
148: public boolean lessThanMin(int num) {
149: return num < min;
150: }
151:
152: /**
153: * Is the given number in the range
154: * @param num The given number
155: * @return true or false
156: */
157: public boolean inRange(int num) {
158: if (max == -1)
159: return num >= min;
160: return ((num >= min) && (num <= max));
161: }
162:
163: /**
164: * Is the range has no upper limitation
165: * @return true or false
166: */
167: public boolean isNoLimit() {
168: return (max == -1);
169: }
170:
171: /**
172: * (non-Javadoc)
173: * @see java.lang.Object#toString()
174: */
175: public String toString() {
176: StringBuffer sb = new StringBuffer();
177: sb.append("[").append(min).append(",");
178: if (max != -1)
179: sb.append(max);
180: else
181: sb.append("*");
182: sb.append("]");
183: return sb.toString();
184: }
185: }
|