forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCogen.java
More file actions
1184 lines (1088 loc) · 41.8 KB
/
Cogen.java
File metadata and controls
1184 lines (1088 loc) · 41.8 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.*;
import static fj.Function.curry;
import static fj.P.p;
import fj.data.*;
import static fj.data.Array.array;
import static fj.data.Array.iterableArray;
import static fj.data.List.fromString;
import static fj.data.List.nil;
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.Map;
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 Gen gen} functions.
*
* @version %build.number%
*/
public abstract class Cogen<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> cogen(A a, Gen<B> g);
/**
* A curried version of {@link #cogen(Object, Gen)}.
*
* @param a The value to produce the generator from.
* @return A curried version of {@link #cogen(Object, Gen)}.
*/
public final <B> F<Gen<B>, Gen<B>> cogen(final A a) {
return g -> cogen(a, g);
}
/**
* Composes the given function with this cogen to produce a new cogen.
*
* @param f The function to compose.
* @return A new cogen composed with the given function.
*/
public final <B> Cogen<B> compose(final F<B, A> f) {
return new Cogen<B>() {
public <X> Gen<X> cogen(final B b, final Gen<X> g) {
return Cogen.this.cogen(f.f(b), g);
}
};
}
/**
* Contra-maps this cogen using the given function.
*
* @param f The function to co-map with.
* @return A contra-mapped cogen.
*/
public final <B> Cogen<B> contramap(final F<B, A> f) {
return new Cogen<B>() {
public <X> Gen<X> cogen(final B b, final Gen<X> g) {
return Cogen.this.cogen(f.f(b), g);
}
};
}
/**
* A cogen for a function.
*
* @param a A gen for the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function.
*/
public static <A, B> Cogen<F<A, B>> cogenF(final Gen<A> a, final Cogen<B> c) {
return new Cogen<F<A, B>>() {
public <X> Gen<X> cogen(final F<A, B> f, final Gen<X> g) {
return a.bind(a1 -> c.cogen(f.f(a1), g));
}
};
}
/**
* A cogen for a function-2.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-2.
*/
public static <A, B, C> Cogen<F2<A, B, C>> cogenF2(final Gen<A> aa, final Gen<B> ab,
final Cogen<C> c) {
return new Cogen<F2<A, B, C>>() {
public <X> Gen<X> cogen(final F2<A, B, C> f, final Gen<X> g) {
return cogenF(aa, cogenF(ab, c)).cogen(curry(f), g);
}
};
}
/**
* A cogen for a function-3.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param ac A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-3.
*/
public static <A, B, C, D> Cogen<F3<A, B, C, D>> cogenF3(final Gen<A> aa, final Gen<B> ab,
final Gen<C> ac, final Cogen<D> c) {
return new Cogen<F3<A, B, C, D>>() {
public <X> Gen<X> cogen(final F3<A, B, C, D> f, final Gen<X> g) {
return cogenF(aa, cogenF(ab, cogenF(ac, c))).cogen(curry(f), g);
}
};
}
/**
* A cogen for a function-4.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param ac A gen for part of the domain of the function.
* @param ad A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-4.
*/
public static <A, B, C, D, E> Cogen<F4<A, B, C, D, E>> cogenF4(final Gen<A> aa, final Gen<B> ab,
final Gen<C> ac, final Gen<D> ad,
final Cogen<E> c) {
return new Cogen<F4<A, B, C, D, E>>() {
public <X> Gen<X> cogen(final F4<A, B, C, D, E> f, final Gen<X> g) {
return cogenF(aa, cogenF(ab, cogenF(ac, cogenF(ad, c)))).cogen(curry(f), g);
}
};
}
/**
* A cogen for a function-5.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param ac A gen for part of the domain of the function.
* @param ad A gen for part of the domain of the function.
* @param ae A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-5.
*/
public static <A, B, C, D, E, F$> Cogen<F5<A, B, C, D, E, F$>> cogenF5(final Gen<A> aa,
final Gen<B> ab,
final Gen<C> ac,
final Gen<D> ad,
final Gen<E> ae,
final Cogen<F$> c) {
return new Cogen<F5<A, B, C, D, E, F$>>() {
public <X> Gen<X> cogen(final F5<A, B, C, D, E, F$> f, final Gen<X> g) {
return cogenF(aa, cogenF(ab, cogenF(ac, cogenF(ad, cogenF(ae, c))))).cogen(curry(f), g);
}
};
}
/**
* A cogen for a function-6.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param ac A gen for part of the domain of the function.
* @param ad A gen for part of the domain of the function.
* @param ae A gen for part of the domain of the function.
* @param af A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-6.
*/
public static <A, B, C, D, E, F$, G> Cogen<F6<A, B, C, D, E, F$, G>> cogenF6(final Gen<A> aa,
final Gen<B> ab,
final Gen<C> ac,
final Gen<D> ad,
final Gen<E> ae,
final Gen<F$> af,
final Cogen<G> c) {
return new Cogen<F6<A, B, C, D, E, F$, G>>() {
public <X> Gen<X> cogen(final F6<A, B, C, D, E, F$, G> f, final Gen<X> g) {
return cogenF(aa, cogenF(ab, cogenF(ac, cogenF(ad, cogenF(ae, cogenF(af, c)))))).cogen(curry(f), g);
}
};
}
/**
* A cogen for a function-7.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param ac A gen for part of the domain of the function.
* @param ad A gen for part of the domain of the function.
* @param ae A gen for part of the domain of the function.
* @param af A gen for part of the domain of the function.
* @param ag A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-7.
*/
public static <A, B, C, D, E, F$, G, H> Cogen<F7<A, B, C, D, E, F$, G, H>> cogenF7(final Gen<A> aa,
final Gen<B> ab,
final Gen<C> ac,
final Gen<D> ad,
final Gen<E> ae,
final Gen<F$> af,
final Gen<G> ag,
final Cogen<H> c) {
return new Cogen<F7<A, B, C, D, E, F$, G, H>>() {
public <X> Gen<X> cogen(final F7<A, B, C, D, E, F$, G, H> f, final Gen<X> g) {
return cogenF(aa, cogenF(ab, cogenF(ac, cogenF(ad, cogenF(ae, cogenF(af, cogenF(ag, c)))))))
.cogen(curry(f), g);
}
};
}
/**
* A cogen for a function-8.
*
* @param aa A gen for part of the domain of the function.
* @param ab A gen for part of the domain of the function.
* @param ac A gen for part of the domain of the function.
* @param ad A gen for part of the domain of the function.
* @param ae A gen for part of the domain of the function.
* @param af A gen for part of the domain of the function.
* @param ag A gen for part of the domain of the function.
* @param ah A gen for part of the domain of the function.
* @param c A cogen for the codomain of the function.
* @return A cogen for a function-8.
*/
public static <A, B, C, D, E, F$, G, H, I> Cogen<F8<A, B, C, D, E, F$, G, H, I>> cogenF8(final Gen<A> aa,
final Gen<B> ab,
final Gen<C> ac,
final Gen<D> ad,
final Gen<E> ae,
final Gen<F$> af,
final Gen<G> ag,
final Gen<H> ah,
final Cogen<I> c) {
return new Cogen<F8<A, B, C, D, E, F$, G, H, I>>() {
public <X> Gen<X> cogen(final F8<A, B, C, D, E, F$, G, H, I> f, final Gen<X> g) {
return cogenF(aa, cogenF(ab, cogenF(ac, cogenF(ad, cogenF(ae, cogenF(af, cogenF(ag, cogenF(ah, c))))))))
.cogen(curry(f), g);
}
};
}
/**
* A cogen for booleans.
*/
public static final Cogen<Boolean> cogenBoolean = new Cogen<Boolean>() {
public <B> Gen<B> cogen(final Boolean b, final Gen<B> g) {
return variant(b ? 0 : 1, g);
}
};
/**
* A cogen for integers.
*/
public static final Cogen<Integer> cogenInteger = new Cogen<Integer>() {
public <B> Gen<B> cogen(final Integer i, final Gen<B> g) {
return variant(i >= 0 ? 2 * i : -2 * i + 1, g);
}
};
/**
* A cogen for bytes.
*/
public static final Cogen<Byte> cogenByte = new Cogen<Byte>() {
public <B> Gen<B> cogen(final Byte b, final Gen<B> g) {
return variant(b >= 0 ? 2 * b : -2 * b + 1, g);
}
};
/**
* A cogen for shorts.
*/
public static final Cogen<Short> cogenShort = new Cogen<Short>() {
public <B> Gen<B> cogen(final Short s, final Gen<B> g) {
return variant(s >= 0 ? 2 * s : -2 * s + 1, g);
}
};
/**
* A cogen for longs.
*/
public static final Cogen<Long> cogenLong = new Cogen<Long>() {
public <B> Gen<B> cogen(final Long l, final Gen<B> g) {
return variant(l >= 0L ? 2L * l : -2L * l + 1L, g);
}
};
/**
* A cogen for characters.
*/
public static final Cogen<Character> cogenCharacter = new Cogen<Character>() {
public <B> Gen<B> cogen(final Character c, final Gen<B> g) {
return variant(c << 1, g);
}
};
/**
* A cogen for floats.
*/
public static final Cogen<Float> cogenFloat = new Cogen<Float>() {
public <B> Gen<B> cogen(final Float f, final Gen<B> g) {
return cogenInteger.cogen(floatToRawIntBits(f), g);
}
};
/**
* A cogen for doubles.
*/
public static final Cogen<Double> cogenDouble = new Cogen<Double>() {
public <B> Gen<B> cogen(final Double d, final Gen<B> g) {
return cogenLong.cogen(doubleToRawLongBits(d), g);
}
};
/**
* A cogen for the optional value.
*
* @param ca A cogen for the type of the optional value.
* @return A cogen for the optional value.
*/
public static <A> Cogen<Option<A>> cogenOption(final Cogen<A> ca) {
return new Cogen<Option<A>>() {
public <B> Gen<B> cogen(final Option<A> o, final Gen<B> g) {
return o.isNone() ? variant(0, g) : variant(1, ca.cogen(o.some(), g));
}
};
}
/**
* A cogen for the disjoint union.
*
* @param ca A cogen for one side of the disjoint union.
* @param cb A cogen for one side of the disjoint union.
* @return A cogen for the disjoint union.
*/
public static <A, B> Cogen<Either<A, B>> cogenEither(final Cogen<A> ca, final Cogen<B> cb) {
return new Cogen<Either<A, B>>() {
public <X> Gen<X> cogen(final Either<A, B> e, final Gen<X> g) {
return e.isLeft() ?
variant(0, ca.cogen(e.left().value(), g)) :
variant(1, cb.cogen(e.right().value(), g));
}
};
}
/**
* A cogen for lists.
*
* @param ca A cogen for the elements of the list.
* @return A cogen for lists.
*/
public static <A> Cogen<List<A>> cogenList(final Cogen<A> ca) {
return new Cogen<List<A>>() {
public <B> Gen<B> cogen(final List<A> as, final Gen<B> g) {
return as.isEmpty() ?
variant(0, g) :
variant(1, ca.cogen(as.head(), cogen(as.tail(), g)));
}
};
}
/**
* A cogen for strings.
*/
public static final Cogen<String> cogenString = new Cogen<String>() {
public <B> Gen<B> cogen(final String s, final Gen<B> g) {
return cogenList(cogenCharacter).cogen(fromString(s), g);
}
};
/**
* A cogen for string buffers.
*/
public static final Cogen<StringBuffer> cogenStringBuffer = new Cogen<StringBuffer>() {
public <B> Gen<B> cogen(final StringBuffer s, final Gen<B> g) {
return cogenString.cogen(s.toString(), g);
}
};
/**
* A cogen for string builders.
*/
public static final Cogen<StringBuilder> cogenStringBuilder = new Cogen<StringBuilder>() {
public <B> Gen<B> cogen(final StringBuilder s, final Gen<B> g) {
return cogenString.cogen(s.toString(), g);
}
};
/**
* A cogen for streams.
*
* @param ca A cogen for the elements of the stream.
* @return A cogen for streams.
*/
public static <A> Cogen<Stream<A>> cogenStream(final Cogen<A> ca) {
return new Cogen<Stream<A>>() {
public <B> Gen<B> cogen(final Stream<A> as, final Gen<B> g) {
return as.isEmpty() ?
variant(0, g) :
variant(1, ca.cogen(as.head(), cogen(as.tail()._1(), g)));
}
};
}
/**
* A cogen for the provided LcgRng
* @return A cogen for the provided LcgRng.
*/
public static Cogen<LcgRng> cogenLcgRng() {
return new Cogen<LcgRng>() {
@Override
public <B> Gen<B> cogen(LcgRng rng, Gen<B> g) {
long i = rng.getSeed();
return variant(i >= 0 ? 2 * i : -2 * i + 1, g);
}
};
}
/**
* A cogen for state.
*/
public static <S, A> Cogen<State<S, A>> cogenState(Gen<S> as, F2<S, A, Long> f) {
return new Cogen<State<S, A>>() {
@Override
public <B> Gen<B> cogen(State<S, A> s1, Gen<B> g) {
return as.bind(r -> {
P2<S, A> p = s1.run(r);
return variant(f.f(p._1(), p._2()), g);
});
}
};
}
/**
* A cogen for arrays.
*
* @param ca A cogen for the elements of the array.
* @return A cogen for arrays.
*/
public static <A> Cogen<Array<A>> cogenArray(final Cogen<A> ca) {
return new Cogen<Array<A>>() {
public <B> Gen<B> cogen(final Array<A> as, final Gen<B> g) {
return cogenList(ca).cogen(as.toList(), g);
}
};
}
/**
* A cogen for throwables.
*
* @param cs A cogen for the throwable message.
* @return A cogen for throwables.
*/
public static Cogen<Throwable> cogenThrowable(final Cogen<String> cs) {
return cs.contramap(new F<Throwable, String>() {
public String f(final Throwable t) {
return t.getMessage();
}
});
}
/**
* A cogen for throwables.
*/
public static final Cogen<Throwable> cogenThrowable =
cogenThrowable(cogenString);
// BEGIN java.util
/**
* A cogen for array lists.
*
* @param ca A cogen for the elements of the array list.
* @return A cogen for array lists.
*/
public static <A> Cogen<ArrayList<A>> cogenArrayList(final Cogen<A> ca) {
return new Cogen<ArrayList<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final ArrayList<A> as, final Gen<B> g) {
return cogenArray(ca).cogen(iterableArray(as), g);
}
};
}
/**
* A cogen for bit sets.
*/
public static final Cogen<BitSet> cogenBitSet = new Cogen<BitSet>() {
public <B> Gen<B> cogen(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 cogenList(cogenBoolean).cogen(x, g);
}
};
/**
* A cogen for calendars.
*/
public static final Cogen<Calendar> cogenCalendar = new Cogen<Calendar>() {
public <B> Gen<B> cogen(final Calendar c, final Gen<B> g) {
return cogenLong.cogen(c.getTime().getTime(), g);
}
};
/**
* A cogen for dates.
*/
public static final Cogen<Date> cogenDate = new Cogen<Date>() {
public <B> Gen<B> cogen(final Date d, final Gen<B> g) {
return cogenLong.cogen(d.getTime(), g);
}
};
/**
* A cogen for enum maps.
*
* @param ck A cogen for the map keys.
* @param cv A cogen for the map values.
* @return A cogen for enum maps.
*/
public static <K extends Enum<K>, V> Cogen<EnumMap<K, V>> cogenEnumMap(final Cogen<K> ck,
final Cogen<V> cv) {
return new Cogen<EnumMap<K, V>>() {
@SuppressWarnings("UseOfObsoleteCollectionType")
public <B> Gen<B> cogen(final EnumMap<K, V> m, final Gen<B> g) {
return cogenHashtable(ck, cv).cogen(new Hashtable<>(m), g);
}
};
}
/**
* A cogen for enum sets.
*
* @param c A cogen for the elements of the enum set.
* @return A cogen for enum sets.
*/
public static <A extends Enum<A>> Cogen<EnumSet<A>> cogenEnumSet(final Cogen<A> c) {
return new Cogen<EnumSet<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final EnumSet<A> as, final Gen<B> g) {
return cogenHashSet(c).cogen(new HashSet<>(as), g);
}
};
}
/**
* A cogen for gregorian calendars.
*/
public static final Cogen<GregorianCalendar> cogenGregorianCalendar = new Cogen<GregorianCalendar>() {
public <B> Gen<B> cogen(final GregorianCalendar c, final Gen<B> g) {
return cogenLong.cogen(c.getTime().getTime(), g);
}
};
/**
* A cogen for hash maps.
*
* @param ck A cogen for the map keys.
* @param cv A cogen for the map values.
* @return A cogen for hash maps.
*/
public static <K, V> Cogen<HashMap<K, V>> cogenHashMap(final Cogen<K> ck, final Cogen<V> cv) {
return new Cogen<HashMap<K, V>>() {
@SuppressWarnings("UseOfObsoleteCollectionType")
public <B> Gen<B> cogen(final HashMap<K, V> m, final Gen<B> g) {
return cogenHashtable(ck, cv).cogen(new Hashtable<>(m), g);
}
};
}
/**
* A cogen for hash sets.
*
* @param c A cogen for the elements of the hash set.
* @return A cogen for hash sets.
*/
public static <A> Cogen<HashSet<A>> cogenHashSet(final Cogen<A> c) {
return new Cogen<HashSet<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final HashSet<A> as, final Gen<B> g) {
return cogenArray(c).cogen(iterableArray(as), g);
}
};
}
/**
* A cogen for hash tables.
*
* @param ck A cogen for the map keys.
* @param cv A cogen for the map values.
* @return A cogen for hash tables.
*/
public static <K, V> Cogen<Hashtable<K, V>> cogenHashtable(final Cogen<K> ck, final Cogen<V> cv) {
return new Cogen<Hashtable<K, V>>() {
@SuppressWarnings("UseOfObsoleteCollectionType")
public <B> Gen<B> cogen(final Hashtable<K, V> h, final Gen<B> g) {
List<P2<K, V>> x = nil();
for (final Map.Entry<K, V> entry : h.entrySet()) {
x = x.snoc(p(entry.getKey(), entry.getValue()));
}
return cogenList(cogenP2(ck, cv)).cogen(x, g);
}
};
}
/**
* A cogen for identity hash maps.
*
* @param ck A cogen for the map keys.
* @param cv A cogen for the map values.
* @return A cogen for identity hash maps.
*/
public static <K, V> Cogen<IdentityHashMap<K, V>> cogenIdentityHashMap(final Cogen<K> ck,
final Cogen<V> cv) {
return new Cogen<IdentityHashMap<K, V>>() {
@SuppressWarnings("UseOfObsoleteCollectionType")
public <B> Gen<B> cogen(final IdentityHashMap<K, V> m, final Gen<B> g) {
return cogenHashtable(ck, cv).cogen(new Hashtable<>(m), g);
}
};
}
/**
* A cogen for linked hash maps.
*
* @param ck A cogen for the map keys.
* @param cv A cogen for the map values.
* @return A cogen for linked hash maps.
*/
public static <K, V> Cogen<LinkedHashMap<K, V>> cogenLinkedHashMap(final Cogen<K> ck,
final Cogen<V> cv) {
return new Cogen<LinkedHashMap<K, V>>() {
@SuppressWarnings("UseOfObsoleteCollectionType")
public <B> Gen<B> cogen(final LinkedHashMap<K, V> m, final Gen<B> g) {
return cogenHashtable(ck, cv).cogen(new Hashtable<>(m), g);
}
};
}
/**
* A cogen for linked hash sets.
*
* @param c A cogen for the elements of the linked hash set.
* @return A cogen for linked hash sets.
*/
public static <A> Cogen<LinkedHashSet<A>> cogenLinkedHashSet(final Cogen<A> c) {
return new Cogen<LinkedHashSet<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final LinkedHashSet<A> as, final Gen<B> g) {
return cogenHashSet(c).cogen(new HashSet<>(as), g);
}
};
}
/**
* A cogen for linked lists.
*
* @param c A cogen for the elements of the linked list.
* @return A cogen for linked lists.
*/
public static <A> Cogen<LinkedList<A>> cogenLinkedList(final Cogen<A> c) {
return new Cogen<LinkedList<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final LinkedList<A> as, final Gen<B> g) {
return cogenArray(c).cogen(iterableArray(as), g);
}
};
}
/**
* A cogen for priority queues.
*
* @param c A cogen for the elements of the priority queue.
* @return A cogen for priority queues.
*/
public static <A> Cogen<PriorityQueue<A>> cogenPriorityQueue(final Cogen<A> c) {
return new Cogen<PriorityQueue<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final PriorityQueue<A> as, final Gen<B> g) {
return cogenArray(c).cogen(iterableArray(as), g);
}
};
}
/**
* A cogen for properties.
*/
public static final Cogen<Properties> cogenProperties = new Cogen<Properties>() {
@SuppressWarnings("UseOfObsoleteCollectionType")
public <B> Gen<B> cogen(final Properties p, final Gen<B> g) {
final Hashtable<String, String> t = new Hashtable<>();
for (final Object s : p.keySet()) {
t.put((String) s, p.getProperty((String) s));
}
return cogenHashtable(cogenString, cogenString).cogen(t, g);
}
};
/**
* A cogen for stacks.
*
* @param c A cogen for the elements of the stack.
* @return A cogen for stacks.
*/
public static <A> Cogen<Stack<A>> cogenStack(final Cogen<A> c) {
return new Cogen<Stack<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final Stack<A> as, final Gen<B> g) {
return cogenArray(c).cogen(iterableArray(as), g);
}
};
}
/**
* A cogen for tree maps.
*
* @param ck A cogen for the map keys.
* @param cv A cogen for the map values.
* @return A cogen for tree maps.
*/
public static <K, V> Cogen<TreeMap<K, V>> cogenTreeMap(final Cogen<K> ck, final Cogen<V> cv) {
return new Cogen<TreeMap<K, V>>() {
@SuppressWarnings("UseOfObsoleteCollectionType")
public <B> Gen<B> cogen(final TreeMap<K, V> m, final Gen<B> g) {
return cogenHashtable(ck, cv).cogen(new Hashtable<>(m), g);
}
};
}
/**
* A cogen for tree sets.
*
* @param c A cogen for the elements of the tree set.
* @return A cogen for tree sets.
*/
public static <A> Cogen<TreeSet<A>> cogenTreeSet(final Cogen<A> c) {
return new Cogen<TreeSet<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final TreeSet<A> as, final Gen<B> g) {
return cogenHashSet(c).cogen(new HashSet<>(as), g);
}
};
}
/**
* A cogen for vectors.
*
* @param c A cogen for the elements of the vector.
* @return A cogen for vectors.
*/
public static <A> Cogen<Vector<A>> cogenVector(final Cogen<A> c) {
return new Cogen<Vector<A>>() {
@SuppressWarnings({"unchecked", "UseOfObsoleteCollectionType"})
public <B> Gen<B> cogen(final Vector<A> as, final Gen<B> g) {
return cogenArray(c).cogen(iterableArray(as), g);
}
};
}
/**
* A cogen for weak hash maps.
*
* @param ck A cogen for the map keys.
* @param cv A cogen for the map values.
* @return A cogen for weak hash maps.
*/
public static <K, V> Cogen<WeakHashMap<K, V>> cogenWeakHashMap(final Cogen<K> ck,
final Cogen<V> cv) {
return new Cogen<WeakHashMap<K, V>>() {
@SuppressWarnings("UseOfObsoleteCollectionType")
public <B> Gen<B> cogen(final WeakHashMap<K, V> m, final Gen<B> g) {
return cogenHashtable(ck, cv).cogen(new Hashtable<>(m), g);
}
};
}
// END java.util
// BEGIN java.util.concurrent
/**
* A cogen for array blocking queues.
*
* @param c A cogen for the elements of the array blocking queue.
* @return A cogen for array blocking queues.
*/
public static <A> Cogen<ArrayBlockingQueue<A>> cogenArrayBlockingQueue(final Cogen<A> c) {
return new Cogen<ArrayBlockingQueue<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final ArrayBlockingQueue<A> as, final Gen<B> g) {
return cogenArray(c).cogen(iterableArray(as), g);
}
};
}
/**
* A cogen for concurrent hash maps.
*
* @param ck A cogen for the map keys.
* @param cv A cogen for the map values.
* @return A cogen for concurrent hash maps.
*/
public static <K, V> Cogen<ConcurrentHashMap<K, V>> cogenConcurrentHashMap(final Cogen<K> ck,
final Cogen<V> cv) {
return new Cogen<ConcurrentHashMap<K, V>>() {
@SuppressWarnings("UseOfObsoleteCollectionType")
public <B> Gen<B> cogen(final ConcurrentHashMap<K, V> m, final Gen<B> g) {
return cogenHashtable(ck, cv).cogen(new Hashtable<>(m), g);
}
};
}
/**
* A cogen for concurrent linked queues.
*
* @param c A cogen for the elements of the concurrent linked queue.
* @return A cogen for concurrent linked queues.
*/
public static <A> Cogen<ConcurrentLinkedQueue<A>> cogenConcurrentLinkedQueue(final Cogen<A> c) {
return new Cogen<ConcurrentLinkedQueue<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final ConcurrentLinkedQueue<A> as, final Gen<B> g) {
return cogenArray(c).cogen(iterableArray(as), g);
}
};
}
/**
* A cogen for copy-on-write array lists.
*
* @param c A cogen for the elements of the copy-on-write array list.
* @return A cogen for copy-on-write array lists.
*/
public static <A> Cogen<CopyOnWriteArrayList<A>> cogenCopyOnWriteArrayList(final Cogen<A> c) {
return new Cogen<CopyOnWriteArrayList<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final CopyOnWriteArrayList<A> as, final Gen<B> g) {
return cogenArray(c).cogen(iterableArray(as), g);
}
};
}
/**
* A cogen for copy-on-write array sets.
*
* @param c A cogen for the elements of the copy-on-write array set.
* @return A cogen for copy-on-write array sets.
*/
public static <A> Cogen<CopyOnWriteArraySet<A>> cogenCopyOnWriteArraySet(final Cogen<A> c) {
return new Cogen<CopyOnWriteArraySet<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final CopyOnWriteArraySet<A> as, final Gen<B> g) {
return cogenArray(c).cogen(iterableArray(as), g);
}
};
}
/**
* A cogen for delay queues.
*
* @param c A cogen for the elements of the delay queue.
* @return A cogen for delay queues.
*/
public static <A extends Delayed> Cogen<DelayQueue<A>> cogenDelayQueue(final Cogen<A> c) {
return new Cogen<DelayQueue<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final DelayQueue<A> as, final Gen<B> g) {
return cogenArray(c).cogen(iterableArray(as), g);
}
};
}
/**
* A cogen for linked blocking queues.
*
* @param c A cogen for the elements of the linked blocking queue.
* @return A cogen for linked blocking queues.
*/
public static <A> Cogen<LinkedBlockingQueue<A>> cogenLinkedBlockingQueue(final Cogen<A> c) {
return new Cogen<LinkedBlockingQueue<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final LinkedBlockingQueue<A> as, final Gen<B> g) {
return cogenArray(c).cogen(iterableArray(as), g);
}
};
}
/**
* A cogen for priority blocking queues.
*
* @param c A cogen for the elements of the priority blocking queue.
* @return A cogen for priority blocking queues.
*/
public static <A> Cogen<PriorityBlockingQueue<A>> cogenPriorityBlockingQueue(final Cogen<A> c) {
return new Cogen<PriorityBlockingQueue<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final PriorityBlockingQueue<A> as, final Gen<B> g) {
return cogenArray(c).cogen(iterableArray(as), g);
}
};
}
/**
* A cogen for synchronous queues.
*
* @param c A cogen for the elements of the synchronous queue.
* @return A cogen for synchronous queues.
*/
public static <A> Cogen<SynchronousQueue<A>> cogenSynchronousQueue(final Cogen<A> c) {
return new Cogen<SynchronousQueue<A>>() {
@SuppressWarnings("unchecked")
public <B> Gen<B> cogen(final SynchronousQueue<A> as, final Gen<B> g) {
return cogenArray(c).cogen(iterableArray(as), g);
}
};
}
// END java.util.concurrent
// BEGIN java.sql
public static final Cogen<java.sql.Date> cogenSQLDate = new Cogen<java.sql.Date>() {
public <B> Gen<B> cogen(final java.sql.Date d, final Gen<B> g) {
return cogenLong.cogen(d.getTime(), g);
}
};
public static final Cogen<Timestamp> cogenTimestamp = new Cogen<Timestamp>() {
public <B> Gen<B> cogen(final Timestamp t, final Gen<B> g) {
return cogenLong.cogen(t.getTime(), g);
}
};
public static final Cogen<Time> cogenTime = new Cogen<Time>() {
public <B> Gen<B> cogen(final Time t, final Gen<B> g) {
return cogenLong.cogen(t.getTime(), g);
}
};
// END java.sql
// BEGIN java.math