2 // This software is now distributed according to
\r
3 // the Lesser Gnu Public License. Please see
\r
4 // http://www.gnu.org/copyleft/lesser.txt for
\r
6 // -- Happy Computing!
\r
8 package com.stevesoft.pat;
\r
10 /** This is just an integer that can have infinite value.
\r
11 It is used internally to implement the *, and + parts
\r
12 of regular expressions.
\r
14 public class patInt {
\r
17 /** Initialize to zero. */
\r
18 public patInt() { i = 0; inf = false; }
\r
19 /** Initialize to the value of init. */
\r
20 public patInt(int init) { i = init; inf = false; }
\r
21 /** Initialize to the value of p. */
\r
22 public patInt(patInt p) { i = p.i; inf = p.inf; }
\r
23 /** set this int to infinity. */
\r
24 public void setInf(boolean b) {
\r
26 if(b) i = Integer.MAX_VALUE;
\r
28 /** Increment the value of this by 1. */
\r
29 public final void inc() {
\r
32 /** Decrement the value of this by 1. */
\r
33 public final void dec() {
\r
36 /** Test to see if this is less than or equal to j. */
\r
37 public final boolean lessEq(patInt j) { /*
\r
38 if(inf) return false;
\r
39 if(j.inf) return true;
\r
41 return !inf && (j.inf || i <= j.i);
\r
43 /** Test to see if two patterns are equal. */
\r
44 public final boolean equals(patInt j) {
\r
45 return !j.inf && !inf && i==j.i;
\r
47 /** Formats the pattern as a String. Contrary to
\r
48 what you might expect, infinity is formatted as "" */
\r
49 final public String toString() {
\r
53 /** This would be operator+=(patInt) if I were programming
\r
55 public final patInt pluseq(patInt p) {
\r
56 if(inf||p.inf) setInf(true);
\r
60 /** Returns a patInt with value equal to the product
\r
61 of the value of p and this. */
\r
62 public final patInt mul(patInt p) {
\r
63 if(inf||p.inf) return new patInf();
\r
64 return new patInt(i*p.i);
\r
66 /** If the argument p has a smaller value than this,
\r
67 then set this Object equal to p. */
\r
68 public final patInt mineq(patInt p) {
\r
69 if(p.inf) return this;
\r
71 else if(p.i < i) i = p.i;
\r
75 /** If the argument p has a greater than this,
\r
76 then set this object equal to p. */
\r
77 public final patInt maxeq(patInt p) {
\r
78 if(inf || p.inf) { setInf(true); return this; }
\r
79 if(p.i > i) i = p.i;
\r
82 /** Tests to see if this represents an infinite quantity. */
\r
83 public boolean finite() { return !inf; }
\r
84 /** Converts to a patInt to an int. Infinity is
\r
85 mapped Integer.MAX_VALUE;
\r
87 public int intValue() { return inf ? Integer.MAX_VALUE : i; }
\r