001/**
002 * BooleanArrayCursor.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.booleans;
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 boolean} 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 BooleanArrayCursor
039    extends
040        AbstractBooleanPrimitive<BooleanArrayCursor>
041{
042    private static final long serialVersionUID = -4340680759559160007L;
043
044    private final int _theKey;
045    private final boolean[] _theSuperstructure;
046
047    public BooleanArrayCursor()
048    {
049        this(new boolean[0]);
050    }
051
052    public BooleanArrayCursor( final boolean[] aSuperstructure )
053    {
054        this(aSuperstructure, 0);
055    }
056
057    public BooleanArrayCursor( final boolean[] aSuperstructure, final int aKey )
058    {
059        this._theSuperstructure = aSuperstructure;
060        this._theKey = aKey;
061    }
062
063    @Override
064    public final BooleanArrayCursor and( final boolean 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 boolean booleanValue()
075    {
076        return this._theSuperstructure[this._theKey];
077    }
078
079    @Override
080    public final BooleanArrayCursor copy()
081    {
082        return new BooleanArrayCursor(this._theSuperstructure, this._theKey);
083    }
084
085    /**
086     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
087     * type, using the same source domain, but keyed using the value specified
088     * 
089     * @param aKey
090     *        a new cursor
091     * @return a copy (clone) of this cursor initialized using same source
092     *         domain and the specified key
093     */
094    @Override
095    public final BooleanArrayCursor copyUsing( final BigDecimal aKey )
096    {
097        return this.copyUsing(aKey.intValue());
098    }
099
100    /**
101     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
102     * type, using the same source domain, but keyed using the value specified
103     * 
104     * @param aKey
105     *        a new cursor
106     * @return a copy (clone) of this cursor initialized using same source
107     *         domain and the specified key
108     */
109    @Override
110    public final BooleanArrayCursor copyUsing( final BigInteger aKey )
111    {
112        return this.copyUsing(aKey.intValue());
113    }
114
115    /**
116     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
117     * type, using the same source domain, but keyed using the value specified
118     * 
119     * @param aKey
120     *        a new cursor
121     * @return a copy (clone) of this cursor initialized using same source
122     *         domain and the specified key
123     */
124    @Override
125    public final BooleanArrayCursor copyUsing( final boolean aKey )
126    {
127        return this.copyUsing(To.intValue(aKey));
128    }
129
130    /**
131     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
132     * type, using the same source domain, but keyed using the value specified
133     * 
134     * @param aKey
135     *        a new cursor
136     * @return a copy (clone) of this cursor initialized using same source
137     *         domain and the specified key
138     */
139    @Override
140    public final BooleanArrayCursor copyUsing( final byte aKey )
141    {
142        return this.copyUsing((int)aKey);
143    }
144
145    /**
146     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
147     * type, using the same source domain, but keyed using the value specified
148     * 
149     * @param aKey
150     *        a new cursor
151     * @return a copy (clone) of this cursor initialized using same source
152     *         domain and the specified key
153     */
154    @Override
155    public final BooleanArrayCursor copyUsing( final char aKey )
156    {
157        return this.copyUsing((int)aKey);
158    }
159
160    /**
161     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
162     * type, using the same source domain, but keyed using the value specified
163     * 
164     * @param aKey
165     *        a new cursor
166     * @return a copy (clone) of this cursor initialized using same source
167     *         domain and the specified key
168     */
169    @Override
170    public final BooleanArrayCursor copyUsing( final double aKey )
171    {
172        return this.copyUsing((int)aKey);
173    }
174
175    /**
176     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
177     * type, using the same source domain, but keyed using the value specified
178     * 
179     * @param aKey
180     *        a new cursor
181     * @return a copy (clone) of this cursor initialized using same source
182     *         domain and the specified key
183     */
184    @Override
185    public final BooleanArrayCursor copyUsing( final float aKey )
186    {
187        return this.copyUsing((int)aKey);
188    }
189
190    /**
191     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
192     * type, using the same source domain, but keyed using the value specified
193     * 
194     * @param aKey
195     *        a new cursor
196     * @return a copy (clone) of this cursor initialized using same source
197     *         domain and the specified key
198     */
199    @Override
200    public final BooleanArrayCursor copyUsing( final int aKey )
201    {
202        return new BooleanArrayCursor(this._theSuperstructure, aKey);
203    }
204
205    /**
206     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
207     * type, using the same source domain, but keyed using the value specified
208     * 
209     * @param aKey
210     *        a new cursor
211     * @return a copy (clone) of this cursor initialized using same source
212     *         domain and the specified key
213     */
214    @Override
215    public final BooleanArrayCursor copyUsing( final long aKey )
216    {
217        return this.copyUsing((int)aKey);
218    }
219
220    /**
221     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
222     * type, using the same source domain, but keyed using the value specified
223     * 
224     * @param aKey
225     *        a new cursor
226     * @return a copy (clone) of this cursor initialized using same source
227     *         domain and the specified key
228     */
229    @Override
230    public final BooleanArrayCursor copyUsing( final short aKey )
231    {
232        return this.copyUsing((int)aKey);
233    }
234
235    /**
236     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
237     * type, using the same source domain, but keyed using the value specified
238     * 
239     * @param aKey
240     *        a new cursor
241     * @return a copy (clone) of this cursor initialized using same source
242     *         domain and the specified key
243     */
244    @Override
245    public final BooleanArrayCursor copyUsingPrimitive(
246        final SealedPrimitive<?> aKey )
247    {
248        return this.copyUsing(aKey.intValue());
249    }
250
251    /**
252     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
253     * type, using the same source domain, but keyed using the value specified
254     * 
255     * @param aKey
256     *        a new cursor
257     * @return a copy (clone) of this cursor initialized using same source
258     *         domain and the specified key
259     */
260    @Override
261    public final BooleanArrayCursor copyUsingReal( final SealedReal<?> aKey )
262    {
263        return this.copyUsing(aKey.intValue());
264    }
265
266    /**
267     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
268     * type, using the same source domain, but keyed using the value specified
269     * 
270     * @param aKey
271     *        a new cursor
272     * @return a copy (clone) of this cursor initialized using same source
273     *         domain and the specified key
274     */
275    @Override
276    public final BooleanArrayCursor copyUsingScalar( final SealedScalar<?> aKey )
277    {
278        return this.copyUsing(aKey.intValue());
279    }
280
281    /**
282     * Using this {@link Cursor} as a factory, produce a new Cursor of the same
283     * type, using the same source domain, but keyed using the value specified
284     * 
285     * @param aKey
286     *        a new cursor
287     * @return a copy (clone) of this cursor initialized using same source
288     *         domain and the specified key
289     */
290    @Override
291    public final BooleanArrayCursor copyUsingText( final CharSequence aKey )
292    {
293        return super.copyUsingText(aKey);
294    }
295
296    @Override
297    public final ConcurrencyStrategy getConcurrency()
298    {
299        return ConcurrencyStrategy.SEQUENTIAL;
300    }
301
302    @Override
303    public final BooleanArrayCursor not()
304        throws UnsupportedOperationException,
305            IllegalArgumentException,
306            ArithmeticException
307    {
308        this._theSuperstructure[this._theKey] ^= true;
309        return this;
310    }
311
312    @Override
313    public final BooleanArrayCursor or( final boolean aValue )
314        throws UnsupportedOperationException,
315            IllegalArgumentException,
316            ArithmeticException
317    {
318        this._theSuperstructure[this._theKey] |= aValue;
319        return this;
320    }
321
322    @Override
323    public final BooleanArrayCursor setScalar( final boolean aValue )
324        throws UnsupportedOperationException,
325            IllegalArgumentException,
326            ArithmeticException
327    {
328        this._theSuperstructure[this._theKey] = aValue;
329        return this;
330    }
331
332    @Override
333    public final BooleanArrayCursor xor( final boolean aValue )
334        throws UnsupportedOperationException,
335            IllegalArgumentException,
336            ArithmeticException
337    {
338        this._theSuperstructure[this._theKey] ^= aValue;
339        return this;
340    }
341}