KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > util > math > IntegerReuseable


1 /*
2  * IntegerReusable.java, a mutable Integer for jchart2d.
3  * Copyright (C) Achim Westermann, created on 12.05.2005, 20:11:17
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  *
22  */

23 package info.monitorenter.util.math;
24
25 /**
26  * Mutable {@link java.lang.Integer}.
27  * <p>
28  *
29  * I needed an wrapper of an primitive int to share the same value between
30  * different instances and to have the changes made to the primitive value take
31  * effect on all owners of the same instance.
32  * <p>
33  *
34  * What a pity that java.lang.Integer does not allow to change it's internal
35  * value at runtime. Every time a new Integer has to be constructed.
36  * <p>
37  *
38  */

39 public class IntegerReuseable {
40
41   /**
42    * The largest value of type <code>int</code>. The constant value of this
43    * field is <tt>2147483647</tt>.
44    */

45   public static final int MAX_VALUE = 0x7fffffff;
46
47   /**
48    * The smallest value of type <code>int</code>. The constant value of this
49    * field is <tt>-2147483648</tt>.
50    */

51   public static final int MIN_VALUE = 0x80000000;
52
53   /**
54    * The value of the Integer.
55    */

56   private int m_value;
57
58   /**
59    * Constructs a newly allocated <code>Integer</code> object that represents
60    * the primitive <code>int</code> argument.
61    *
62    * @param value
63    * the value to be represented by the <code>Integer</code>.
64    */

65   public IntegerReuseable(final int value) {
66     this.m_value = value;
67   }
68
69   /**
70    * Adds the given value to the internal value.
71    * <p>
72    *
73    * @param i
74    * the value to add.
75    *
76    * @throws ArithmeticException
77    * if an overflow ({@link Integer#MAX_VALUE}) occurs.
78    */

79   public void add(final int i) throws ArithmeticException JavaDoc {
80     int oldval = this.m_value;
81     this.m_value += i;
82     if (oldval > this.m_value) {
83       this.m_value = oldval;
84       throw new ArithmeticException JavaDoc("Overflow detected. Value saved unchanged.");
85     }
86   }
87
88   /**
89    * Adds the given value to the internal value.
90    * <p>
91    *
92    * @param i
93    * the value to add.
94    *
95    * @throws ArithmeticException
96    * if an overflow ({@link Integer#MAX_VALUE}) occurs.
97    */

98   public void add(final Integer JavaDoc i) throws ArithmeticException JavaDoc {
99     this.add(i.intValue());
100   }
101
102   /**
103    * Adds the given value to the internal value.
104    * <p>
105    *
106    * @param i
107    * the value to add.
108    *
109    * @throws ArithmeticException
110    * if an overflow ({@link Integer#MAX_VALUE}) occurs.
111    */

112   public void add(final IntegerReuseable i) throws ArithmeticException JavaDoc {
113     this.add(i.getValue());
114   }
115
116   /**
117    * Returns the value as an int.
118    * <p>
119    *
120    * @return the value as an int.
121    */

122   public int getValue() {
123     return this.m_value;
124   }
125
126   /**
127    * Returns the value as an int.
128    * <p>
129    *
130    * @return the value as an int.
131    */

132   public int intValue() {
133     return this.m_value;
134   }
135
136   /**
137    * Sets the value.
138    * <p>
139    *
140    * @param value
141    * the value.
142    */

143   public void setValue(final int value) {
144     this.m_value = value;
145   }
146
147   /**
148    * Substracts the given value from the internal value.
149    * <p>
150    *
151    * @param i
152    * the value to subtract.
153    *
154    * @throws ArithmeticException
155    * if a carry ({@link Integer#MIN_VALUE}) occurs.
156    */

157   public void sub(final int i) throws ArithmeticException JavaDoc {
158     int oldval = this.m_value;
159     this.m_value -= i;
160     if (oldval < this.m_value) {
161       this.m_value = oldval;
162       throw new ArithmeticException JavaDoc("Carry detected. Value saved unchanged.");
163     }
164   }
165
166   /**
167    * Substracts the given value from the internal value.
168    * <p>
169    *
170    * @param i
171    * the value to subtract.
172    *
173    * @throws ArithmeticException
174    * if a carry ({@link Integer#MIN_VALUE}) occurs.
175    */

176   public void sub(final Integer JavaDoc i) throws ArithmeticException JavaDoc {
177     this.sub(i.intValue());
178   }
179
180   /**
181    * Substracts the given value from the internal value.
182    * <p>
183    *
184    * @param i
185    * the value to subtract.
186    *
187    * @throws ArithmeticException
188    * if a carry ({@link Integer#MIN_VALUE}) occurs.
189    */

190   public void sub(final IntegerReuseable i) throws ArithmeticException JavaDoc {
191     this.sub(i.intValue());
192   }
193
194   /**
195    * Returns a String object representing this Integer's value.
196    * <p>
197    *
198    * The value is converted to signed decimal representation and returned as a
199    * string, exactly as if the integer value were given as an argument to the
200    * {@link java.lang.Integer#toString(int)} method.
201    * <p>
202    *
203    *
204    * @return a string representation of the value of this object in
205    * base&nbsp;10.
206    */

207   public String JavaDoc toString() {
208     return String.valueOf(this.m_value);
209   }
210 }
211
Popular Tags