-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
RandomValues.java
1607 lines (1439 loc) · 45.4 KB
/
RandomValues.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.values.storable;
import org.apache.commons.lang3.ArrayUtils;
import java.lang.reflect.Array;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.SplittableRandom;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import org.neo4j.function.Predicates;
import static java.lang.Math.abs;
import static java.time.LocalDate.ofEpochDay;
import static java.time.LocalTime.ofNanoOfDay;
import static java.time.ZoneOffset.UTC;
import static java.time.temporal.ChronoUnit.DAYS;
import static org.neo4j.values.storable.DateTimeValue.datetime;
import static org.neo4j.values.storable.DateValue.date;
import static org.neo4j.values.storable.DurationValue.duration;
import static org.neo4j.values.storable.LocalDateTimeValue.localDateTime;
import static org.neo4j.values.storable.LocalTimeValue.localTime;
import static org.neo4j.values.storable.TimeValue.time;
import static org.neo4j.values.storable.Values.byteValue;
import static org.neo4j.values.storable.Values.doubleValue;
import static org.neo4j.values.storable.Values.floatValue;
import static org.neo4j.values.storable.Values.intValue;
import static org.neo4j.values.storable.Values.longValue;
import static org.neo4j.values.storable.Values.shortValue;
/**
* Helper class that generates generator values of all supported types.
* <p>
* Generated values are always uniformly distributed in pseudorandom fashion.
* <p>
* Can generate both {@link Value} and "raw" instances. The "raw" type of a value type means
* the corresponding Core API type if such type exists. For example, {@code String[]} is the raw type of {@link TextArray}.
* <p>
* The length of strings will be governed by {@link RandomValues.Configuration#stringMinLength()} and
* {@link RandomValues.Configuration#stringMaxLength()} and
* the length of arrays will be governed by {@link RandomValues.Configuration#arrayMinLength()} and
* {@link RandomValues.Configuration#arrayMaxLength()}
* unless method provide explicit arguments for those configurations in which case the provided argument will be used instead.
*/
public class RandomValues
{
public interface Configuration
{
int stringMinLength();
int stringMaxLength();
int arrayMinLength();
int arrayMaxLength();
int maxCodePoint();
}
public static class Default implements Configuration
{
@Override
public int stringMinLength()
{
return 5;
}
@Override
public int stringMaxLength()
{
return 20;
}
@Override
public int arrayMinLength()
{
return 1;
}
@Override
public int arrayMaxLength()
{
return 10;
}
@Override
public int maxCodePoint()
{
return Character.MAX_CODE_POINT;
}
}
public static final int MAX_BMP_CODE_POINT = 0xFFFF;
public static final Configuration DEFAULT_CONFIGURATION = new Default();
static final int MAX_ASCII_CODE_POINT = 0x7F;
private static final ValueType[] ALL_TYPES = ValueType.values();
private static final ValueType[] ARRAY_TYPES = ValueType.arrayTypes();
private static final long NANOS_PER_SECOND = 1_000_000_000L;
private final Generator generator;
private final Configuration configuration;
private RandomValues( Generator generator )
{
this( generator, DEFAULT_CONFIGURATION );
}
private RandomValues( Generator generator, Configuration configuration )
{
this.generator = generator;
this.configuration = configuration;
}
/**
* Create a {@code RandomValues} with default configuration
*
* @return a {@code RandomValues} instance
*/
public static RandomValues create()
{
return new RandomValues( new RandomGenerator( ThreadLocalRandom.current() ) );
}
/**
* Create a {@code RandomValues} with the given configuration
*
* @return a {@code RandomValues} instance
*/
public static RandomValues create( Configuration configuration )
{
return new RandomValues( new RandomGenerator( ThreadLocalRandom.current() ), configuration );
}
/**
* Create a {@code RandomValues} using the given {@link Random} with given configuration
*
* @return a {@code RandomValues} instance
*/
public static RandomValues create( Random random, Configuration configuration )
{
return new RandomValues( new RandomGenerator( random ), configuration );
}
/**
* Create a {@code RandomValues} using the given {@link Random} with default configuration
*
* @return a {@code RandomValues} instance
*/
public static RandomValues create( Random random )
{
return new RandomValues( new RandomGenerator( random ) );
}
/**
* Create a {@code RandomValues} using the given {@link SplittableRandom} with given configuration
*
* @return a {@code RandomValues} instance
*/
public static RandomValues create( SplittableRandom random, Configuration configuration )
{
return new RandomValues( new SplittableRandomGenerator( random ), configuration );
}
/**
* Create a {@code RandomValues} using the given {@link SplittableRandom} with default configuration
*
* @return a {@code RandomValues} instance
*/
public static RandomValues create( SplittableRandom random )
{
return new RandomValues( new SplittableRandomGenerator( random ) );
}
/**
* Returns the next {@link Value}, distributed uniformly among the supported Value types.
*
* @see RandomValues
*/
public Value nextValue()
{
return nextValueOfType( among( ALL_TYPES ) );
}
/**
* Returns the next {@link Value}, distributed uniformly among the provided value types.
*
* @see RandomValues
*/
public Value nextValueOfTypes( ValueType... types )
{
return nextValueOfType( among( types ) );
}
public static ValueType[] including( Predicate<ValueType> include )
{
return Arrays.stream( ValueType.values() )
.filter( include )
.toArray( ValueType[]::new );
}
/**
* Create an array containing all value types, excluding provided types.
*/
public static ValueType[] excluding( ValueType... exclude )
{
return excluding( ValueType.values(), exclude );
}
public static ValueType[] excluding( ValueType[] among, ValueType... exclude )
{
return excluding( among, t -> ArrayUtils.contains( exclude, t ) );
}
public static ValueType[] excluding( ValueType[] among, Predicate<ValueType> exclude )
{
return Arrays.stream( among )
.filter( Predicates.not( exclude ) )
.toArray( ValueType[]::new );
}
public static ValueType[] typesOfGroup( ValueGroup valueGroup )
{
return Arrays.stream( ValueType.values() )
.filter( t -> t.valueGroup == valueGroup )
.toArray( ValueType[]::new );
}
/**
* Returns the next {@link Value} of provided type.
*
* @see RandomValues
*/
public Value nextValueOfType( ValueType type )
{
switch ( type )
{
case BOOLEAN:
return nextBooleanValue();
case BYTE:
return nextByteValue();
case SHORT:
return nextShortValue();
case STRING:
return nextTextValue();
case INT:
return nextIntValue();
case LONG:
return nextLongValue();
case FLOAT:
return nextFloatValue();
case DOUBLE:
return nextDoubleValue();
case CHAR:
return nextCharValue();
case STRING_ALPHANUMERIC:
return nextAlphaNumericTextValue();
case STRING_ASCII:
return nextAsciiTextValue();
case STRING_BMP:
return nextBasicMultilingualPlaneTextValue();
case LOCAL_DATE_TIME:
return nextLocalDateTimeValue();
case DATE:
return nextDateValue();
case LOCAL_TIME:
return nextLocalTimeValue();
case PERIOD:
return nextPeriod();
case DURATION:
return nextDuration();
case TIME:
return nextTimeValue();
case DATE_TIME:
return nextDateTimeValue();
case CARTESIAN_POINT:
return nextCartesianPoint();
case CARTESIAN_POINT_3D:
return nextCartesian3DPoint();
case GEOGRAPHIC_POINT:
return nextGeographicPoint();
case GEOGRAPHIC_POINT_3D:
return nextGeographic3DPoint();
case BOOLEAN_ARRAY:
return nextBooleanArray();
case BYTE_ARRAY:
return nextByteArray();
case SHORT_ARRAY:
return nextShortArray();
case INT_ARRAY:
return nextIntArray();
case LONG_ARRAY:
return nextLongArray();
case FLOAT_ARRAY:
return nextFloatArray();
case DOUBLE_ARRAY:
return nextDoubleArray();
case CHAR_ARRAY:
return nextCharArray();
case STRING_ARRAY:
return nextTextArray();
case STRING_ALPHANUMERIC_ARRAY:
return nextAlphaNumericTextArray();
case STRING_ASCII_ARRAY:
return nextAsciiTextArray();
case STRING_BMP_ARRAY:
return nextBasicMultilingualPlaneTextArray();
case LOCAL_DATE_TIME_ARRAY:
return nextLocalDateTimeArray();
case DATE_ARRAY:
return nextDateArray();
case LOCAL_TIME_ARRAY:
return nextLocalTimeArray();
case PERIOD_ARRAY:
return nextPeriodArray();
case DURATION_ARRAY:
return nextDurationArray();
case TIME_ARRAY:
return nextTimeArray();
case DATE_TIME_ARRAY:
return nextDateTimeArray();
case CARTESIAN_POINT_ARRAY:
return nextCartesianPointArray();
case CARTESIAN_POINT_3D_ARRAY:
return nextCartesian3DPointArray();
case GEOGRAPHIC_POINT_ARRAY:
return nextGeographicPointArray();
case GEOGRAPHIC_POINT_3D_ARRAY:
return nextGeographic3DPointArray();
default:
throw new IllegalArgumentException( "Unknown value type: " + type );
}
}
/**
* Returns the next {@link ArrayValue}, distributed uniformly among all array types.
*
* @see RandomValues
*/
public ArrayValue nextArray()
{
return (ArrayValue) nextValueOfType( among( ARRAY_TYPES ) );
}
/**
* @see RandomValues
*/
public BooleanValue nextBooleanValue()
{
return Values.booleanValue( generator.nextBoolean() );
}
/**
* @see RandomValues
*/
public boolean nextBoolean()
{
return generator.nextBoolean();
}
/**
* @see RandomValues
*/
public ByteValue nextByteValue()
{
return byteValue( (byte) generator.nextInt() );
}
/**
* Returns the next {@link ByteValue} between 0 (inclusive) and the specified value (exclusive)
*
* @param bound the upper bound (exclusive). Must be positive.
* @return {@link ByteValue}
*/
public ByteValue nextByteValue( byte bound )
{
return byteValue( (byte) generator.nextInt( bound ) );
}
/**
* @see RandomValues
*/
public ShortValue nextShortValue()
{
return shortValue( (short) generator.nextInt() );
}
/**
* Returns the next {@link ShortValue} between 0 (inclusive) and the specified value (exclusive)
*
* @param bound the upper bound (exclusive). Must be positive.
* @return {@link ShortValue}
*/
public ShortValue nextShortValue( short bound )
{
return shortValue( (short) generator.nextInt( bound ) );
}
/**
* @see RandomValues
*/
public IntValue nextIntValue()
{
return intValue( generator.nextInt() );
}
/**
* @see RandomValues
*/
public int nextInt()
{
return generator.nextInt();
}
/**
* Returns the next {@link IntValue} between 0 (inclusive) and the specified value (exclusive)
*
* @param bound the upper bound (exclusive). Must be positive.
* @return {@link IntValue}
* @see RandomValues
*/
public IntValue nextIntValue( int bound )
{
return intValue( generator.nextInt( bound ) );
}
/**
* Returns the next {@code int} between 0 (inclusive) and the specified value (exclusive)
*
* @param bound the upper bound (exclusive). Must be positive.
* @return {@code int}
* @see RandomValues
*/
public int nextInt( int bound )
{
return generator.nextInt( bound );
}
/**
* Returns an {@code int} between the given lower bound (inclusive) and the upper bound (inclusive)
*
* @param min minimum value that can be chosen (inclusive)
* @param max maximum value that can be chosen (inclusive)
* @return an {@code int} in the given inclusive range.
* @see RandomValues
*/
public int intBetween( int min, int max )
{
return min + generator.nextInt( max - min + 1 );
}
/**
* @see RandomValues
*/
public long nextLong()
{
return generator.nextLong();
}
/**
* Returns the next {@code long} between 0 (inclusive) and the specified value (exclusive)
*
* @param bound the upper bound (exclusive). Must be positive.
* @return {@code long}
* @see RandomValues
*/
public long nextLong( long bound )
{
return abs( generator.nextLong() ) % bound;
}
/**
* Returns a {@code long} between the given lower bound (inclusive) and the upper bound (inclusive)
*
* @param min minimum value that can be chosen (inclusive)
* @param max maximum value that can be chosen (inclusive)
* @return a {@code long} in the given inclusive range.
* @see RandomValues
*/
private long longBetween( long min, long max )
{
return nextLong( (max - min) + 1L ) + min;
}
/**
* @see RandomValues
*/
public LongValue nextLongValue()
{
return longValue( generator.nextLong() );
}
/**
* Returns the next {@link LongValue} between 0 (inclusive) and the specified value (exclusive)
*
* @param bound the upper bound (exclusive). Must be positive.
* @return {@link LongValue}
* @see RandomValues
*/
public LongValue nextLongValue( long bound )
{
return longValue( nextLong( bound ) );
}
/**
* Returns the next {@link LongValue} between the specified lower bound (inclusive) and the specified upper bound (inclusive)
*
* @param lower the lower bound (inclusive).
* @param upper the upper bound (inclusive).
* @return {@link LongValue}
* @see RandomValues
*/
public LongValue nextLongValue( long lower, long upper )
{
return longValue( nextLong( (upper - lower) + 1L ) + lower );
}
/**
* Returns the next {@link FloatValue} between 0 (inclusive) and 1.0 (exclusive)
*
* @return {@link FloatValue}
* @see RandomValues
*/
public FloatValue nextFloatValue()
{
return floatValue( generator.nextFloat() );
}
/**
* Returns the next {@code float} between 0 (inclusive) and 1.0 (exclusive)
*
* @return {@code float}
* @see RandomValues
*/
public float nextFloat()
{
return generator.nextFloat();
}
/**
* @see RandomValues
*/
public DoubleValue nextDoubleValue()
{
return doubleValue( nextDouble() );
}
/**
* Returns the next {@code double} between 0 (inclusive) and 1.0 (exclusive)
*
* @return {@code float}
* @see RandomValues
*/
public double nextDouble()
{
return generator.nextDouble();
}
private double doubleBetween( double min, double max )
{
return nextDouble() * (max - min) + min;
}
/**
* @see RandomValues
*/
public NumberValue nextNumberValue()
{
int type = generator.nextInt( 6 );
switch ( type )
{
case 0:
return nextByteValue();
case 1:
return nextShortValue();
case 2:
return nextIntValue();
case 3:
return nextLongValue();
case 4:
return nextFloatValue();
case 5:
return nextDoubleValue();
default:
throw new IllegalArgumentException( "Unknown value type " + type );
}
}
public CharValue nextCharValue()
{
return Values.charValue( nextCharRaw() );
}
public char nextCharRaw()
{
int codePoint = bmpCodePoint();
assert (codePoint & ~0xFFFF) == 0;
return (char) codePoint;
}
/**
* @return a {@link TextValue} consisting only of ascii alphabetic and numerical characters.
* @see RandomValues
*/
public TextValue nextAlphaNumericTextValue()
{
return nextAlphaNumericTextValue( minString(), maxString() );
}
/**
* @param minLength the minimum length of the string
* @param maxLength the maximum length of the string
* @return a {@link TextValue} consisting only of ascii alphabetic and numerical characters.
* @see RandomValues
*/
public TextValue nextAlphaNumericTextValue( int minLength, int maxLength )
{
return nextTextValue( minLength, maxLength, this::alphaNumericCodePoint );
}
/**
* @return a {@link TextValue} consisting only of ascii characters.
* @see RandomValues
*/
public TextValue nextAsciiTextValue()
{
return nextAsciiTextValue( minString(), maxString() );
}
/**
* @param minLength the minimum length of the string
* @param maxLength the maximum length of the string
* @return a {@link TextValue} consisting only of ascii characters.
* @see RandomValues
*/
public TextValue nextAsciiTextValue( int minLength, int maxLength )
{
return nextTextValue( minLength, maxLength, this::asciiCodePoint );
}
/**
* @return a {@link TextValue} consisting only of characters in the Basic Multilingual Plane(BMP).
* @see RandomValues
*/
public TextValue nextBasicMultilingualPlaneTextValue()
{
return nextTextValue( minString(), maxString(), this::bmpCodePoint );
}
/**
* @see RandomValues
*/
public TextValue nextTextValue()
{
return nextTextValue( minString(), maxString() );
}
/**
* @param minLength the minimum length of the string
* @param maxLength the maximum length of the string
* @return {@link TextValue}.
* @see RandomValues
*/
public TextValue nextTextValue( int minLength, int maxLength )
{
return nextTextValue( minLength, maxLength, this::nextValidCodePoint );
}
private TextValue nextTextValue( int minLength, int maxLength, CodePointFactory codePointFactory )
{
// todo should we generate UTF8StringValue or StringValue? Or maybe both? Randomly?
int length = intBetween( minLength, maxLength );
UTF8StringValueBuilder builder = new UTF8StringValueBuilder( nextPowerOf2( length ) );
for ( int i = 0; i < length; i++ )
{
builder.addCodePoint( codePointFactory.generate() );
}
return builder.build();
}
/**
* Generate next code point that is valid for composition of a string.
* Additional limitation on code point range is given by configuration.
*
* @return A pseudorandom valid code point
*/
private int nextValidCodePoint()
{
return nextValidCodePoint( configuration.maxCodePoint() );
}
/**
* Generate next code point that is valid for composition of a string.
* Additional limitation on code point range is given by method argument.
*
* @param maxCodePoint the maximum code point to consider
* @return A pseudorandom valid code point
*/
private int nextValidCodePoint( int maxCodePoint )
{
int codePoint;
int type;
do
{
codePoint = intBetween( Character.MIN_CODE_POINT, maxCodePoint );
type = Character.getType( codePoint );
}
while ( type == Character.UNASSIGNED ||
type == Character.PRIVATE_USE ||
type == Character.SURROGATE );
return codePoint;
}
/**
* @return next code point limited to the ascii characters.
*/
private int asciiCodePoint()
{
return nextValidCodePoint( MAX_ASCII_CODE_POINT );
}
/**
* @return next code point limited to the alpha numeric characters.
*/
private int alphaNumericCodePoint()
{
int nextInt = generator.nextInt( 4 );
if ( nextInt == 0 )
{
return intBetween( 'A', 'Z' );
}
else if ( nextInt == 1 )
{
return intBetween( 'a', 'z' );
}
else
{
//We want digits being roughly as frequent as letters
return intBetween( '0', '9' );
}
}
/**
* @return next code point limited to the Basic Multilingual Plane (BMP).
*/
private int bmpCodePoint()
{
return nextValidCodePoint( MAX_BMP_CODE_POINT );
}
/**
* @see RandomValues
*/
public TimeValue nextTimeValue()
{
return time( nextTimeRaw() );
}
/**
* @see RandomValues
*/
public LocalDateTimeValue nextLocalDateTimeValue()
{
return localDateTime( nextLocalDateTimeRaw() );
}
/**
* @see RandomValues
*/
public DateValue nextDateValue()
{
return date( nextDateRaw() );
}
/**
* @see RandomValues
*/
public LocalTimeValue nextLocalTimeValue()
{
return localTime( nextLocalTimeRaw() );
}
/**
* @see RandomValues
*/
public DateTimeValue nextDateTimeValue()
{
return nextDateTimeValue( UTC );
}
/**
* @see RandomValues
*/
public DateTimeValue nextDateTimeValue( ZoneId zoneId )
{
return datetime( nextZonedDateTimeRaw( zoneId ) );
}
/**
* @return next {@link DurationValue} based on java {@link Period} (years, months and days).
* @see RandomValues
*/
public DurationValue nextPeriod()
{
return duration( nextPeriodRaw() );
}
/**
* @return next {@link DurationValue} based on java {@link Duration} (seconds, nanos).
* @see RandomValues
*/
public DurationValue nextDuration()
{
return duration( nextDurationRaw() );
}
/**
* Returns a randomly selected temporal value spread uniformly over the supported types.
*
* @return a randomly selected temporal value
*/
public Value nextTemporalValue()
{
int nextInt = generator.nextInt( 6 );
switch ( nextInt )
{
case 0:
return nextDateValue();
case 1:
return nextLocalDateTimeValue();
case 2:
return nextDateTimeValue();
case 3:
return nextLocalTimeValue();
case 4:
return nextTimeValue();
case 5:
return nextDuration();
default:
throw new IllegalArgumentException( nextInt + " not a valid temporal type" );
}
}
/**
* @return the next pseudorandom two-dimensional cartesian {@link PointValue}.
* @see RandomValues
*/
public PointValue nextCartesianPoint()
{
double x = randomCartesianCoordinate();
double y = randomCartesianCoordinate();
return Values.pointValue( CoordinateReferenceSystem.Cartesian, x, y );
}
/**
* @return the next pseudorandom three-dimensional cartesian {@link PointValue}.
* @see RandomValues
*/
public PointValue nextCartesian3DPoint()
{
double x = randomCartesianCoordinate();
double y = randomCartesianCoordinate();
double z = randomCartesianCoordinate();
return Values.pointValue( CoordinateReferenceSystem.Cartesian_3D, x, y, z );
}
/**
* @return the next pseudorandom two-dimensional geographic {@link PointValue}.
* @see RandomValues
*/
public PointValue nextGeographicPoint()
{
double longitude = randomLongitude();
double latitude = randomLatitude();
return Values.pointValue( CoordinateReferenceSystem.WGS84, longitude, latitude );
}
/**
* @return the next pseudorandom three-dimensional geographic {@link PointValue}.
* @see RandomValues
*/
public PointValue nextGeographic3DPoint()
{
double longitude = randomLongitude();
double latitude = randomLatitude();
double z = randomCartesianCoordinate();
return Values.pointValue( CoordinateReferenceSystem.WGS84_3D, longitude, latitude, z );
}
private double randomLatitude()
{
double spatialDefaultMinLatitude = -90;
double spatialDefaultMaxLatitude = 90;
return doubleBetween( spatialDefaultMinLatitude, spatialDefaultMaxLatitude );
}
private double randomLongitude()
{
double spatialDefaultMinLongitude = -180;
double spatialDefaultMaxLongitude = 180;
return doubleBetween( spatialDefaultMinLongitude, spatialDefaultMaxLongitude );
}
private double randomCartesianCoordinate()
{
double spatialDefaultMinExtent = -1000000;
double spatialDefaultMaxExtent = 1000000;
return doubleBetween( spatialDefaultMinExtent, spatialDefaultMaxExtent );
}
/**
* Returns a randomly selected point value spread uniformly over the supported types of points.
*
* @return a randomly selected point value
*/
public PointValue nextPointValue()
{
int nextInt = generator.nextInt( 4 );
switch ( nextInt )
{
case 0:
return nextCartesianPoint();
case 1:
return nextCartesian3DPoint();
case 2:
return nextGeographicPoint();
case 3:
return nextGeographic3DPoint();
default:
throw new IllegalStateException( nextInt + " not a valid point type" );
}
}
public CharArray nextCharArray()
{
return Values.charArray( nextCharArrayRaw( minArray(), maxArray() ) );
}
private char[] nextCharArrayRaw( int minLength, int maxLength )
{
int length = intBetween( minLength, maxLength );
char[] array = new char[length];
for ( int i = 0; i < length; i++ )
{
array[i] = nextCharRaw();
}
return array;
}
/**
* @see RandomValues
*/
public DoubleArray nextDoubleArray()
{
double[] array = nextDoubleArrayRaw( minArray(), maxArray() );
return Values.doubleArray( array );
}