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