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