1 //
2 // This software is now distributed according to
3 // the Lesser Gnu Public License.  Please see
4 // http://www.gnu.org/copyleft/lesser.txt for
5 // the details.
6 //    -- Happy Computing!
7 //
8 package com.stevesoft.pat;
9 
10 /**
11  * This is just an integer that can have infinite value. It is used internally
12  * to implement the *, and + parts of regular expressions.
13  */
14 public class patInt
15 {
16   int i;
17 
18   boolean inf;
19 
20   /** Initialize to zero. */
patInt()21   public patInt()
22   {
23     i = 0;
24     inf = false;
25   }
26 
27   /** Initialize to the value of init. */
patInt(int init)28   public patInt(int init)
29   {
30     i = init;
31     inf = false;
32   }
33 
34   /** Initialize to the value of p. */
patInt(patInt p)35   public patInt(patInt p)
36   {
37     i = p.i;
38     inf = p.inf;
39   }
40 
41   /** set this int to infinity. */
setInf(boolean b)42   public void setInf(boolean b)
43   {
44     inf = b;
45     if (b)
46     {
47       i = Integer.MAX_VALUE;
48     }
49   }
50 
51   /** Increment the value of this by 1. */
inc()52   public final void inc()
53   {
54     if (!inf)
55     {
56       i++;
57     }
58   }
59 
60   /** Decrement the value of this by 1. */
dec()61   public final void dec()
62   {
63     if (!inf)
64     {
65       i--;
66     }
67   }
68 
69   /** Test to see if this is less than or equal to j. */
lessEq(patInt j)70   public final boolean lessEq(patInt j)
71   { /*
72      * if(inf) return false; if(j.inf) return true; return i <= j.i;
73      */
74     return !inf && (j.inf || i <= j.i);
75   }
76 
77   /** Test to see if two patterns are equal. */
equals(patInt j)78   public final boolean equals(patInt j)
79   {
80     return !j.inf && !inf && i == j.i;
81   }
82 
83   /**
84    * Formats the pattern as a String. Contrary to what you might expect,
85    * infinity is formatted as ""
86    */
toString()87   final public String toString()
88   {
89     if (inf)
90     {
91       return "";
92     }
93     else
94     {
95       return "" + i;
96     }
97   }
98 
99   /**
100    * This would be operator+=(patInt) if I were programming in C++.
101    */
pluseq(patInt p)102   public final patInt pluseq(patInt p)
103   {
104     if (inf || p.inf)
105     {
106       setInf(true);
107     }
108     else
109     {
110       i += p.i;
111     }
112     return this;
113   }
114 
115   /**
116    * Returns a patInt with value equal to the product of the value of p and
117    * this.
118    */
mul(patInt p)119   public final patInt mul(patInt p)
120   {
121     if (inf || p.inf)
122     {
123       return new patInf();
124     }
125     return new patInt(i * p.i);
126   }
127 
128   /**
129    * If the argument p has a smaller value than this, then set this Object equal
130    * to p.
131    */
mineq(patInt p)132   public final patInt mineq(patInt p)
133   {
134     if (p.inf)
135     {
136       return this;
137     }
138     if (inf)
139     {
140       i = p.i;
141     }
142     else if (p.i < i)
143     {
144       i = p.i;
145     }
146     setInf(false);
147     return this;
148   }
149 
150   /**
151    * If the argument p has a greater than this, then set this object equal to p.
152    */
maxeq(patInt p)153   public final patInt maxeq(patInt p)
154   {
155     if (inf || p.inf)
156     {
157       setInf(true);
158       return this;
159     }
160     if (p.i > i)
161     {
162       i = p.i;
163     }
164     return this;
165   }
166 
167   /** Tests to see if this represents an infinite quantity. */
finite()168   public boolean finite()
169   {
170     return !inf;
171   }
172 
173   /**
174    * Converts to a patInt to an int. Infinity is mapped Integer.MAX_VALUE;
175    */
intValue()176   public int intValue()
177   {
178     return inf ? Integer.MAX_VALUE : i;
179   }
180 };
181