Copyright test
[jalview.git] / src / com / stevesoft / pat / patInt.java
1 /*******************************************************************************
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $(date) The Jalview Authors
4  *
5  * This file is part of Jalview.
6  *  
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *   
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  *******************************************************************************/
21 //
22 // This software is now distributed according to
23 // the Lesser Gnu Public License.  Please see
24 // http://www.gnu.org/copyleft/lesser.txt for
25 // the details.
26 //    -- Happy Computing!
27 //
28 package com.stevesoft.pat;
29
30 /**
31  * This is just an integer that can have infinite value. It is used internally
32  * to implement the *, and + parts of regular expressions.
33  */
34 public class patInt
35 {
36   int i;
37
38   boolean inf;
39
40   /** Initialize to zero. */
41   public patInt()
42   {
43     i = 0;
44     inf = false;
45   }
46
47   /** Initialize to the value of init. */
48   public patInt(int init)
49   {
50     i = init;
51     inf = false;
52   }
53
54   /** Initialize to the value of p. */
55   public patInt(patInt p)
56   {
57     i = p.i;
58     inf = p.inf;
59   }
60
61   /** set this int to infinity. */
62   public void setInf(boolean b)
63   {
64     inf = b;
65     if (b)
66     {
67       i = Integer.MAX_VALUE;
68     }
69   }
70
71   /** Increment the value of this by 1. */
72   public final void inc()
73   {
74     if (!inf)
75     {
76       i++;
77     }
78   }
79
80   /** Decrement the value of this by 1. */
81   public final void dec()
82   {
83     if (!inf)
84     {
85       i--;
86     }
87   }
88
89   /** Test to see if this is less than or equal to j. */
90   public final boolean lessEq(patInt j)
91   { /*
92      * if(inf) return false; if(j.inf) return true; return i <= j.i;
93      */
94     return !inf && (j.inf || i <= j.i);
95   }
96
97   /** Test to see if two patterns are equal. */
98   public final boolean equals(patInt j)
99   {
100     return !j.inf && !inf && i == j.i;
101   }
102
103   /**
104    * Formats the pattern as a String. Contrary to what you might expect,
105    * infinity is formatted as ""
106    */
107   final public String toString()
108   {
109     if (inf)
110     {
111       return "";
112     }
113     else
114     {
115       return "" + i;
116     }
117   }
118
119   /**
120    * This would be operator+=(patInt) if I were programming in C++.
121    */
122   public final patInt pluseq(patInt p)
123   {
124     if (inf || p.inf)
125     {
126       setInf(true);
127     }
128     else
129     {
130       i += p.i;
131     }
132     return this;
133   }
134
135   /**
136    * Returns a patInt with value equal to the product of the value of p and
137    * this.
138    */
139   public final patInt mul(patInt p)
140   {
141     if (inf || p.inf)
142     {
143       return new patInf();
144     }
145     return new patInt(i * p.i);
146   }
147
148   /**
149    * If the argument p has a smaller value than this, then set this Object equal
150    * to p.
151    */
152   public final patInt mineq(patInt p)
153   {
154     if (p.inf)
155     {
156       return this;
157     }
158     if (inf)
159     {
160       i = p.i;
161     }
162     else if (p.i < i)
163     {
164       i = p.i;
165     }
166     setInf(false);
167     return this;
168   }
169
170   /**
171    * If the argument p has a greater than this, then set this object equal to p.
172    */
173   public final patInt maxeq(patInt p)
174   {
175     if (inf || p.inf)
176     {
177       setInf(true);
178       return this;
179     }
180     if (p.i > i)
181     {
182       i = p.i;
183     }
184     return this;
185   }
186
187   /** Tests to see if this represents an infinite quantity. */
188   public boolean finite()
189   {
190     return !inf;
191   }
192
193   /**
194    * Converts to a patInt to an int. Infinity is mapped Integer.MAX_VALUE;
195    */
196   public int intValue()
197   {
198     return inf ? Integer.MAX_VALUE : i;
199   }
200 };