forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoarbitrary.java
More file actions
1176 lines (1086 loc) · 46.2 KB
/
Coarbitrary.java
File metadata and controls
1176 lines (1086 loc) · 46.2 KB
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
package fj.test;
import fj.F;
import fj.F2;
import fj.F3;
import fj.F4;
import fj.F5;
import fj.F6;
import fj.F7;
import fj.F8;
import static fj.Function.curry;
import static fj.P.p;
import fj.P1;
import fj.P2;
import fj.P3;
import fj.P4;
import fj.P5;
import fj.P6;
import fj.P7;
import fj.P8;
import fj.data.Array;
import static fj.data.Array.array;
import fj.data.Either;
import fj.data.List;
import static fj.data.List.fromString;
import static fj.data.List.nil;
import fj.data.Option;
import fj.data.Stream;
import static fj.test.Variant.variant;
import static java.lang.Double.doubleToRawLongBits;
import static java.lang.Float.floatToRawIntBits;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Date;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Properties;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.util.WeakHashMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.SynchronousQueue;
/**
* Transforms a type and a generator to produce a new generator. This function is used to generate
* {@link Arbitrary arbitrary} functions.
*
* @version %build.number%
*/
public abstract class Coarbitrary<A> {
/**
* Transforms the given value and generator to a new generator with a high probability of being
* independent.
*
* @param a The value to produce the generator from.
* @param g The generator to produce the new generator from.
* @return A new generator with a high probability of being independent.
*/
public abstract <B> Gen<B> coarbitrary(A a, Gen<B> g);
/**
* A curried version of {@link #coarbitrary(Object, Gen)}.
*
* @param a The value to produce the generator from.
* @return A curried version of {@link #coarbitrary(Object, Gen)}.
*/
public final <B> F<Gen<B>, Gen<B>> coarbitrary(final A a) {
return new F<Gen<B>, Gen<B>>() {
public Gen<B> f(final Gen<B> g) {
return coarbitrary(a, g);
}
};
}
/**
* Composes the given function with this coarbitrary to produce a new coarbitrary.
*
* @param f The function to compose.
* @return A new coarbitrary composed with the given function.
*/
public final <B> Coarbitrary<B> compose(final F<B, A> f) {
return new Coarbitrary<B>() {
public <X> Gen<X> coarbitrary(final B b, final Gen<X> g) {
return Coarbitrary.this.coarbitrary(f.f(b), g);
}
};
}
/**
* Co-maps this coarbitrary using the given function.
*
* @param f The function to co-map with.
* @return A co-mapped coarbitrary.
*/
public final <B> Coarbitrary<B> comap(final F<B, A> f) {
return new Coarbitrary<B>() {
public <X> Gen<X> coarbitrary(final B b, final Gen<X> g) {
return Coarbitrary.this.coarbitrary(f.f(b), g);
}
};
}
/**
* A coarbitrary for a function.
*
* @param a An arbitrary for the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function.
*/
public static <A, B> Coarbitrary<F<A, B>> coarbF(final Arbitrary<A> a, final Coarbitrary<B> c) {
return new Coarbitrary<F<A, B>>() {
public <X> Gen<X> coarbitrary(final F<A, B> f, final Gen<X> g) {
return a.gen.bind(new F<A, Gen<X>>() {
public Gen<X> f(final A a) {
return c.coarbitrary(f.f(a), g);
}
});
}
};
}
/**
* A coarbitrary for a function-2.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-2.
*/
public static <A, B, C> Coarbitrary<F2<A, B, C>> coarbF2(final Arbitrary<A> aa, final Arbitrary<B> ab,
final Coarbitrary<C> c) {
return new Coarbitrary<F2<A, B, C>>() {
public <X> Gen<X> coarbitrary(final F2<A, B, C> f, final Gen<X> g) {
return coarbF(aa, coarbF(ab, c)).coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for a function-3.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param ac An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-3.
*/
public static <A, B, C, D> Coarbitrary<F3<A, B, C, D>> coarbF3(final Arbitrary<A> aa, final Arbitrary<B> ab,
final Arbitrary<C> ac, final Coarbitrary<D> c) {
return new Coarbitrary<F3<A, B, C, D>>() {
public <X> Gen<X> coarbitrary(final F3<A, B, C, D> f, final Gen<X> g) {
return coarbF(aa, coarbF(ab, coarbF(ac, c))).coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for a function-4.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param ac An arbitrary for part of the domain of the function.
* @param ad An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-4.
*/
public static <A, B, C, D, E> Coarbitrary<F4<A, B, C, D, E>> coarbF4(final Arbitrary<A> aa, final Arbitrary<B> ab,
final Arbitrary<C> ac, final Arbitrary<D> ad,
final Coarbitrary<E> c) {
return new Coarbitrary<F4<A, B, C, D, E>>() {
public <X> Gen<X> coarbitrary(final F4<A, B, C, D, E> f, final Gen<X> g) {
return coarbF(aa, coarbF(ab, coarbF(ac, coarbF(ad, c)))).coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for a function-5.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param ac An arbitrary for part of the domain of the function.
* @param ad An arbitrary for part of the domain of the function.
* @param ae An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-5.
*/
public static <A, B, C, D, E, F$> Coarbitrary<F5<A, B, C, D, E, F$>> coarbF5(final Arbitrary<A> aa,
final Arbitrary<B> ab,
final Arbitrary<C> ac,
final Arbitrary<D> ad,
final Arbitrary<E> ae,
final Coarbitrary<F$> c) {
return new Coarbitrary<F5<A, B, C, D, E, F$>>() {
public <X> Gen<X> coarbitrary(final F5<A, B, C, D, E, F$> f, final Gen<X> g) {
return coarbF(aa, coarbF(ab, coarbF(ac, coarbF(ad, coarbF(ae, c))))).coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for a function-6.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param ac An arbitrary for part of the domain of the function.
* @param ad An arbitrary for part of the domain of the function.
* @param ae An arbitrary for part of the domain of the function.
* @param af An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-6.
*/
public static <A, B, C, D, E, F$, G> Coarbitrary<F6<A, B, C, D, E, F$, G>> coarbF6(final Arbitrary<A> aa,
final Arbitrary<B> ab,
final Arbitrary<C> ac,
final Arbitrary<D> ad,
final Arbitrary<E> ae,
final Arbitrary<F$> af,
final Coarbitrary<G> c) {
return new Coarbitrary<F6<A, B, C, D, E, F$, G>>() {
public <X> Gen<X> coarbitrary(final F6<A, B, C, D, E, F$, G> f, final Gen<X> g) {
return coarbF(aa, coarbF(ab, coarbF(ac, coarbF(ad, coarbF(ae, coarbF(af, c)))))).coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for a function-7.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param ac An arbitrary for part of the domain of the function.
* @param ad An arbitrary for part of the domain of the function.
* @param ae An arbitrary for part of the domain of the function.
* @param af An arbitrary for part of the domain of the function.
* @param ag An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-7.
*/
public static <A, B, C, D, E, F$, G, H> Coarbitrary<F7<A, B, C, D, E, F$, G, H>> coarbF7(final Arbitrary<A> aa,
final Arbitrary<B> ab,
final Arbitrary<C> ac,
final Arbitrary<D> ad,
final Arbitrary<E> ae,
final Arbitrary<F$> af,
final Arbitrary<G> ag,
final Coarbitrary<H> c) {
return new Coarbitrary<F7<A, B, C, D, E, F$, G, H>>() {
public <X> Gen<X> coarbitrary(final F7<A, B, C, D, E, F$, G, H> f, final Gen<X> g) {
return coarbF(aa, coarbF(ab, coarbF(ac, coarbF(ad, coarbF(ae, coarbF(af, coarbF(ag, c)))))))
.coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for a function-8.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param ac An arbitrary for part of the domain of the function.
* @param ad An arbitrary for part of the domain of the function.
* @param ae An arbitrary for part of the domain of the function.
* @param af An arbitrary for part of the domain of the function.
* @param ag An arbitrary for part of the domain of the function.
* @param ah An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-8.
*/
public static <A, B, C, D, E, F$, G, H, I> Coarbitrary<F8<A, B, C, D, E, F$, G, H, I>> coarbF8(final Arbitrary<A> aa,
final Arbitrary<B> ab,
final Arbitrary<C> ac,
final Arbitrary<D> ad,
final Arbitrary<E> ae,
final Arbitrary<F$> af,
final Arbitrary<G> ag,
final Arbitrary<H> ah,
final Coarbitrary<I> c) {
return new Coarbitrary<F8<A, B, C, D, E, F$, G, H, I>>() {
public <X> Gen<X> coarbitrary(final F8<A, B, C, D, E, F$, G, H, I> f, final Gen<X> g) {
return coarbF(aa, coarbF(ab, coarbF(ac, coarbF(ad, coarbF(ae, coarbF(af, coarbF(ag, coarbF(ah, c))))))))
.coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for booleans.
*/
public static final Coarbitrary<Boolean> coarbBoolean = new Coarbitrary<Boolean>() {
public <B> Gen<B> coarbitrary(final Boolean b, final Gen<B> g) {
return variant(b ? 0 : 1, g);
}
};
/**
* A coarbitrary for integers.
*/
public static final Coarbitrary<Integer> coarbInteger = new Coarbitrary<Integer>() {
public <B> Gen<B> coarbitrary(final Integer i, final Gen<B> g) {
return variant(i >= 0 ? 2 * i : -2 * i + 1, g);
}
};
/**
* A coarbitrary for bytes.
*/
public static final Coarbitrary<Byte> coarbByte = new Coarbitrary<Byte>() {
public <B> Gen<B> coarbitrary(final Byte b, final Gen<B> g) {
return variant(b >= 0 ? 2 * b : -2 * b + 1, g);
}
};
/**
* A coarbitrary for shorts.
*/
public static final Coarbitrary<Short> coarbShort = new Coarbitrary<Short>() {
public <B> Gen<B> coarbitrary(final Short s, final Gen<B> g) {
return variant(s >= 0 ? 2 * s : -2 * s + 1, g);
}
};
/**
* A coarbitrary for longs.
*/
public static final Coarbitrary<Long> coarbLong = new Coarbitrary<Long>() {
public <B> Gen<B> coarbitrary(final Long l, final Gen<B> g) {
return variant(l >= 0L ? 2L * l : -2L * l + 1L, g);
}
};
/**
* A coarbitrary for characters.
*/
public static final Coarbitrary<Character> coarbCharacter = new Coarbitrary<Character>() {
public <B> Gen<B> coarbitrary(final Character c, final Gen<B> g) {
return variant(c << 1, g);
}
};
/**
* A coarbitrary for floats.
*/
public static final Coarbitrary<Float> coarbFloat = new Coarbitrary<Float>() {
public <B> Gen<B> coarbitrary(final Float f, final Gen<B> g) {
return coarbInteger.coarbitrary(floatToRawIntBits(f), g);
}
};
/**
* A coarbitrary for doubles.
*/
public static final Coarbitrary<Double> coarbDouble = new Coarbitrary<Double>() {
public <B> Gen<B> coarbitrary(final Double d, final Gen<B> g) {
return coarbLong.coarbitrary(doubleToRawLongBits(d), g);
}
};
/**
* A coarbitrary for the optional value.
*
* @param ca A coarbitrary for the type of the optional value.
* @return A coarbitrary for the optional value.
*/
public static <A> Coarbitrary<Option<A>> coarbOption(final Coarbitrary<A> ca) {
return new Coarbitrary<Option<A>>() {
public <B> Gen<B> coarbitrary(final Option<A> o, final Gen<B> g) {
return o.isNone() ? variant(0, g) : variant(1, ca.coarbitrary(o.some(), g));
}
};
}
/**
* A coarbitrary for the disjoint union.
*
* @param ca A coarbitrary for one side of the disjoint union.
* @param cb A coarbitrary for one side of the disjoint union.
* @return A coarbitrary for the disjoint union.
*/
public static <A, B> Coarbitrary<Either<A, B>> coarbEither(final Coarbitrary<A> ca, final Coarbitrary<B> cb) {
return new Coarbitrary<Either<A, B>>() {
public <X> Gen<X> coarbitrary(final Either<A, B> e, final Gen<X> g) {
return e.isLeft() ?
variant(0, ca.coarbitrary(e.left().value(), g)) :
variant(1, cb.coarbitrary(e.right().value(), g));
}
};
}
/**
* A coarbitrary for lists.
*
* @param ca A coarbitrary for the elements of the list.
* @return A coarbitrary for lists.
*/
public static <A> Coarbitrary<List<A>> coarbList(final Coarbitrary<A> ca) {
return new Coarbitrary<List<A>>() {
public <B> Gen<B> coarbitrary(final List<A> as, final Gen<B> g) {
return as.isEmpty() ?
variant(0, g) :
variant(1, ca.coarbitrary(as.head(), coarbitrary(as.tail(), g)));
}
};
}
/**
* A coarbitrary for strings.
*/
public static final Coarbitrary<String> coarbString = new Coarbitrary<String>() {
public <B> Gen<B> coarbitrary(final String s, final Gen<B> g) {
return coarbList(coarbCharacter).coarbitrary(fromString(s), g);
}
};
/**
* A coarbitrary for string buffers.
*/
public static final Coarbitrary<StringBuffer> coarbStringBuffer = new Coarbitrary<StringBuffer>() {
public <B> Gen<B> coarbitrary(final StringBuffer s, final Gen<B> g) {
return coarbString.coarbitrary(s.toString(), g);
}
};
/**
* A coarbitrary for string builders.
*/
public static final Coarbitrary<StringBuilder> coarbStringBuilder = new Coarbitrary<StringBuilder>() {
public <B> Gen<B> coarbitrary(final StringBuilder s, final Gen<B> g) {
return coarbString.coarbitrary(s.toString(), g);
}
};
/**
* A coarbitrary for streams.
*
* @param ca A coarbitrary for the elements of the stream.
* @return A coarbitrary for streams.
*/
public static <A> Coarbitrary<Stream<A>> coarbStream(final Coarbitrary<A> ca) {
return new Coarbitrary<Stream<A>>() {
public <B> Gen<B> coarbitrary(final Stream<A> as, final Gen<B> g) {
return as.isEmpty() ?
variant(0, g) :
variant(1, ca.coarbitrary(as.head(), coarbitrary(as.tail()._1(), g)));
}
};
}
/**
* A coarbitrary for arrays.
*
* @param ca A coarbitrary for the elements of the array.
* @return A coarbitrary for arrays.
*/
public static <A> Coarbitrary<Array<A>> coarbArray(final Coarbitrary<A> ca) {
return new Coarbitrary<Array<A>>() {
public <B> Gen<B> coarbitrary(final Array<A> as, final Gen<B> g) {
return coarbList(ca).coarbitrary(as.toList(), g);
}
};
}
/**
* A coarbitrary for throwables.
*
* @param cs A coarbitrary for the throwable message.
* @return A coarbitrary for throwables.
*/
public static Coarbitrary<Throwable> coarbThrowable(final Coarbitrary<String> cs) {
return cs.comap(new F<Throwable, String>() {
public String f(final Throwable t) {
return t.getMessage();
}
});
}
/**
* A coarbitrary for throwables.
*/
public static final Coarbitrary<Throwable> coarbThrowable =
coarbThrowable(coarbString);
// BEGIN java.util
/**
* A coarbitrary for array lists.
*
* @param ca A coarbitrary for the elements of the array list.
* @return A coarbitrary for array lists.
*/
public static <A> Coarbitrary<ArrayList<A>> coarbArrayList(final Coarbitrary<A> ca) {
return new Coarbitrary<ArrayList<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final ArrayList<A> as, final Gen<B> g) {
return coarbArray(ca).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
/**
* A coarbitrary for bit sets.
*/
public static final Coarbitrary<BitSet> coarbBitSet = new Coarbitrary<BitSet>() {
public <B> Gen<B> coarbitrary(final BitSet s, final Gen<B> g) {
List<Boolean> x = nil();
for (int i = 0; i < s.size(); i++) {
x = x.snoc(s.get(i));
}
return coarbList(coarbBoolean).coarbitrary(x, g);
}
};
/**
* A coarbitrary for calendars.
*/
public static final Coarbitrary<Calendar> coarbCalendar = new Coarbitrary<Calendar>() {
public <B> Gen<B> coarbitrary(final Calendar c, final Gen<B> g) {
return coarbLong.coarbitrary(c.getTime().getTime(), g);
}
};
/**
* A coarbitrary for dates.
*/
public static final Coarbitrary<Date> coarbDate = new Coarbitrary<Date>() {
public <B> Gen<B> coarbitrary(final Date d, final Gen<B> g) {
return coarbLong.coarbitrary(d.getTime(), g);
}
};
/**
* A coarbitrary for enum maps.
*
* @param ck A coarbitrary for the map keys.
* @param cv A coarbitrary for the map values.
* @return A coarbitrary for enum maps.
*/
public static <K extends Enum<K>, V> Coarbitrary<EnumMap<K, V>> coarbEnumMap(final Coarbitrary<K> ck,
final Coarbitrary<V> cv) {
return new Coarbitrary<EnumMap<K, V>>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public <B> Gen<B> coarbitrary(final EnumMap<K, V> m, final Gen<B> g) {
return coarbHashtable(ck, cv).coarbitrary(new Hashtable<K, V>(m), g);
}
};
}
/**
* A coarbitrary for enum sets.
*
* @param c A coarbitrary for the elements of the enum set.
* @return A coarbitrary for enum sets.
*/
public static <A extends Enum<A>> Coarbitrary<EnumSet<A>> coarbEnumSet(final Coarbitrary<A> c) {
return new Coarbitrary<EnumSet<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final EnumSet<A> as, final Gen<B> g) {
return coarbHashSet(c).coarbitrary(new HashSet<A>(as), g);
}
};
}
/**
* A coarbitrary for gregorian calendars.
*/
public static final Coarbitrary<GregorianCalendar> coarbGregorianCalendar = new Coarbitrary<GregorianCalendar>() {
public <B> Gen<B> coarbitrary(final GregorianCalendar c, final Gen<B> g) {
return coarbLong.coarbitrary(c.getTime().getTime(), g);
}
};
/**
* A coarbitrary for hash maps.
*
* @param ck A coarbitrary for the map keys.
* @param cv A coarbitrary for the map values.
* @return A coarbitrary for hash maps.
*/
public static <K, V> Coarbitrary<HashMap<K, V>> coarbHashMap(final Coarbitrary<K> ck, final Coarbitrary<V> cv) {
return new Coarbitrary<HashMap<K, V>>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public <B> Gen<B> coarbitrary(final HashMap<K, V> m, final Gen<B> g) {
return coarbHashtable(ck, cv).coarbitrary(new Hashtable<K, V>(m), g);
}
};
}
/**
* A coarbitrary for hash sets.
*
* @param c A coarbitrary for the elements of the hash set.
* @return A coarbitrary for hash sets.
*/
public static <A> Coarbitrary<HashSet<A>> coarbHashSet(final Coarbitrary<A> c) {
return new Coarbitrary<HashSet<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final HashSet<A> as, final Gen<B> g) {
return coarbArray(c).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
/**
* A coarbitrary for hash tables.
*
* @param ck A coarbitrary for the map keys.
* @param cv A coarbitrary for the map values.
* @return A coarbitrary for hash tables.
*/
public static <K, V> Coarbitrary<Hashtable<K, V>> coarbHashtable(final Coarbitrary<K> ck, final Coarbitrary<V> cv) {
return new Coarbitrary<Hashtable<K, V>>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public <B> Gen<B> coarbitrary(final Hashtable<K, V> h, final Gen<B> g) {
List<P2<K, V>> x = nil();
for (final K k : h.keySet()) {
x = x.snoc(p(k, h.get(k)));
}
return coarbList(coarbP2(ck, cv)).coarbitrary(x, g);
}
};
}
/**
* A coarbitrary for identity hash maps.
*
* @param ck A coarbitrary for the map keys.
* @param cv A coarbitrary for the map values.
* @return A coarbitrary for identity hash maps.
*/
public static <K, V> Coarbitrary<IdentityHashMap<K, V>> coarbIdentityHashMap(final Coarbitrary<K> ck,
final Coarbitrary<V> cv) {
return new Coarbitrary<IdentityHashMap<K, V>>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public <B> Gen<B> coarbitrary(final IdentityHashMap<K, V> m, final Gen<B> g) {
return coarbHashtable(ck, cv).coarbitrary(new Hashtable<K, V>(m), g);
}
};
}
/**
* A coarbitrary for linked hash maps.
*
* @param ck A coarbitrary for the map keys.
* @param cv A coarbitrary for the map values.
* @return A coarbitrary for linked hash maps.
*/
public static <K, V> Coarbitrary<LinkedHashMap<K, V>> coarbLinkedHashMap(final Coarbitrary<K> ck,
final Coarbitrary<V> cv) {
return new Coarbitrary<LinkedHashMap<K, V>>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public <B> Gen<B> coarbitrary(final LinkedHashMap<K, V> m, final Gen<B> g) {
return coarbHashtable(ck, cv).coarbitrary(new Hashtable<K, V>(m), g);
}
};
}
/**
* A coarbitrary for linked hash sets.
*
* @param c A coarbitrary for the elements of the linked hash set.
* @return A coarbitrary for linked hash sets.
*/
public static <A> Coarbitrary<LinkedHashSet<A>> coarbLinkedHashSet(final Coarbitrary<A> c) {
return new Coarbitrary<LinkedHashSet<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final LinkedHashSet<A> as, final Gen<B> g) {
return coarbHashSet(c).coarbitrary(new HashSet<A>(as), g);
}
};
}
/**
* A coarbitrary for linked lists.
*
* @param c A coarbitrary for the elements of the linked list.
* @return A coarbitrary for linked lists.
*/
public static <A> Coarbitrary<LinkedList<A>> coarbLinkedList(final Coarbitrary<A> c) {
return new Coarbitrary<LinkedList<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final LinkedList<A> as, final Gen<B> g) {
return coarbArray(c).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
/**
* A coarbitrary for priority queues.
*
* @param c A coarbitrary for the elements of the priority queue.
* @return A coarbitrary for priority queues.
*/
public static <A> Coarbitrary<PriorityQueue<A>> coarbPriorityQueue(final Coarbitrary<A> c) {
return new Coarbitrary<PriorityQueue<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final PriorityQueue<A> as, final Gen<B> g) {
return coarbArray(c).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
/**
* A coarbitrary for properties.
*/
public static final Coarbitrary<Properties> coarbProperties = new Coarbitrary<Properties>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public <B> Gen<B> coarbitrary(final Properties p, final Gen<B> g) {
final Hashtable<String, String> t = new Hashtable<String, String>();
for (final Object s : p.keySet()) {
t.put((String) s, p.getProperty((String) s));
}
return coarbHashtable(coarbString, coarbString).coarbitrary(t, g);
}
};
/**
* A coarbitrary for stacks.
*
* @param c A coarbitrary for the elements of the stack.
* @return A coarbitrary for stacks.
*/
public static <A> Coarbitrary<Stack<A>> coarbStack(final Coarbitrary<A> c) {
return new Coarbitrary<Stack<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final Stack<A> as, final Gen<B> g) {
return coarbArray(c).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
/**
* A coarbitrary for tree maps.
*
* @param ck A coarbitrary for the map keys.
* @param cv A coarbitrary for the map values.
* @return A coarbitrary for tree maps.
*/
public static <K, V> Coarbitrary<TreeMap<K, V>> coarbTreeMap(final Coarbitrary<K> ck, final Coarbitrary<V> cv) {
return new Coarbitrary<TreeMap<K, V>>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public <B> Gen<B> coarbitrary(final TreeMap<K, V> m, final Gen<B> g) {
return coarbHashtable(ck, cv).coarbitrary(new Hashtable<K, V>(m), g);
}
};
}
/**
* A coarbitrary for tree sets.
*
* @param c A coarbitrary for the elements of the tree set.
* @return A coarbitrary for tree sets.
*/
public static <A> Coarbitrary<TreeSet<A>> coarbTreeSet(final Coarbitrary<A> c) {
return new Coarbitrary<TreeSet<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final TreeSet<A> as, final Gen<B> g) {
return coarbHashSet(c).coarbitrary(new HashSet<A>(as), g);
}
};
}
/**
* A coarbitrary for vectors.
*
* @param c A coarbitrary for the elements of the vector.
* @return A coarbitrary for vectors.
*/
public static <A> Coarbitrary<Vector<A>> coarbVector(final Coarbitrary<A> c) {
return new Coarbitrary<Vector<A>>() {
@SuppressWarnings({"unchecked", "UseOfObsoleteCollectionType"})
public <B> Gen<B> coarbitrary(final Vector<A> as, final Gen<B> g) {
return coarbArray(c).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
/**
* A coarbitrary for weak hash maps.
*
* @param ck A coarbitrary for the map keys.
* @param cv A coarbitrary for the map values.
* @return A coarbitrary for weak hash maps.
*/
public static <K, V> Coarbitrary<WeakHashMap<K, V>> coarbWeakHashMap(final Coarbitrary<K> ck,
final Coarbitrary<V> cv) {
return new Coarbitrary<WeakHashMap<K, V>>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public <B> Gen<B> coarbitrary(final WeakHashMap<K, V> m, final Gen<B> g) {
return coarbHashtable(ck, cv).coarbitrary(new Hashtable<K, V>(m), g);
}
};
}
// END java.util
// BEGIN java.util.concurrent
/**
* A coarbitrary for array blocking queues.
*
* @param c A coarbitrary for the elements of the array blocking queue.
* @return A coarbitrary for array blocking queues.
*/
public static <A> Coarbitrary<ArrayBlockingQueue<A>> coarbArrayBlockingQueue(final Coarbitrary<A> c) {
return new Coarbitrary<ArrayBlockingQueue<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final ArrayBlockingQueue<A> as, final Gen<B> g) {
return coarbArray(c).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
/**
* A coarbitrary for concurrent hash maps.
*
* @param ck A coarbitrary for the map keys.
* @param cv A coarbitrary for the map values.
* @return A coarbitrary for concurrent hash maps.
*/
public static <K, V> Coarbitrary<ConcurrentHashMap<K, V>> coarbConcurrentHashMap(final Coarbitrary<K> ck,
final Coarbitrary<V> cv) {
return new Coarbitrary<ConcurrentHashMap<K, V>>() {
@SuppressWarnings({"UseOfObsoleteCollectionType"})
public <B> Gen<B> coarbitrary(final ConcurrentHashMap<K, V> m, final Gen<B> g) {
return coarbHashtable(ck, cv).coarbitrary(new Hashtable<K, V>(m), g);
}
};
}
/**
* A coarbitrary for concurrent linked queues.
*
* @param c A coarbitrary for the elements of the concurrent linked queue.
* @return A coarbitrary for concurrent linked queues.
*/
public static <A> Coarbitrary<ConcurrentLinkedQueue<A>> coarbConcurrentLinkedQueue(final Coarbitrary<A> c) {
return new Coarbitrary<ConcurrentLinkedQueue<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final ConcurrentLinkedQueue<A> as, final Gen<B> g) {
return coarbArray(c).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
/**
* A coarbitrary for copy-on-write array lists.
*
* @param c A coarbitrary for the elements of the copy-on-write array list.
* @return A coarbitrary for copy-on-write array lists.
*/
public static <A> Coarbitrary<CopyOnWriteArrayList<A>> coarbCopyOnWriteArrayList(final Coarbitrary<A> c) {
return new Coarbitrary<CopyOnWriteArrayList<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final CopyOnWriteArrayList<A> as, final Gen<B> g) {
return coarbArray(c).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
/**
* A coarbitrary for copy-on-write array sets.
*
* @param c A coarbitrary for the elements of the copy-on-write array set.
* @return A coarbitrary for copy-on-write array sets.
*/
public static <A> Coarbitrary<CopyOnWriteArraySet<A>> coarbCopyOnWriteArraySet(final Coarbitrary<A> c) {
return new Coarbitrary<CopyOnWriteArraySet<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final CopyOnWriteArraySet<A> as, final Gen<B> g) {
return coarbArray(c).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
/**
* A coarbitrary for delay queues.
*
* @param c A coarbitrary for the elements of the delay queue.
* @return A coarbitrary for delay queues.
*/
public static <A extends Delayed> Coarbitrary<DelayQueue<A>> coarbDelayQueue(final Coarbitrary<A> c) {
return new Coarbitrary<DelayQueue<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final DelayQueue<A> as, final Gen<B> g) {
return coarbArray(c).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
/**
* A coarbitrary for linked blocking queues.
*
* @param c A coarbitrary for the elements of the linked blocking queue.
* @return A coarbitrary for linked blocking queues.
*/
public static <A> Coarbitrary<LinkedBlockingQueue<A>> coarbLinkedBlockingQueue(final Coarbitrary<A> c) {
return new Coarbitrary<LinkedBlockingQueue<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final LinkedBlockingQueue<A> as, final Gen<B> g) {
return coarbArray(c).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
/**
* A coarbitrary for priority blocking queues.
*
* @param c A coarbitrary for the elements of the priority blocking queue.
* @return A coarbitrary for priority blocking queues.
*/
public static <A> Coarbitrary<PriorityBlockingQueue<A>> coarbPriorityBlockingQueue(final Coarbitrary<A> c) {
return new Coarbitrary<PriorityBlockingQueue<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final PriorityBlockingQueue<A> as, final Gen<B> g) {
return coarbArray(c).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
/**
* A coarbitrary for synchronous queues.
*
* @param c A coarbitrary for the elements of the synchronous queue.
* @return A coarbitrary for synchronous queues.
*/
public static <A> Coarbitrary<SynchronousQueue<A>> coarbSynchronousQueue(final Coarbitrary<A> c) {
return new Coarbitrary<SynchronousQueue<A>>() {
@SuppressWarnings({"unchecked"})
public <B> Gen<B> coarbitrary(final SynchronousQueue<A> as, final Gen<B> g) {
return coarbArray(c).coarbitrary(array(as.toArray((A[]) new Object[as.size()])), g);
}
};
}
// END java.util.concurrent
// BEGIN java.sql
public static final Coarbitrary<java.sql.Date> coarbSQLDate = new Coarbitrary<java.sql.Date>() {
public <B> Gen<B> coarbitrary(final java.sql.Date d, final Gen<B> g) {
return coarbLong.coarbitrary(d.getTime(), g);
}
};
public static final Coarbitrary<Timestamp> coarbTimestamp = new Coarbitrary<Timestamp>() {
public <B> Gen<B> coarbitrary(final Timestamp t, final Gen<B> g) {
return coarbLong.coarbitrary(t.getTime(), g);
}
};
public static final Coarbitrary<Time> coarbTime = new Coarbitrary<Time>() {
public <B> Gen<B> coarbitrary(final Time t, final Gen<B> g) {
return coarbLong.coarbitrary(t.getTime(), g);
}
};
// END java.sql
// BEGIN java.math
public static final Coarbitrary<BigInteger> coarbBigInteger = new Coarbitrary<BigInteger>() {
public <B> Gen<B> coarbitrary(final BigInteger i, final Gen<B> g) {
return variant((i.compareTo(BigInteger.ZERO) >= 0 ?
i.multiply(BigInteger.valueOf(2L)) :
i.multiply(BigInteger.valueOf(-2L).add(BigInteger.ONE))).longValue(), g);
}
};