1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.millscript.millscript.tools;
23
24 import org.millscript.millscript.alert.Alerts;
25
26 /**
27 * This utility class provides simple integer based functionality to support
28 * MillScript.
29 */
30 public final class IntegerTools {
31
32 /**
33 * The number of Integers to share. This is really just half the number that
34 * will be shared, as both positive and negative integers up to this value
35 * will be available.
36 */
37 private static final int WIDTH = 32;
38
39 /**
40 * The array of Integers available for sharing.
41 */
42 private static final Integer[] INTEGERS = new Integer[ 2 * WIDTH ];
43
44
45 static {
46 for ( int i = 0 - WIDTH; i < WIDTH; i++ ) {
47 INTEGERS[ i + WIDTH ] = new Integer( i );
48 }
49 }
50
51 /**
52 * Hidden constructor.
53 */
54 private IntegerTools() {
55 }
56
57 /**
58 * Returns an Integer for the specified int, using a shared Integer if
59 * available.
60 *
61 * @param n the int to make an Integer for
62 * @return an Integer for the specified int, using a shared Integer if
63 * available
64 */
65 public static Integer make( final int n ) {
66 return (
67 -WIDTH <= n && n < WIDTH ? INTEGERS[ WIDTH + n ] :
68 new Integer( n )
69 );
70 }
71
72 /**
73 * Performs a specified operation on the specified integers, using integer
74 * arithmetic.
75 *
76 * @param sym the symbol for the arithmetic operation to perform
77 * @param x the first integer
78 * @param y the second integer
79 * @return an Integer holding the result of the arithmetic operation
80 * @todo This method should take Integers as arguments not Objects
81 */
82 public static Integer op( final String sym, final Object x, final Object y ) {
83 if ( sym == "+" ) {
84 return add( x, y );
85 }
86 if ( sym == "-" ) {
87 return sub( x, y );
88 }
89 if ( sym == "*" ) {
90 return mul( x, y );
91 }
92 if ( sym == "div" ) {
93 return div( x, y );
94 }
95 if ( sym == "mod" ) {
96 return mod( x, y );
97 }
98 throw( Alerts.fault( sym ).mishap() );
99 }
100
101 /**
102 * Returns the Integer result of adding the two specified Integers
103 * together.
104 *
105 * @param a the first Integer
106 * @param b the second Integer
107 * @return the Integer result of adding the two values
108 */
109 public static Integer add( final int a, final Object b ) {
110 return new Integer( a + CastLibrary.toInt( b ) );
111 }
112
113 /**
114 * Returns the Integer result of adding the two specified Integers
115 * together.
116 *
117 * @param a the first Integer
118 * @param b the second Integer
119 * @return the Integer result of adding the two values
120 */
121 public static Integer add( final Object a, final int b ) {
122 return new Integer( CastLibrary.toInt( a ) + b );
123 }
124
125 /**
126 * Returns the Integer result of adding the two specified Integers
127 * together.
128 *
129 * @param a the first Integer
130 * @param b the second Integer
131 * @return the Integer result of adding the two values
132 */
133 public static Integer add( final Object a, final Object b ) {
134 return new Integer( CastLibrary.toInt( a ) + CastLibrary.toInt( b ) );
135 }
136
137 /**
138 * Returns the Integer result of subtracting the second Integer from the
139 * first.
140 *
141 * @param a the first Integer
142 * @param b the second Integer
143 * @return the Integer result of subtracting the second Integer from the
144 * first
145 */
146 public static Integer sub( final int a, final Object b ) {
147 return new Integer( a - CastLibrary.toInt( b ) );
148 }
149
150 /**
151 * Returns the Integer result of subtracting the second Integer from the
152 * first.
153 *
154 * @param a the first Integer
155 * @param b the second Integer
156 * @return the Integer result of subtracting the second Integer from the
157 * first
158 */
159 public static Integer sub( final Object a, final int b ) {
160 return new Integer( CastLibrary.toInt( a ) - b );
161 }
162
163 /**
164 * Returns the Integer result of subtracting the second Integer from the
165 * first.
166 *
167 * @param a the first Integer
168 * @param b the second Integer
169 * @return the Integer result of subtracting the second Integer from the
170 * first
171 */
172 public static Integer sub( final Object a, final Object b ) {
173 return new Integer( CastLibrary.toInt( a ) - CastLibrary.toInt( b ) );
174 }
175
176 /**
177 * Returns the Integer result of multiplying the two specified Integers
178 * together.
179 *
180 * @param a the first Integer
181 * @param b the second Integer
182 * @return the Integer result of multiplying the two values
183 */
184 public static Integer mul( final int a, final Object b ) {
185 return new Integer( a * CastLibrary.toInt( b ) );
186 }
187
188 /**
189 * Returns the Integer result of multiplying the two specified Integers
190 * together.
191 *
192 * @param a the first Integer
193 * @param b the second Integer
194 * @return the Integer result of multiplying the two values
195 */
196 public static Integer mul( final Object a, final int b ) {
197 return new Integer( CastLibrary.toInt( a ) * b );
198 }
199
200 /**
201 * Returns the Integer result of multiplying the two specified Integers
202 * together.
203 *
204 * @param a the first Integer
205 * @param b the second Integer
206 * @return the Integer result of multiplying the two values
207 */
208 public static Integer mul( final Object a, final Object b ) {
209 return new Integer( CastLibrary.toInt( a ) * CastLibrary.toInt( b ) );
210 }
211
212 /**
213 * Returns the Integer result of dividing the first Integer by the second.
214 *
215 * @param a the first Integer
216 * @param b the second Integer
217 * @return the Integer result of dividing the first Integer by the second
218 */
219 public static Integer div( final int a, final Object b ) {
220 return new Integer( a / CastLibrary.toInt( b ) );
221 }
222
223 /**
224 * Returns the Integer result of dividing the first Integer by the second.
225 *
226 * @param a the first Integer
227 * @param b the second Integer
228 * @return the Integer result of dividing the first Integer by the second
229 */
230 public static Integer div( final Object a, final int b ) {
231 return new Integer( CastLibrary.toInt( a ) / b );
232 }
233
234 /**
235 * Returns the Integer result of dividing the first Integer by the second.
236 *
237 * @param a the first Integer
238 * @param b the second Integer
239 * @return the Integer result of dividing the first Integer by the second
240 */
241 public static Integer div( final Object a, final Object b ) {
242 return new Integer( CastLibrary.toInt( a ) / CastLibrary.toInt( b ) );
243 }
244
245 /**
246 * Returns the Integer remainder left after dividing the first Integer by
247 * the second.
248 *
249 * @param a the first Integer
250 * @param b the second Integer
251 * @return the Integer remainder left after dividing the first Integer by
252 * the second
253 */
254 public static Integer mod( final int a, final Object b ) {
255 return new Integer( a % CastLibrary.toInt( b ) );
256 }
257
258 /**
259 * Returns the Integer remainder left after dividing the first Integer by
260 * the second.
261 *
262 * @param a the first Integer
263 * @param b the second Integer
264 * @return the Integer remainder left after dividing the first Integer by
265 * the second
266 */
267 public static Integer mod( final Object a, final int b ) {
268 return new Integer( CastLibrary.toInt( a ) % b );
269 }
270
271 /**
272 * Returns the Integer remainder left after dividing the first Integer by
273 * the second.
274 *
275 * @param a the first Integer
276 * @param b the second Integer
277 * @return the Integer remainder left after dividing the first Integer by
278 * the second
279 */
280 public static Integer mod( final Object a, final Object b ) {
281 return new Integer( CastLibrary.toInt( a ) % CastLibrary.toInt( b ) );
282 }
283
284 }