001/**
002 * AbstractBytePrimitive.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.math.BigDecimal;
022import java.math.BigInteger;
023import java.util.NoSuchElementException;
024
025import net.sf.jaccumulator.Domain;
026import net.sf.jaccumulator.StructureStrategy;
027import net.sf.jaccumulator.lex.To;
028import net.sf.jaccumulator.primitives.AbstractNumericPrimitive;
029import net.sf.jaccumulator.primitives.Constant;
030import net.sf.jaccumulator.primitives.Primitive;
031import net.sf.jaccumulator.primitives.SealedPrimitive;
032import net.sf.jaccumulator.reals.Real;
033import net.sf.jaccumulator.reals.SealedReal;
034import net.sf.jaccumulator.scalars.NumericPrecision;
035import net.sf.jaccumulator.scalars.RoundingStrategy;
036import net.sf.jaccumulator.scalars.Scalar;
037import net.sf.jaccumulator.scalars.SealedScalar;
038
039/**
040 * Abstract {@code byte} precision {@link Primitive primitive} implementation
041 * 
042 * @param <PRIMITIVE>
043 *        this primitive type (used to facilitate operation chaining on write
044 *        operations)
045 * @since JAccumulator 4.0
046 * @author Nicole Tedesco (<a
047 *         href="mailto:Nicole@NicoleTedesco.com">Nicole@NicoleTedesco.com</a>)
048 */
049public abstract class AbstractBytePrimitive<PRIMITIVE extends Primitive<PRIMITIVE>>
050    extends
051        AbstractNumericPrimitive<PRIMITIVE>
052{
053    private static final long serialVersionUID = 8080497188206480415L;
054
055    public AbstractBytePrimitive()
056    {
057        super(NumericPrecision.BYTE);
058    }
059
060    public AbstractBytePrimitive( final Domain aDomain )
061    {
062        super(aDomain, NumericPrecision.BYTE);
063    }
064
065    @Override
066    public final PRIMITIVE absoluteValue()
067        throws UnsupportedOperationException,
068            ArithmeticException,
069            IllegalArgumentException
070    {
071        return this.setScalar(Math.abs(this.intValue()));
072    }
073
074    @Override
075    public final PRIMITIVE and( final boolean aValue )
076        throws UnsupportedOperationException,
077            IllegalArgumentException,
078            ArithmeticException
079    {
080        return this.and(To.byteValue(aValue));
081    }
082
083    @Override
084    public final boolean booleanPostDecrement()
085        throws UnsupportedOperationException,
086            ArithmeticException,
087            IllegalArgumentException
088    {
089        return To.booleanValue(this.bytePostDecrement());
090    }
091
092    @Override
093    public final boolean booleanPostIncrement()
094        throws UnsupportedOperationException,
095            ArithmeticException,
096            IllegalArgumentException
097    {
098        return To.booleanValue(this.bytePostIncrement());
099    }
100
101    @Override
102    public final boolean booleanValue()
103    {
104        return To.booleanValue(this.byteValue());
105    }
106
107    @Override
108    public final char charPostDecrement()
109        throws UnsupportedOperationException,
110            ArithmeticException,
111            IllegalArgumentException
112    {
113        return (char)this.bytePostDecrement();
114    }
115
116    @Override
117    public final char charPostIncrement()
118        throws UnsupportedOperationException,
119            ArithmeticException,
120            IllegalArgumentException
121    {
122        return (char)this.bytePostIncrement();
123    }
124
125    @Override
126    public final char charValue()
127    {
128        return (char)this.byteValue();
129    }
130
131    @Override
132    public PRIMITIVE copyUsing( final BigDecimal aValue )
133    {
134        return this.copyUsing(aValue.byteValue());
135    }
136
137    @Override
138    public PRIMITIVE copyUsing( final BigInteger aValue )
139    {
140        return this.copyUsing(aValue.byteValue());
141    }
142
143    @Override
144    public PRIMITIVE copyUsing( final boolean aValue )
145    {
146        return this.copyUsing(To.byteValue(aValue));
147    }
148
149    @Override
150    public PRIMITIVE copyUsing( final char aValue )
151    {
152        return this.copyUsing((byte)aValue);
153    }
154
155    @Override
156    public PRIMITIVE copyUsing( final double aValue )
157    {
158        return this.copyUsing((byte)aValue);
159    }
160
161    @Override
162    public PRIMITIVE copyUsing( final float aValue )
163    {
164        return this.copyUsing((byte)aValue);
165    }
166
167    @Override
168    public PRIMITIVE copyUsing( final int aValue )
169    {
170        return this.copyUsing((byte)aValue);
171    }
172
173    @Override
174    public PRIMITIVE copyUsing( final long aValue )
175    {
176        return this.copyUsing((byte)aValue);
177    }
178
179    @Override
180    public PRIMITIVE copyUsing( final short aValue )
181    {
182        return this.copyUsing((byte)aValue);
183    }
184
185    @Override
186    public PRIMITIVE copyUsingPrimitive( final SealedPrimitive<?> aValue )
187    {
188        return this.copyUsing(aValue.byteValue());
189    }
190
191    @Override
192    public PRIMITIVE copyUsingReal( final SealedReal<?> aValue )
193    {
194        return this.copyUsing(aValue.byteValue());
195    }
196
197    @Override
198    public PRIMITIVE copyUsingScalar( final SealedScalar<?> aValue )
199    {
200        return this.copyUsing(aValue.byteValue());
201    }
202
203    @Override
204    public final PRIMITIVE cube()
205        throws UnsupportedOperationException,
206            IllegalArgumentException,
207            ArithmeticException
208    {
209        final byte myValue = this.byteValue();
210        return this.setScalar(myValue * myValue * myValue);
211    }
212
213    @Override
214    public final PRIMITIVE difference( final boolean aValue )
215        throws UnsupportedOperationException,
216            IllegalArgumentException,
217            ArithmeticException
218    {
219        return this.difference(To.byteValue(aValue));
220    }
221
222    @Override
223    public final double doublePostDecrement()
224        throws UnsupportedOperationException,
225            ArithmeticException,
226            IllegalArgumentException
227    {
228        return this.bytePostDecrement();
229    }
230
231    @Override
232    public final double doublePostIncrement()
233        throws UnsupportedOperationException,
234            ArithmeticException,
235            IllegalArgumentException
236    {
237        return this.bytePostIncrement();
238    }
239
240    @Override
241    public final double doubleValue()
242    {
243        return this.byteValue();
244    }
245
246    @Override
247    public final float floatPostDecrement()
248        throws UnsupportedOperationException,
249            ArithmeticException,
250            IllegalArgumentException
251    {
252        return this.bytePostDecrement();
253    }
254
255    @Override
256    public final float floatPostIncrement()
257        throws UnsupportedOperationException,
258            ArithmeticException,
259            IllegalArgumentException
260    {
261        return this.bytePostIncrement();
262    }
263
264    @Override
265    public final float floatValue()
266    {
267        return this.byteValue();
268    }
269
270    @Override
271    public final StructureStrategy getStructureStrategy()
272    {
273        return StructureStrategy.BITS;
274    }
275
276    @Override
277    public final int hashCode()
278    {
279        return this.byteValue();
280    }
281
282    @Override
283    @SuppressWarnings( "unchecked" )
284    public final <S extends Scalar<?>> S inducePostDecrement( final S aTarget )
285        throws NullPointerException,
286            NoSuchElementException,
287            UnsupportedOperationException,
288            IllegalStateException
289    {
290        return (S)aTarget.setScalar(this.bytePostDecrement());
291    }
292
293    @Override
294    @SuppressWarnings( "unchecked" )
295    public final <S extends Scalar<?>> S inducePostIncrement( final S aTarget )
296        throws NullPointerException,
297            NoSuchElementException,
298            UnsupportedOperationException,
299            IllegalStateException
300    {
301        return (S)aTarget.setScalar(this.bytePostIncrement());
302    }
303
304    @Override
305    public final <REAL extends Real<?>> REAL induceRealMaximum(
306        final REAL aTarget )
307        throws NullPointerException,
308            NoSuchElementException,
309            UnsupportedOperationException,
310            IllegalStateException
311    {
312        return this.induceScalarMaximum(aTarget);
313    }
314
315    @Override
316    public final <REAL extends Real<?>> REAL induceRealMinimum(
317        final REAL aTarget )
318        throws NullPointerException,
319            NoSuchElementException,
320            UnsupportedOperationException,
321            IllegalStateException
322    {
323        return this.induceScalarMinimum(aTarget);
324    }
325
326    @Override
327    public final <REAL extends Real<?>> REAL induceRealValue( final REAL aTarget )
328        throws NullPointerException,
329            NoSuchElementException,
330            UnsupportedOperationException,
331            IllegalStateException
332    {
333        return this.induceScalarValue(aTarget);
334    }
335
336    @Override
337    @SuppressWarnings( "unchecked" )
338    public final <S extends Scalar<?>> S induceScalarMaximum( final S aTarget )
339    {
340        return (S)aTarget.setScalar(Byte.MAX_VALUE);
341    }
342
343    @Override
344    @SuppressWarnings( "unchecked" )
345    public final <S extends Scalar<?>> S induceScalarMinimum( final S aTarget )
346    {
347        return (S)aTarget.setScalar(Byte.MIN_VALUE);
348    }
349
350    @Override
351    @SuppressWarnings( "unchecked" )
352    public final <S extends Scalar<?>> S induceScalarValue( final S aTarget )
353    {
354        return (S)aTarget.setScalar(this.byteValue());
355    }
356
357    @Override
358    public final int intPostDecrement()
359        throws UnsupportedOperationException,
360            ArithmeticException,
361            IllegalArgumentException
362    {
363        return this.bytePostDecrement();
364    }
365
366    @Override
367    public final int intPostIncrement()
368        throws UnsupportedOperationException,
369            ArithmeticException,
370            IllegalArgumentException
371    {
372        return this.bytePostIncrement();
373    }
374
375    @Override
376    public final int intValue()
377    {
378        return this.byteValue();
379    }
380
381    @Override
382    public final PRIMITIVE inverse()
383        throws UnsupportedOperationException,
384            ArithmeticException,
385            IllegalArgumentException
386    {
387        if (this.isZero()) return this.quotient(0);
388        return this.setUnity();
389    }
390
391    @Override
392    public final boolean isDigit()
393    {
394        return Character.isDigit(this.charValue());
395    }
396
397    @Override
398    public final boolean isFinite()
399    {
400        return true;
401    }
402
403    @Override
404    public final boolean isIdentifierIgnorable()
405    {
406        return Character.isIdentifierIgnorable(this.charValue());
407    }
408
409    @Override
410    public final boolean isInfinity()
411    {
412        return false;
413    }
414
415    @Override
416    public final boolean isInvalid()
417    {
418        return false;
419    }
420
421    @Override
422    public final boolean isISOControl()
423    {
424        return Character.isISOControl(this.charValue());
425    }
426
427    @Override
428    public final boolean isJavaIdentifierPart()
429    {
430        return Character.isJavaIdentifierPart(this.charValue());
431    }
432
433    @Override
434    public final boolean isJavaIdentifierStart()
435    {
436        return Character.isJavaIdentifierStart(this.charValue());
437    }
438
439    @Override
440    public final boolean isLetter()
441    {
442        return Character.isLetter(this.charValue());
443    }
444
445    @Override
446    public final boolean isLetterOrDigit()
447    {
448        return Character.isLetterOrDigit(this.charValue());
449    }
450
451    @Override
452    public final boolean isLowerCase()
453    {
454        return Character.isLowerCase(this.charValue());
455    }
456
457    @Override
458    public final boolean isMaximum()
459    {
460        return this.byteValue() == Byte.MAX_VALUE;
461    }
462
463    @Override
464    public final boolean isMinimum()
465    {
466        return this.byteValue() == Byte.MIN_VALUE;
467    }
468
469    @Override
470    public final boolean isMirrored()
471    {
472        return Character.isMirrored(this.charValue());
473    }
474
475    @Override
476    public final boolean isModulo()
477    {
478        return false;
479    }
480
481    @Override
482    public final boolean isNegative()
483    {
484        return this.byteValue() < (byte)0;
485    }
486
487    @Override
488    public final boolean isNegativeFinite()
489    {
490        return this.isNegative();
491    }
492
493    @Override
494    public final boolean isNegativeInfinity()
495    {
496        return false;
497    }
498
499    @Override
500    public final boolean isPositive()
501    {
502        return (byte)0 < this.byteValue();
503    }
504
505    @Override
506    public final boolean isPositiveFinite()
507    {
508        return this.isPositive();
509    }
510
511    @Override
512    public final boolean isPositiveInfinity()
513    {
514        return false;
515    }
516
517    @Override
518    public final boolean isReal()
519    {
520        return true;
521    }
522
523    @Override
524    public final boolean isTitleCase()
525    {
526        return Character.isTitleCase(this.charValue());
527    }
528
529    @Override
530    public final boolean isUnicode()
531    {
532        return Character.isDefined(this.charValue());
533    }
534
535    @Override
536    public final boolean isUnicodeIdentifierPart()
537    {
538        return Character.isUnicodeIdentifierPart(this.charValue());
539    }
540
541    @Override
542    public final boolean isUnicodeIdentifierStart()
543    {
544        return Character.isUnicodeIdentifierStart(this.charValue());
545    }
546
547    @Override
548    public final boolean isUnity()
549    {
550        return this.byteValue() == (byte)1;
551    }
552
553    @Override
554    public final boolean isUpperCase()
555    {
556        return Character.isUpperCase(this.charValue());
557    }
558
559    @Override
560    public final boolean isWhitespace()
561    {
562        return Character.isWhitespace(this.charValue());
563    }
564
565    @Override
566    public final boolean isZero()
567    {
568        return this.byteValue() == (byte)0;
569    }
570
571    @Override
572    public final long longPostDecrement()
573        throws UnsupportedOperationException,
574            ArithmeticException,
575            IllegalArgumentException
576    {
577        return this.bytePostDecrement();
578    }
579
580    @Override
581    public final long longPostIncrement()
582        throws UnsupportedOperationException,
583            ArithmeticException,
584            IllegalArgumentException
585    {
586        return this.bytePostIncrement();
587    }
588
589    @Override
590    public final long longValue()
591    {
592        return this.byteValue();
593    }
594
595    @Override
596    public final PRIMITIVE lowerCase()
597    {
598        return this.setScalar(Character.toLowerCase(this.charValue()));
599    }
600
601    @Override
602    public final PRIMITIVE negate()
603        throws UnsupportedOperationException,
604            ArithmeticException,
605            IllegalArgumentException
606    {
607        return this.setScalar(-this.byteValue());
608    }
609
610    @Override
611    public final PRIMITIVE or( final boolean aValue )
612        throws UnsupportedOperationException,
613            IllegalArgumentException,
614            ArithmeticException
615    {
616        return this.or(To.byteValue(aValue));
617    }
618
619    @Override
620    public final PRIMITIVE product( final boolean aValue )
621        throws UnsupportedOperationException,
622            IllegalArgumentException,
623            ArithmeticException
624    {
625        return this.product(To.byteValue(aValue));
626    }
627
628    @Override
629    public final PRIMITIVE quotient( final boolean aValue )
630        throws UnsupportedOperationException,
631            IllegalArgumentException,
632            ArithmeticException
633    {
634        return this.quotient(To.byteValue(aValue));
635    }
636
637    @Override
638    @SuppressWarnings( "unchecked" )
639    public final PRIMITIVE round( final RoundingStrategy aRoundingStrategy )
640        throws UnsupportedOperationException,
641            IllegalArgumentException,
642            ArithmeticException,
643            NullPointerException
644    {
645        return (PRIMITIVE)this;
646    }
647
648    @Override
649    public final PRIMITIVE setMaximum()
650        throws UnsupportedOperationException,
651            IllegalArgumentException,
652            ArithmeticException
653    {
654        return this.setScalar(Byte.MAX_VALUE);
655    }
656
657    @Override
658    public final PRIMITIVE setMinimum()
659        throws UnsupportedOperationException,
660            IllegalArgumentException,
661            ArithmeticException
662    {
663        return this.setScalar(Byte.MIN_VALUE);
664    }
665
666    @Override
667    public final PRIMITIVE setNumber( final Number aValue )
668        throws UnsupportedOperationException,
669            IllegalArgumentException,
670            ArithmeticException,
671            NullPointerException
672    {
673        return this.setScalar(aValue.byteValue());
674    }
675
676    @Override
677    public final PRIMITIVE setPrimitive( final SealedPrimitive<?> aValue )
678        throws UnsupportedOperationException,
679            IllegalArgumentException,
680            ArithmeticException,
681            NullPointerException
682    {
683        return this.setScalar(aValue.byteValue());
684    }
685
686    @Override
687    public final PRIMITIVE setReal( final BigDecimal aValue )
688        throws UnsupportedOperationException,
689            IllegalArgumentException,
690            ArithmeticException,
691            NullPointerException
692    {
693        return this.setScalar(aValue.byteValue());
694    }
695
696    @Override
697    public final PRIMITIVE setReal( final BigInteger aValue )
698        throws UnsupportedOperationException,
699            IllegalArgumentException,
700            ArithmeticException,
701            NullPointerException
702    {
703        return this.setScalar(aValue.byteValue());
704    }
705
706    @Override
707    public final PRIMITIVE setReal( final SealedReal<?> aValue )
708        throws UnsupportedOperationException,
709            IllegalArgumentException,
710            ArithmeticException,
711            NullPointerException
712    {
713        return this.setScalar(aValue.byteValue());
714    }
715
716    @Override
717    public final PRIMITIVE setScalar( final boolean aValue )
718        throws UnsupportedOperationException,
719            IllegalArgumentException,
720            ArithmeticException
721    {
722        return this.setScalar(To.byteValue(aValue));
723    }
724
725    @Override
726    public final PRIMITIVE setScalar( final char aValue )
727        throws UnsupportedOperationException,
728            IllegalArgumentException,
729            ArithmeticException
730    {
731        return this.setScalar((byte)aValue);
732    }
733
734    @Override
735    public final PRIMITIVE setScalar( final double aValue )
736        throws UnsupportedOperationException,
737            IllegalArgumentException,
738            ArithmeticException
739    {
740        return this.setScalar((byte)aValue);
741    }
742
743    @Override
744    public final PRIMITIVE setScalar( final float aValue )
745        throws UnsupportedOperationException,
746            IllegalArgumentException,
747            ArithmeticException
748    {
749        return this.setScalar((byte)aValue);
750    }
751
752    @Override
753    public final PRIMITIVE setScalar( final int aValue )
754        throws UnsupportedOperationException,
755            IllegalArgumentException,
756            ArithmeticException
757    {
758        return this.setScalar((byte)aValue);
759    }
760
761    @Override
762    public final PRIMITIVE setScalar( final long aValue )
763        throws UnsupportedOperationException,
764            IllegalArgumentException,
765            ArithmeticException
766    {
767        return this.setScalar((byte)aValue);
768    }
769
770    @Override
771    public final PRIMITIVE setScalar( final SealedScalar<?> aValue )
772        throws UnsupportedOperationException,
773            IllegalArgumentException,
774            ArithmeticException,
775            NullPointerException
776    {
777        return this.setScalar(aValue.byteValue());
778    }
779
780    @Override
781    public final PRIMITIVE setScalar( final short aValue )
782        throws UnsupportedOperationException,
783            IllegalArgumentException,
784            ArithmeticException
785    {
786        return this.setScalar((byte)aValue);
787    }
788
789    @Override
790    public final PRIMITIVE setUnity()
791        throws UnsupportedOperationException,
792            IllegalArgumentException,
793            ArithmeticException
794    {
795        return this.setScalar((byte)1);
796    }
797
798    @Override
799    public final PRIMITIVE setZero()
800        throws UnsupportedOperationException,
801            IllegalArgumentException,
802            ArithmeticException
803    {
804        return this.setScalar((byte)0);
805    }
806
807    @Override
808    public final short shortPostDecrement()
809        throws UnsupportedOperationException,
810            ArithmeticException,
811            IllegalArgumentException
812    {
813        return this.bytePostDecrement();
814    }
815
816    @Override
817    public final short shortPostIncrement()
818        throws UnsupportedOperationException,
819            ArithmeticException,
820            IllegalArgumentException
821    {
822        return this.bytePostIncrement();
823    }
824
825    @Override
826    public final short shortValue()
827    {
828        return this.byteValue();
829    }
830
831    @Override
832    public final PRIMITIVE square()
833        throws UnsupportedOperationException,
834            IllegalArgumentException,
835            ArithmeticException
836    {
837        final byte myValue = this.byteValue();
838        return this.setScalar(myValue * myValue);
839    }
840
841    @Override
842    public final PRIMITIVE sum( final boolean aValue )
843        throws UnsupportedOperationException,
844            IllegalArgumentException,
845            ArithmeticException
846    {
847        return this.sum(To.byteValue(aValue));
848    }
849
850    @Override
851    public final PRIMITIVE swapPrimitives( final Primitive<?> aPrimitive )
852    {
853        final byte myValue = this.byteValue();
854        final byte aValue = aPrimitive.byteValue();
855        aPrimitive.setScalar(myValue);
856        return this.setScalar(aValue);
857    }
858
859    @Override
860    public final PRIMITIVE titleCase()
861    {
862        return this.setScalar(Character.toTitleCase(this.charValue()));
863    }
864
865    @Override
866    public final Number toNumber()
867    {
868        return Byte.valueOf(this.byteValue());
869    }
870
871    @Override
872    public final String toString()
873    {
874        return String.valueOf(this.byteValue());
875    }
876
877    @Override
878    public final BigDecimal toUnlimitedDecimal()
879    {
880        return Constant.toUnlimitedDecimal(this.byteValue());
881    }
882
883    @Override
884    public final BigInteger toUnlimitedInteger()
885    {
886        return BigInteger.valueOf(this.byteValue());
887    }
888
889    @Override
890    public final BigDecimal unlimitedDecimalPostDecrement()
891        throws UnsupportedOperationException,
892            ArithmeticException,
893            IllegalArgumentException
894    {
895        return Constant.toUnlimitedDecimal(this.bytePostDecrement());
896    }
897
898    @Override
899    public final BigDecimal unlimitedDecimalPostIncrement()
900        throws UnsupportedOperationException,
901            ArithmeticException,
902            IllegalArgumentException
903    {
904        return Constant.toUnlimitedDecimal(this.bytePostIncrement());
905    }
906
907    @Override
908    public final BigInteger unlimitedIntegerPostDecrement()
909        throws UnsupportedOperationException,
910            ArithmeticException,
911            IllegalArgumentException
912    {
913        return Constant.toUnlimitedInteger(this.bytePostDecrement());
914    }
915
916    @Override
917    public final BigInteger unlimitedIntegerPostIncrement()
918        throws UnsupportedOperationException,
919            ArithmeticException,
920            IllegalArgumentException
921    {
922        return Constant.toUnlimitedInteger(this.bytePostIncrement());
923    }
924
925    @Override
926    public final PRIMITIVE upperCase()
927    {
928        return this.setScalar(Character.toUpperCase(this.charValue()));
929    }
930
931    @Override
932    public final PRIMITIVE xor( final boolean aValue )
933        throws UnsupportedOperationException,
934            IllegalArgumentException,
935            ArithmeticException
936    {
937        return this.xor(To.byteValue(aValue));
938    }
939}