001/**
002 * ByteArrayCursor.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.bytes;
020
021import java.awt.Cursor;
022import java.math.BigDecimal;
023import java.math.BigInteger;
024
025import net.sf.jaccumulator.ConcurrencyStrategy;
026import net.sf.jaccumulator.lex.To;
027import net.sf.jaccumulator.primitives.SealedPrimitive;
028import net.sf.jaccumulator.reals.SealedReal;
029import net.sf.jaccumulator.scalars.SealedScalar;
030
031/**
032 * A {@code byte} fixed array cursor
033 * 
034 * @since JAccumulator 4.0
035 * @author Nicole Tedesco (<a
036 *         href="mailto:Nicole@NicoleTedesco.com">Nicole@NicoleTedesco.com</a>)
037 */
038public final class ByteArrayCursor
039    extends
040        AbstractBytePrimitive<ByteArrayCursor>
041{
042    private static final long serialVersionUID = -3613279034020619630L;
043
044    private final int _theKey;
045    private final byte[] _theSuperstructure;
046
047    public ByteArrayCursor()
048    {
049        this(new byte[0]);
050    }
051
052    public ByteArrayCursor( final byte[] aSuperstructure )
053    {
054        this(aSuperstructure, 0);
055    }
056
057    public ByteArrayCursor( final byte[] aSuperstructure, final int aKey )
058    {
059        this._theSuperstructure = aSuperstructure;
060        this._theKey = aKey;
061    }
062
063    @Override
064    public final ByteArrayCursor and( final byte aValue )
065        throws UnsupportedOperationException,
066            IllegalArgumentException,
067            ArithmeticException
068    {
069        this._theSuperstructure[this._theKey] &= aValue;
070        return this;
071    }
072
073    @Override
074    public final byte bytePostDecrement()
075        throws UnsupportedOperationException,
076            ArithmeticException,
077            IllegalArgumentException
078    {
079        return this._theSuperstructure[this._theKey]--;
080    }
081
082    @Override
083    public final byte bytePostIncrement()
084        throws UnsupportedOperationException,
085            ArithmeticException,
086            IllegalArgumentException
087    {
088        return this._theSuperstructure[this._theKey]++;
089    }
090
091    @Override
092    public final byte byteValue()
093    {
094        return this._theSuperstructure[this._theKey];
095    }
096
097    @Override
098    public final ByteArrayCursor copy()
099    {
100        return new ByteArrayCursor(this._theSuperstructure, this._theKey);
101    }
102
103    /**
104     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
105     * type, using the same source domain, but keyed using the value specified
106     * 
107     * @param aKey
108     *        a new cursor
109     * @return a copy (clone) of this cursor initialized using same source
110     *         domain and the specified key
111     */
112    @Override
113    public final ByteArrayCursor copyUsing( final BigDecimal aKey )
114    {
115        return this.copyUsing(aKey.intValue());
116    }
117
118    /**
119     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
120     * type, using the same source domain, but keyed using the value specified
121     * 
122     * @param aKey
123     *        a new cursor
124     * @return a copy (clone) of this cursor initialized using same source
125     *         domain and the specified key
126     */
127    @Override
128    public final ByteArrayCursor copyUsing( final BigInteger aKey )
129    {
130        return this.copyUsing(aKey.intValue());
131    }
132
133    /**
134     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
135     * type, using the same source domain, but keyed using the value specified
136     * 
137     * @param aKey
138     *        a new cursor
139     * @return a copy (clone) of this cursor initialized using same source
140     *         domain and the specified key
141     */
142    @Override
143    public final ByteArrayCursor copyUsing( final boolean aKey )
144    {
145        return this.copyUsing(To.intValue(aKey));
146    }
147
148    /**
149     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
150     * type, using the same source domain, but keyed using the value specified
151     * 
152     * @param aKey
153     *        a new cursor
154     * @return a copy (clone) of this cursor initialized using same source
155     *         domain and the specified key
156     */
157    @Override
158    public final ByteArrayCursor copyUsing( final byte aKey )
159    {
160        return this.copyUsing((int)aKey);
161    }
162
163    /**
164     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
165     * type, using the same source domain, but keyed using the value specified
166     * 
167     * @param aKey
168     *        a new cursor
169     * @return a copy (clone) of this cursor initialized using same source
170     *         domain and the specified key
171     */
172    @Override
173    public final ByteArrayCursor copyUsing( final char aKey )
174    {
175        return this.copyUsing((int)aKey);
176    }
177
178    /**
179     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
180     * type, using the same source domain, but keyed using the value specified
181     * 
182     * @param aKey
183     *        a new cursor
184     * @return a copy (clone) of this cursor initialized using same source
185     *         domain and the specified key
186     */
187    @Override
188    public final ByteArrayCursor copyUsing( final double aKey )
189    {
190        return this.copyUsing((int)aKey);
191    }
192
193    /**
194     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
195     * type, using the same source domain, but keyed using the value specified
196     * 
197     * @param aKey
198     *        a new cursor
199     * @return a copy (clone) of this cursor initialized using same source
200     *         domain and the specified key
201     */
202    @Override
203    public final ByteArrayCursor copyUsing( final float aKey )
204    {
205        return this.copyUsing((int)aKey);
206    }
207
208    /**
209     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
210     * type, using the same source domain, but keyed using the value specified
211     * 
212     * @param aKey
213     *        a new cursor
214     * @return a copy (clone) of this cursor initialized using same source
215     *         domain and the specified key
216     */
217    @Override
218    public final ByteArrayCursor copyUsing( final int aKey )
219    {
220        return new ByteArrayCursor(this._theSuperstructure, aKey);
221    }
222
223    /**
224     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
225     * type, using the same source domain, but keyed using the value specified
226     * 
227     * @param aKey
228     *        a new cursor
229     * @return a copy (clone) of this cursor initialized using same source
230     *         domain and the specified key
231     */
232    @Override
233    public final ByteArrayCursor copyUsing( final long aKey )
234    {
235        return this.copyUsing((int)aKey);
236    }
237
238    /**
239     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
240     * type, using the same source domain, but keyed using the value specified
241     * 
242     * @param aKey
243     *        a new cursor
244     * @return a copy (clone) of this cursor initialized using same source
245     *         domain and the specified key
246     */
247    @Override
248    public final ByteArrayCursor copyUsing( final short aKey )
249    {
250        return this.copyUsing((int)aKey);
251    }
252
253    /**
254     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
255     * type, using the same source domain, but keyed using the value specified
256     * 
257     * @param aKey
258     *        a new cursor
259     * @return a copy (clone) of this cursor initialized using same source
260     *         domain and the specified key
261     */
262    @Override
263    public final ByteArrayCursor copyUsingPrimitive(
264        final SealedPrimitive<?> aKey )
265    {
266        return this.copyUsing(aKey.intValue());
267    }
268
269    /**
270     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
271     * type, using the same source domain, but keyed using the value specified
272     * 
273     * @param aKey
274     *        a new cursor
275     * @return a copy (clone) of this cursor initialized using same source
276     *         domain and the specified key
277     */
278    @Override
279    public final ByteArrayCursor copyUsingReal( final SealedReal<?> aKey )
280    {
281        return this.copyUsing(aKey.intValue());
282    }
283
284    /**
285     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
286     * type, using the same source domain, but keyed using the value specified
287     * 
288     * @param aKey
289     *        a new cursor
290     * @return a copy (clone) of this cursor initialized using same source
291     *         domain and the specified key
292     */
293    @Override
294    public final ByteArrayCursor copyUsingScalar( final SealedScalar<?> aKey )
295    {
296        return this.copyUsing(aKey.intValue());
297    }
298
299    /**
300     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
301     * type, using the same source domain, but keyed using the value specified
302     * 
303     * @param aKey
304     *        a new cursor
305     * @return a copy (clone) of this cursor initialized using same source
306     *         domain and the specified key
307     */
308    @Override
309    public final ByteArrayCursor copyUsingText( final CharSequence aKey )
310    {
311        return super.copyUsingText(aKey);
312    }
313
314    @Override
315    public final ByteArrayCursor decrement()
316        throws UnsupportedOperationException,
317            ArithmeticException,
318            IllegalArgumentException
319    {
320        --this._theSuperstructure[this._theKey];
321        return this;
322    }
323
324    @Override
325    public final ByteArrayCursor difference( final byte aValue )
326        throws UnsupportedOperationException,
327            IllegalArgumentException,
328            ArithmeticException
329    {
330        this._theSuperstructure[this._theKey] -= aValue;
331        return this;
332    }
333
334    @Override
335    public final ConcurrencyStrategy getConcurrency()
336    {
337        return ConcurrencyStrategy.SEQUENTIAL;
338    }
339
340    @Override
341    public final ByteArrayCursor increment()
342        throws UnsupportedOperationException,
343            ArithmeticException,
344            IllegalArgumentException
345    {
346        ++this._theSuperstructure[this._theKey];
347        return this;
348    }
349
350    @Override
351    public final ByteArrayCursor mod( final byte aValue )
352        throws UnsupportedOperationException,
353            IllegalArgumentException,
354            ArithmeticException
355    {
356        this._theSuperstructure[this._theKey] %= aValue;
357        return this;
358    }
359
360    @Override
361    public final ByteArrayCursor not()
362        throws UnsupportedOperationException,
363            IllegalArgumentException,
364            ArithmeticException
365    {
366        this._theSuperstructure[this._theKey] ^= (byte)-1;
367        return this;
368    }
369
370    @Override
371    public final ByteArrayCursor or( final byte aValue )
372        throws UnsupportedOperationException,
373            IllegalArgumentException,
374            ArithmeticException
375    {
376        this._theSuperstructure[this._theKey] |= aValue;
377        return this;
378    }
379
380    @Override
381    public final ByteArrayCursor product( final byte aValue )
382        throws UnsupportedOperationException,
383            IllegalArgumentException,
384            ArithmeticException
385    {
386        this._theSuperstructure[this._theKey] *= aValue;
387        return this;
388    }
389
390    @Override
391    public final ByteArrayCursor quotient( final byte aValue )
392        throws UnsupportedOperationException,
393            IllegalArgumentException,
394            ArithmeticException
395    {
396        this._theSuperstructure[this._theKey] /= aValue;
397        return this;
398    }
399
400    @Override
401    public final ByteArrayCursor setScalar( final byte aValue )
402        throws UnsupportedOperationException,
403            IllegalArgumentException,
404            ArithmeticException
405    {
406        this._theSuperstructure[this._theKey] = aValue;
407        return this;
408    }
409
410    @Override
411    public final ByteArrayCursor shiftLeft( final int count )
412        throws UnsupportedOperationException,
413            IllegalArgumentException,
414            ArithmeticException
415    {
416        this._theSuperstructure[this._theKey] <<= count;
417        return this;
418    }
419
420    @Override
421    public final ByteArrayCursor shiftRight( final int count )
422        throws UnsupportedOperationException,
423            IllegalArgumentException,
424            ArithmeticException
425    {
426        this._theSuperstructure[this._theKey] >>= count;
427        return this;
428    }
429
430    @Override
431    public final ByteArrayCursor shiftRightExtendZero( final int count )
432        throws UnsupportedOperationException,
433            IllegalArgumentException,
434            ArithmeticException
435    {
436        this._theSuperstructure[this._theKey] >>>= count;
437        return this;
438    }
439
440    @Override
441    public final ByteArrayCursor sum( final byte aValue )
442        throws UnsupportedOperationException,
443            IllegalArgumentException,
444            ArithmeticException
445    {
446        this._theSuperstructure[this._theKey] += aValue;
447        return this;
448    }
449
450    @Override
451    public final ByteArrayCursor xor( final byte aValue )
452        throws UnsupportedOperationException,
453            IllegalArgumentException,
454            ArithmeticException
455    {
456        this._theSuperstructure[this._theKey] ^= aValue;
457        return this;
458    }
459}