2.08, not 2.07
[jalview.git] / src / 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 /** 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
13 */\r
14 public class patInt {\r
15     int i;\r
16     boolean inf;\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
25         inf = b;\r
26         if(b) i = Integer.MAX_VALUE;\r
27     }\r
28     /** Increment the value of this by 1. */\r
29     public final void inc() {\r
30         if(!inf) i++;\r
31     }\r
32     /** Decrement the value of this by 1. */\r
33     public final void dec() {\r
34         if(!inf) i--;\r
35     }\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
40                 return i <= j.i; */\r
41         return !inf && (j.inf || i <= j.i);\r
42     }\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
46     }\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
50         if(inf) return "";\r
51         else return ""+i;\r
52     }\r
53     /** This would be operator+=(patInt) if I were programming\r
54          in C++. */\r
55     public final patInt pluseq(patInt p) {\r
56         if(inf||p.inf) setInf(true);\r
57         else i += p.i;\r
58         return this;\r
59     }\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
65     }\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
70         if(inf) i = p.i;\r
71         else if(p.i < i) i = p.i;\r
72         setInf(false);\r
73         return this;\r
74     }\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
80         return this;\r
81     }\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
86         */\r
87     public int intValue() { return inf ? Integer.MAX_VALUE : i; }\r
88 };\r