-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
RandomValues.java
1886 lines (1722 loc) · 63.1 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 org.eclipse.collections.impl.factory.Lists;
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.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.SplittableRandom;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;
import static java.lang.Math.abs;
import static java.time.LocalDate.ofEpochDay;
import static java.time.LocalDateTime.ofInstant;
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>
* Value are generated in a pseudorandom fashion.
* <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 enum Types
{
BOOLEAN( ValueGroup.NUMBER, BooleanValue.class ),
BYTE( ValueGroup.NUMBER, ByteValue.class ),
SHORT( ValueGroup.NUMBER, ShortValue.class ),
INT( ValueGroup.NUMBER, IntValue.class ),
LONG( ValueGroup.NUMBER, LongValue.class ),
FLOAT( ValueGroup.NUMBER, FloatValue.class ),
DOUBLE( ValueGroup.NUMBER, DoubleValue.class ),
STRING( ValueGroup.TEXT, UTF8StringValue.class ),
LOCAL_DATE_TIME( ValueGroup.LOCAL_DATE_TIME, LocalDateTimeValue.class ),
DATE( ValueGroup.DATE, DateValue.class ),
LOCAL_TIME( ValueGroup.LOCAL_TIME, LocalTimeValue.class ),
PERIOD( ValueGroup.DURATION, DurationValue.class ),
DURATION( ValueGroup.DURATION, DurationValue.class ),
TIME( ValueGroup.ZONED_TIME, TimeValue.class ),
DATE_TIME( ValueGroup.ZONED_DATE_TIME, DateTimeValue.class ),
CARTESIAN_POINT( ValueGroup.GEOMETRY, PointValue.class ),
CARTESIAN_POINT_3D( ValueGroup.GEOMETRY, PointValue.class ),
GEOGRAPHIC_POINT( ValueGroup.GEOMETRY, PointValue.class ),
GEOGRAPHIC_POINT_3D( ValueGroup.GEOMETRY, PointValue.class ),
BOOLEAN_ARRAY( ValueGroup.BOOLEAN_ARRAY, BooleanArray.class, true ),
BYTE_ARRAY( ValueGroup.NUMBER_ARRAY, ByteArray.class, true ),
SHORT_ARRAY( ValueGroup.NUMBER_ARRAY, ShortArray.class, true ),
INT_ARRAY( ValueGroup.NUMBER_ARRAY, IntArray.class, true ),
LONG_ARRAY( ValueGroup.NUMBER_ARRAY, LongArray.class, true ),
FLOAT_ARRAY( ValueGroup.NUMBER_ARRAY, FloatArray.class, true ),
DOUBLE_ARRAY( ValueGroup.NUMBER_ARRAY, DoubleArray.class, true ),
STRING_ARRAY( ValueGroup.TEXT_ARRAY, StringArray.class, true ),
LOCAL_DATE_TIME_ARRAY( ValueGroup.LOCAL_DATE_TIME_ARRAY, LocalDateTimeArray.class, true ),
DATE_ARRAY( ValueGroup.DATE_ARRAY, DateArray.class, true ),
LOCAL_TIME_ARRAY( ValueGroup.LOCAL_TIME_ARRAY, LocalTimeArray.class, true ),
PERIOD_ARRAY( ValueGroup.DURATION_ARRAY, DurationArray.class, true ),
DURATION_ARRAY( ValueGroup.DURATION_ARRAY, DurationArray.class, true ),
TIME_ARRAY( ValueGroup.ZONED_TIME_ARRAY, TimeArray.class, true ),
DATE_TIME_ARRAY( ValueGroup.ZONED_DATE_TIME_ARRAY, DateTimeArray.class, true ),
CARTESIAN_POINT_ARRAY( ValueGroup.GEOMETRY_ARRAY, PointArray.class, true ),
CARTESIAN_POINT_3D_ARRAY( ValueGroup.GEOMETRY_ARRAY, PointArray.class, true ),
GEOGRAPHIC_POINT_ARRAY( ValueGroup.GEOMETRY_ARRAY, PointArray.class, true ),
GEOGRAPHIC_POINT_3D_ARRAY( ValueGroup.GEOMETRY_ARRAY, PointArray.class, true );
public final ValueGroup valueGroup;
public final Class<? extends Value> valueClass;
public final boolean arrayType;
Types( ValueGroup valueGroup, Class<? extends Value> valueClass )
{
this( valueGroup, valueClass, false );
}
Types( ValueGroup valueGroup, Class<? extends Value> valueClass, boolean arrayType )
{
this.valueGroup = valueGroup;
this.valueClass = valueClass;
this.arrayType = arrayType;
}
static Types[] arrayTypes()
{
return Lists.mutable.of( Types.values() )
.select( t -> t.arrayType )
.toArray( new Types[0] );
}
static Types[] nonArrayTypes()
{
return Lists.mutable.of( Types.values() )
.select( t -> !t.arrayType )
.toArray( new Types[0] );
}
}
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_BASIC_MULTILINGUAL_PLANE_CODE_POINT = 0xFFFF;
public static final Configuration DEFAULT_CONFIGURATION = new Default();
private static final Types[] ALL_TYPES = Types.values();
private static final Types[] ARRAY_TYPES = Types.arrayTypes();
private static final Types[] NON_ARRAY_TYPES = Types.nonArrayTypes();
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 pseudorandom {@link Value}, distributed uniformly among the supported Value types.
* <p>
* The length of strings will be governed by {@link Configuration#stringMinLength()} and
* {@link Configuration#stringMaxLength()} and
* the length of arrays will be governed by {@link Configuration#arrayMinLength()} and
* {@link Configuration#arrayMaxLength()}
*
* @return the next pseudorandom {@link Value}
*/
public Value nextValue()
{
return nextValueOfType( among( ALL_TYPES ) );
}
public Value nextValueOfTypes( Types... types )
{
return nextValueOfType( among( types ) );
}
public Types[] excluding( Types... types )
{
return Lists.mutable.of( Types.values() )
.withoutAll( Arrays.asList( types ) )
.toArray( new Types[Types.values().length - types.length] );
}
public Value nextValueOfType( Types 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 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 STRING_ARRAY:
return nextStringArray();
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 pseudorandom {@link ArrayValue}, distributed uniformly among the supported Value types.
* <p>
* The length of arrays will be governed by {@link Configuration#arrayMinLength()} and
* {@link Configuration#arrayMaxLength()}
*
* @return the next pseudorandom {@link ArrayValue}
*/
public ArrayValue nextArray()
{
return (ArrayValue) nextValueOfType( among( ARRAY_TYPES ) );
}
/**
* Returns the next pseudorandom uniformly distributed {@link BooleanValue}
*
* @return the next pseudorandom uniformly distributed {@link BooleanValue}
*/
public BooleanValue nextBooleanValue()
{
return Values.booleanValue( generator.nextBoolean() );
}
/**
* Returns the next pseudorandom uniformly distributed {@code boolean}
*
* @return the next pseudorandom uniformly distributed {@code boolean}
*/
public boolean nextBoolean()
{
return generator.nextBoolean();
}
/**
* Returns the next pseudorandom uniformly distributed {@link ByteValue}
*
* @return the next pseudorandom uniformly distributed {@link ByteValue}
*/
public ByteValue nextByteValue()
{
return byteValue( (byte) generator.nextInt() );
}
/**
* Returns the next pseudorandom uniformly distributed {@link ByteValue} between 0 (inclusive) and the specified
* value (exclusive)
*
* @param bound the upper bound (exclusive). Must be positive.
* @return the next pseudorandom, uniformly distributed {@link ByteValue}
* value between zero (inclusive) and {@code bound} (exclusive)
*/
public ByteValue nextByteValue( byte bound )
{
return byteValue( (byte) generator.nextInt( bound ) );
}
/**
* Returns the next pseudorandom uniformly distributed {@link ShortValue}
*
* @return the next pseudorandom uniformly distributed {@link ShortValue}
*/
public ShortValue nextShortValue()
{
return shortValue( (short) generator.nextInt() );
}
/**
* Returns the next pseudorandom uniformly distributed {@link ShortValue} between 0 (inclusive) and the specified
* value (exclusive)
*
* @param bound the upper bound (exclusive). Must be positive.
* @return the next pseudorandom, uniformly distributed {@link ShortValue}
* value between zero (inclusive) and {@code bound} (exclusive)
*/
public ShortValue nextShortValue( short bound )
{
return shortValue( (short) generator.nextInt( bound ) );
}
/**
* Returns the next pseudorandom uniformly distributed {@link IntValue}
*
* @return the next pseudorandom uniformly distributed {@link IntValue}
*/
public IntValue nextIntValue()
{
return intValue( generator.nextInt() );
}
/**
* Returns the next pseudorandom uniformly distributed {@code int}
*
* @return the next pseudorandom uniformly distributed {@code int}
*/
public int nextInt()
{
return generator.nextInt();
}
/**
* Returns the next pseudorandom uniformly distributed {@link IntValue} between 0 (inclusive) and the specified
* value (exclusive)
*
* @param bound the upper bound (exclusive). Must be positive.
* @return the next pseudorandom, uniformly distributed {@link IntValue}
* value between zero (inclusive) and {@code bound} (exclusive)
*/
public IntValue nextIntValue( int bound )
{
return intValue( generator.nextInt( bound ) );
}
/**
* Returns the next pseudorandom uniformly distributed {@code int} between 0 (inclusive) and the specified
* value (exclusive)
*
* @param bound the upper bound (exclusive). Must be positive.
* @return the next pseudorandom, uniformly distributed {@code int}
* value between zero (inclusive) and {@code bound} (exclusive)
*/
public int nextInt( int bound )
{
return generator.nextInt( bound );
}
/**
* Returns a pseudorandom {@code int} between the given lower bound (inclusive) and the upper bound (inclusiv)
*
* @param min minimum value that can be chosen (inclusive)
* @param max maximum value that can be chosen (inclusive)
* @return a pseudorandom {@code int} in the given inclusive range.
*/
public int intBetween( int min, int max )
{
return min + generator.nextInt( max - min + 1 );
}
/**
* Returns the next pseudorandom uniformly distributed {@code long}.
*
* @return the next pseudorandom, uniformly distributed {@code long}
*/
public long nextLong()
{
return generator.nextLong();
}
/**
* Returns the next pseudorandom uniformly distributed {@code long} between 0 (inclusive) and the specified
* value (exclusive)
*
* @param bound the upper bound (exclusive). Must be positive.
* @return the next pseudorandom, uniformly distributed {@code long}
* value between zero (inclusive) and {@code bound} (exclusive)
*/
public long nextLong( long bound )
{
return abs( generator.nextLong() ) % bound;
}
private long nextLong( long origin, long bound )
{
return nextLong( (bound - origin) + 1L ) + origin;
}
/**
* Returns the next pseudorandom uniformly distributed {@link LongValue}
*
* @return the next pseudorandom uniformly distributed {@link LongValue}
*/
public LongValue nextLongValue()
{
return longValue( generator.nextLong() );
}
/**
* Returns the next pseudorandom uniformly distributed {@link LongValue} between 0 (inclusive) and the specified
* value (exclusive)
*
* @param bound the upper bound (exclusive). Must be positive.
* @return the next pseudorandom, uniformly distributed {@link LongValue}
* value between zero (inclusive) and {@code bound} (exclusive)
*/
public LongValue nextLongValue( long bound )
{
return longValue( nextLong( bound ) );
}
/**
* Returns the next pseudorandom uniformly distributed {@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 the next pseudorandom, uniformly distributed {@link LongValue}
* value between {@code lower} (inclusive) and {@code upper} (inclusive)
*/
public LongValue nextLongValue( long lower, long upper )
{
return longValue( nextLong( (upper - lower) + 1L ) + lower );
}
/**
* Returns the next pseudorandom uniformly distributed {@link FloatValue} between 0 (inclusive) and the specified
* 1.0 (exclusive)
*
* @return the next pseudorandom uniformly distributed {@link FloatValue}
*/
public FloatValue nextFloatValue()
{
return floatValue( generator.nextFloat() );
}
/**
* Returns the next pseudorandom uniformly distributed {@code float} between 0 (inclusive) and the specified
* 1.0 (exclusive)
*
* @return the next pseudorandom uniformly distributed {@code float}
*/
public float nextFloat()
{
return generator.nextFloat();
}
/**
* Returns the next pseudorandom uniformly distributed {@link DoubleValue}
*
* @return the next pseudorandom uniformly distributed {@link DoubleValue}
*/
public DoubleValue nextDoubleValue()
{
return doubleValue( generator.nextDouble() );
}
private double doubleBetween( double min, double max )
{
return generator.nextDouble() * (max - min) + min;
}
/**
* Returns the next pseudorandom uniformly distributed {@link NumberValue}
*
* @return the next pseudorandom uniformly distributed {@link NumberValue}
*/
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 );
}
}
/**
* Returns the next pseudorandom {@link TextValue} consisting only of ascii alphabetic and numerical characters.
* <p>
* The length of the text will be between {@link Configuration#stringMinLength()} and
* {@link Configuration#stringMaxLength()}
*
* @return a {@link TextValue} consisting only of ascii alphabetic and numerical characters.
*/
public TextValue nextAlphaNumericTextValue()
{
return nextAlphaNumericTextValue( configuration.stringMinLength(), configuration.stringMaxLength() );
}
/**
* Returns the next pseudorandom {@link TextValue} consisting only of ascii alphabetic and numerical characters.
*
* @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.
*/
public TextValue nextAlphaNumericTextValue( int minLength, int maxLength )
{
int length = intBetween( minLength, maxLength );
byte[] bytes = new byte[length];
for ( int i = 0; i < length; i++ )
{
int nextInt = generator.nextInt( 4 );
switch ( nextInt )
{
case 0:
bytes[i] = (byte) intBetween( 'A', 'Z' );
break;
case 1:
bytes[i] = (byte) intBetween( 'a', 'z' );
break;
//We want digits being roughly as frequent as letters
case 2:
case 3:
bytes[i] = (byte) intBetween( '0', '9' );
break;
default:
throw new IllegalArgumentException( nextInt + " is not an expected value" );
}
}
return Values.utf8Value( bytes );
}
/**
* Returns the next pseudorandom {@link TextValue} consisting only of ascii characters.
* <p>
* The length of the text will be between {@link Configuration#stringMinLength()} and
* {@link Configuration#stringMaxLength()}
*
* @return a {@link TextValue} consisting only of ascii characters.
*/
public TextValue nextAsciiTextValue()
{
return nextAsciiTextValue( configuration.stringMinLength(), configuration.stringMaxLength() );
}
/**
* Returns the next pseudorandom {@link TextValue} consisting only of ascii characters.
*
* @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.
*/
public TextValue nextAsciiTextValue( int minLength, int maxLength )
{
int length = intBetween( minLength, maxLength );
byte[] bytes = new byte[length];
for ( int i = 0; i < length; i++ )
{
bytes[i] = (byte) intBetween( 0, 127 );
}
return Values.utf8Value( bytes );
}
/**
* Returns the next pseudorandom {@link TextValue} consisting only of characters in the Basic Multilingual Plane(BMP).
* <p>
* The length of the text will be between {@link Configuration#stringMinLength()} and
* {@link Configuration#stringMaxLength()}
*
* @return a {@link TextValue} consisting only of characters in the BMP.
*/
public TextValue nextBasicMultilingualPlaneTextValue()
{
return nextBasicMultilingualPlaneTextValue( configuration.stringMinLength(), configuration.stringMaxLength() );
}
/**
* Returns the next pseudorandom {@link TextValue} consisting only of printable ascii characters.
*
* @param minLength the minimum length of the string
* @param maxLength the maximum length of the string
* @return a {@link TextValue} consisting only of printable ascii characters.
*/
public TextValue nextBasicMultilingualPlaneTextValue( int minLength, int maxLength )
{
int length = intBetween( minLength, maxLength );
UTF8StringValueBuilder builder = new UTF8StringValueBuilder( nextPowerOf2( length ) );
for ( int i = 0; i < length; i++ )
{
builder.addCodePoint( nextValidCodePoint( MAX_BASIC_MULTILINGUAL_PLANE_CODE_POINT ) );
}
return builder.build();
}
/**
* Returns the next pseudorandom {@link TextValue}.
* <p>
* The length of the text will be between {@link Configuration#stringMinLength()} and
* {@link Configuration#stringMaxLength()}
*
* @return a generator {@link TextValue}.
*/
public TextValue nextTextValue()
{
return nextTextValue( configuration.stringMinLength(), configuration.stringMaxLength() );
}
/**
* Returns the next pseudorandom {@link TextValue}.
*
* @param minLength the minimum length of the string
* @param maxLength the maximum length of the string
* @return a generator {@link TextValue}.
*/
public TextValue nextTextValue( int minLength, int maxLength )
{
int length = intBetween( minLength, maxLength );
UTF8StringValueBuilder builder = new UTF8StringValueBuilder( nextPowerOf2( length ) );
for ( int i = 0; i < length; i++ )
{
builder.addCodePoint( nextValidCodePoint() );
}
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 configuration.
*
* @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;
}
/**
* Returns the next pseudorandom {@link TimeValue}.
*
* @return the next pseudorandom {@link TimeValue}.
*/
public TimeValue nextTimeValue()
{
return time( OffsetTime.ofInstant( randomInstant(), UTC ) );
}
/**
* Returns the next pseudorandom {@link LocalDateTimeValue}.
*
* @return the next pseudorandom {@link LocalDateTimeValue}.
*/
public LocalDateTimeValue nextLocalDateTimeValue()
{
return localDateTime( ofInstant( randomInstant(), UTC ) );
}
/**
* Returns the next pseudorandom {@link DateValue}.
*
* @return the next pseudorandom {@link DateValue}.
*/
public DateValue nextDateValue()
{
return date( ofEpochDay( nextLong( LocalDate.MIN.toEpochDay(), LocalDate.MAX.toEpochDay() ) ) );
}
/**
* Returns the next pseudorandom {@link LocalTimeValue}.
*
* @return the next pseudorandom {@link LocalTimeValue}.
*/
public LocalTimeValue nextLocalTimeValue()
{
return localTime( ofNanoOfDay( nextLong( LocalTime.MIN.toNanoOfDay(), LocalTime.MAX.toNanoOfDay() ) ) );
}
/**
* Returns the next pseudorandom {@link DateTimeValue}.
*
* @return the next pseudorandom {@link DateTimeValue}.
*/
public DateTimeValue nextDateTimeValue()
{
return nextDateTimeValue( UTC );
}
public DateTimeValue nextDateTimeValue( ZoneId zoneId )
{
return datetime( ZonedDateTime.ofInstant( randomInstant(), zoneId ) );
}
private Instant randomInstant()
{
return Instant.ofEpochSecond(
nextLong( LocalDateTime.MIN.toEpochSecond( UTC ), LocalDateTime.MAX.toEpochSecond( UTC ) ),
nextLong( NANOS_PER_SECOND ) );
}
/**
* Returns the next pseudorandom {@link DurationValue} based on periods.
*
* @return the next pseudorandom {@link DurationValue}.
*/
public DurationValue nextPeriod()
{
// Based on Java period (years, months and days)
return duration( Period.of( generator.nextInt(), generator.nextInt( 12 ), generator.nextInt( 28 ) ) );
}
/**
* Returns the next pseudorandom {@link DurationValue} based on duration.
*
* @return the next pseudorandom {@link DurationValue}.
*/
public DurationValue nextDuration()
{
// Based on java duration (seconds)
return duration( Duration.of( nextLong( DAYS.getDuration().getSeconds() ), ChronoUnit.SECONDS ) );
}
/**
* 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" );
}
}
/**
* Returns the next pseudorandom two-dimensional cartesian {@link PointValue}.
*
* @return the next pseudorandom two-dimensional cartesian {@link PointValue}.
*/
public PointValue nextCartesianPoint()
{
double x = randomCartesianCoordinate();
double y = randomCartesianCoordinate();
return Values.pointValue( CoordinateReferenceSystem.Cartesian, x, y );
}
/**
* Returns the next pseudorandom three-dimensional cartesian {@link PointValue}.
*
* @return the next pseudorandom three-dimensional cartesian {@link PointValue}.
*/
public PointValue nextCartesian3DPoint()
{
double x = randomCartesianCoordinate();
double y = randomCartesianCoordinate();
double z = randomCartesianCoordinate();
return Values.pointValue( CoordinateReferenceSystem.Cartesian_3D, x, y, z );
}
/**
* Returns the next pseudorandom two-dimensional geographic {@link PointValue}.
*
* @return the next pseudorandom two-dimensional geographic {@link PointValue}.
*/
public PointValue nextGeographicPoint()
{
double longitude = randomLongitude();
double latitude = randomLatitude();
return Values.pointValue( CoordinateReferenceSystem.WGS84, longitude, latitude );
}
/**
* Returns the next pseudorandom three-dimensional geographic {@link PointValue}.
*
* @return the next pseudorandom three-dimensional geographic {@link PointValue}.
*/
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;