001/**
002 * IntegerPrimitive.java
003 * 
004 * Copyright (c) 2004-2012, Nicole C. Tedesco. All rights reserved.
005 * 
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007 * use this file except in compliance with the License. You may obtain a copy of
008 * the License at:
009 * 
010 * http://www.apache.org/licenses/LICENSE-2.0
011 * 
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015 * License for the specific language governing permissions and limitations under
016 * the License.
017 */
018
019package net.sf.jaccumulator.integers;
020
021import java.math.BigDecimal;
022import java.math.BigInteger;
023
024import net.sf.jaccumulator.ConcurrencyStrategy;
025import net.sf.jaccumulator.lex.To;
026import net.sf.jaccumulator.primitives.Primitive;
027import net.sf.jaccumulator.scalars.SealedScalar;
028
029/**
030 * A {@code int} precision {@link Primitive primitive} implementation
031 * 
032 * @since JAccumulator 4.0
033 * @author Nicole Tedesco (<a
034 *         href="mailto:Nicole@NicoleTedesco.com">Nicole@NicoleTedesco.com</a>)
035 */
036public final class IntegerPrimitive
037    extends
038        AbstractIntegerPrimitive<IntegerPrimitive>
039{
040    private static final long serialVersionUID = -6318471188234713319L;
041
042    private int _theValue;
043
044    public IntegerPrimitive()
045    {
046        super();
047        this._theValue = 0;
048    }
049
050    public IntegerPrimitive( final BigDecimal aValue )
051    {
052        this(aValue.intValue());
053    }
054
055    public IntegerPrimitive( final BigInteger aValue )
056    {
057        this(aValue.intValue());
058    }
059
060    public IntegerPrimitive( final boolean aValue )
061    {
062        this(To.intValue(aValue));
063    }
064
065    public IntegerPrimitive( final byte aValue )
066    {
067        this((int)aValue);
068    }
069
070    public IntegerPrimitive( final char aValue )
071    {
072        this((int)aValue);
073    }
074
075    public IntegerPrimitive( final double aValue )
076    {
077        this((int)aValue);
078    }
079
080    public IntegerPrimitive( final float aValue )
081    {
082        this((int)aValue);
083    }
084
085    public IntegerPrimitive( final int aValue )
086    {
087        super();
088        this._theValue = aValue;
089    }
090
091    public IntegerPrimitive( final long aValue )
092    {
093        this((int)aValue);
094    }
095
096    public IntegerPrimitive( final SealedScalar<?> aValue )
097    {
098        this(aValue.intValue());
099    }
100
101    public IntegerPrimitive( final short aValue )
102    {
103        this((int)aValue);
104    }
105
106    public IntegerPrimitive( final String aValue )
107    {
108        this();
109        To.SCALAR.reactToText(aValue, this);
110    }
111
112    @Override
113    public final IntegerPrimitive and( final int aValue )
114        throws UnsupportedOperationException,
115            IllegalArgumentException,
116            ArithmeticException
117    {
118        this._theValue &= aValue;
119        return this;
120    }
121
122    @Override
123    public final IntegerPrimitive copy()
124    {
125        return new IntegerPrimitive(this._theValue);
126    }
127
128    @Override
129    public final IntegerPrimitive copyUsing( final int aValue )
130    {
131        return new IntegerPrimitive(aValue);
132    }
133
134    @Override
135    public IntegerPrimitive decrement()
136        throws UnsupportedOperationException,
137            IllegalArgumentException,
138            ArithmeticException
139    {
140        --this._theValue;
141        return this;
142    }
143
144    @Override
145    public final IntegerPrimitive difference( final int aValue )
146        throws UnsupportedOperationException,
147            IllegalArgumentException,
148            ArithmeticException
149    {
150        this._theValue -= aValue;
151        return this;
152    }
153
154    @Override
155    public final ConcurrencyStrategy getConcurrency()
156    {
157        return ConcurrencyStrategy.SEQUENTIAL;
158    }
159
160    @Override
161    public IntegerPrimitive increment()
162        throws UnsupportedOperationException,
163            ArithmeticException,
164            IllegalArgumentException
165    {
166        ++this._theValue;
167        return this;
168    }
169
170    @Override
171    public final int intPostDecrement()
172        throws UnsupportedOperationException,
173            ArithmeticException,
174            IllegalArgumentException
175    {
176        return this._theValue--;
177    }
178
179    @Override
180    public final int intPostIncrement()
181        throws UnsupportedOperationException,
182            ArithmeticException,
183            IllegalArgumentException
184    {
185        return this._theValue++;
186    }
187
188    @Override
189    public final int intValue()
190    {
191        return this._theValue;
192    }
193
194    @Override
195    public final IntegerPrimitive mod( final int aValue )
196        throws UnsupportedOperationException,
197            IllegalArgumentException,
198            ArithmeticException
199    {
200        this._theValue %= aValue;
201        return this;
202    }
203
204    @Override
205    public final IntegerPrimitive not()
206        throws UnsupportedOperationException,
207            IllegalArgumentException,
208            ArithmeticException
209    {
210        this._theValue ^= -1;
211        return this;
212    }
213
214    @Override
215    public final IntegerPrimitive or( final int aValue )
216        throws UnsupportedOperationException,
217            IllegalArgumentException,
218            ArithmeticException
219    {
220        this._theValue |= aValue;
221        return this;
222    }
223
224    @Override
225    public final IntegerPrimitive product( final int aValue )
226        throws UnsupportedOperationException,
227            IllegalArgumentException,
228            ArithmeticException
229    {
230        this._theValue *= aValue;
231        return this;
232    }
233
234    @Override
235    public final IntegerPrimitive quotient( final int aValue )
236        throws UnsupportedOperationException,
237            IllegalArgumentException,
238            ArithmeticException
239    {
240        this._theValue /= aValue;
241        return this;
242    }
243
244    @Override
245    public final IntegerPrimitive setScalar( final int aValue )
246        throws UnsupportedOperationException,
247            IllegalArgumentException,
248            ArithmeticException
249    {
250        this._theValue = aValue;
251        return this;
252    }
253
254    @Override
255    public final IntegerPrimitive shiftLeft( final int count )
256        throws UnsupportedOperationException,
257            IllegalArgumentException,
258            ArithmeticException
259    {
260        this._theValue <<= count;
261        return this;
262    }
263
264    @Override
265    public final IntegerPrimitive shiftRight( final int count )
266        throws UnsupportedOperationException,
267            IllegalArgumentException,
268            ArithmeticException
269    {
270        this._theValue >>= count;
271        return this;
272    }
273
274    @Override
275    public final IntegerPrimitive shiftRightExtendZero( final int count )
276        throws UnsupportedOperationException,
277            IllegalArgumentException,
278            ArithmeticException
279    {
280        this._theValue >>>= count;
281        return this;
282    }
283
284    @Override
285    public final IntegerPrimitive sum( final int aValue )
286        throws UnsupportedOperationException,
287            IllegalArgumentException,
288            ArithmeticException
289    {
290        this._theValue += aValue;
291        return this;
292    }
293
294    @Override
295    public final IntegerPrimitive xor( final int aValue )
296        throws UnsupportedOperationException,
297            IllegalArgumentException,
298            ArithmeticException
299    {
300        this._theValue ^= aValue;
301        return this;
302    }
303}