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