-
Notifications
You must be signed in to change notification settings - Fork 581
/
Functions.java
1218 lines (1028 loc) · 38.1 KB
/
Functions.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) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.block.factory;
import java.util.Map;
import org.eclipse.collections.api.block.SerializableComparator;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.comparator.primitive.BooleanFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.ByteFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.CharFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.DoubleFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.FloatFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.IntFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.LongFunctionComparator;
import org.eclipse.collections.impl.block.comparator.primitive.ShortFunctionComparator;
import org.eclipse.collections.impl.block.function.CaseFunction;
import org.eclipse.collections.impl.block.function.IfFunction;
import org.eclipse.collections.impl.block.function.checked.CheckedFunction;
import org.eclipse.collections.impl.block.function.checked.ThrowingFunction;
import org.eclipse.collections.impl.block.function.primitive.IntegerFunctionImpl;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.StringIterate;
public final class Functions
{
private static final Function<Double, Double> DOUBLE_PASS_THRU_FUNCTION = new DoublePassThruFunction();
private static final Function<Integer, Integer> INTEGER_PASS_THRU_FUNCTION = new IntegerPassThruFunction();
private static final Function<Long, Long> LONG_PASS_THRU_FUNCTION = new LongPassThruFunction();
private static final Function<Object, Boolean> TRUE_FUNCTION = new TrueFunction();
private static final Function<Object, Boolean> FALSE_FUNCTION = new FalseFunction();
private static final Function<?, ?> PASS_THRU_FUNCTION = new PassThruFunction<>();
private static final Function<String, String> STRING_TRIM_FUNCTION = new StringTrimFunction();
private static final Function<Object, Class<?>> CLASS_FUNCTION = new ClassFunction();
private static final Function<Number, Double> MATH_SIN_FUNCTION = new MathSinFunction();
private static final Function<Integer, Integer> SQUARED_INTEGER = new SquaredIntegerFunction();
private static final Function<Object, String> TO_STRING_FUNCTION = new ToStringFunction();
private static final Function<String, Integer> STRING_TO_INTEGER_FUNCTION = new StringToIntegerFunction();
private static final Function<?, ?> MAP_KEY_FUNCTION = new MapKeyFunction<>();
private static final Function<?, ?> MAP_VALUE_FUNCTION = new MapValueFunction<>();
private static final Function<Iterable<?>, Integer> SIZE_FUNCTION = new SizeFunction();
private static final FirstOfPairFunction<?> FIRST_OF_PAIR_FUNCTION = new FirstOfPairFunction<>();
private static final SecondOfPairFunction<?> SECOND_OF_PAIR_FUNCTION = new SecondOfPairFunction<>();
private static final CheckedFunction<String, Class<?>> CLASS_FOR_NAME = new ClassForNameFunction();
private static final SwappedPairFunction<?, ?> SWAPPED_PAIR_FUNCTION = new SwappedPairFunction<>();
private Functions()
{
throw new AssertionError("Suppress default constructor for noninstantiability");
}
private static class PassThruFunction<T> implements Function<T, T>
{
private static final long serialVersionUID = 1L;
@Override
public T valueOf(T anObject)
{
return anObject;
}
}
private static class StringTrimFunction implements Function<String, String>
{
private static final long serialVersionUID = 1L;
@Override
public String valueOf(String s)
{
return s.trim();
}
}
private static final class FixedValueFunction<T, V> implements Function<T, V>
{
private static final long serialVersionUID = 1L;
private final V value;
private FixedValueFunction(V value)
{
this.value = value;
}
@Override
public V valueOf(T object)
{
return this.value;
}
}
private static final class ClassFunction implements Function<Object, Class<?>>
{
private static final long serialVersionUID = 1L;
@Override
public Class<?> valueOf(Object anObject)
{
return anObject.getClass();
}
@Override
public String toString()
{
return "object.getClass()";
}
}
private static final class MathSinFunction implements Function<Number, Double>
{
private static final long serialVersionUID = 1L;
@Override
public Double valueOf(Number number)
{
return Math.sin(number.doubleValue());
}
@Override
public String toString()
{
return "Math.sin()";
}
}
private static final class SquaredIntegerFunction implements Function<Integer, Integer>
{
private static final long serialVersionUID = 1L;
@Override
public Integer valueOf(Integer value)
{
return value * value;
}
}
private static final class ToStringFunction implements Function<Object, String>
{
private static final long serialVersionUID = 1L;
@Override
public String valueOf(Object anObject)
{
return String.valueOf(anObject);
}
@Override
public String toString()
{
return "toString";
}
}
private static final class StringToIntegerFunction implements Function<String, Integer>
{
private static final long serialVersionUID = 1L;
@Override
public Integer valueOf(String string)
{
return Integer.valueOf(string);
}
@Override
public String toString()
{
return "stringToInteger";
}
}
/**
* Allows a lambda or anonymous inner class that needs to throw a checked exception to be safely wrapped as a
* Function that will throw a RuntimeException, wrapping the checked exception that is the cause.
*/
public static <T, V> Function<T, V> throwing(ThrowingFunction<T, V> throwingFunction)
{
return new ThrowingFunctionAdapter<>(throwingFunction);
}
/**
* Allows a lambda or anonymous inner class that needs to throw a checked exception to be safely wrapped as a
* Function that will throw a user specified RuntimeException based on the provided function. The function
* is passed the current element and the checked exception that was thrown as context arguments.
*/
public static <T, V> Function<T, V> throwing(
ThrowingFunction<T, V> throwingFunction,
Function2<T, ? super Throwable, ? extends RuntimeException> rethrow)
{
return each ->
{
try
{
return throwingFunction.safeValueOf(each);
}
catch (RuntimeException e)
{
throw e;
}
catch (Throwable t)
{
throw rethrow.value(each, t);
}
};
}
/**
* Allows a Java 8 lambda and method reference to be used in a method taking a Function as a parameter
* without any ambiguity.
*/
public static <T, V> Function<T, V> cast(Function<T, V> function)
{
return function;
}
/**
* Alias for identity(). Inlineable.
*
* @see #identity()
*/
public static <T> Function<T, T> getPassThru()
{
return Functions.identity();
}
/**
* @since 6.0
*/
public static <T> Function<T, T> identity()
{
return (Function<T, T>) PASS_THRU_FUNCTION;
}
/**
* @since 6.0
*/
public static Function<Object, Boolean> getTrue()
{
return TRUE_FUNCTION;
}
/**
* @since 6.0
*/
public static Function<Object, Boolean> getFalse()
{
return FALSE_FUNCTION;
}
public static <T, V> Function<T, V> getFixedValue(V value)
{
return new FixedValueFunction<>(value);
}
public static Function<Object, Class<?>> getToClass()
{
return CLASS_FUNCTION;
}
public static Function<Number, Double> getMathSinFunction()
{
return MATH_SIN_FUNCTION;
}
public static Function<Number, Number> getNumberPassThru()
{
return (Function<Number, Number>) PASS_THRU_FUNCTION;
}
public static Function<Integer, Integer> getIntegerPassThru()
{
return INTEGER_PASS_THRU_FUNCTION;
}
public static Function<Long, Long> getLongPassThru()
{
return LONG_PASS_THRU_FUNCTION;
}
public static Function<Double, Double> getDoublePassThru()
{
return DOUBLE_PASS_THRU_FUNCTION;
}
public static Function<String, String> getStringPassThru()
{
return (Function<String, String>) PASS_THRU_FUNCTION;
}
public static Function<String, String> getStringTrim()
{
return STRING_TRIM_FUNCTION;
}
public static Function<Object, String> getToString()
{
return TO_STRING_FUNCTION;
}
public static Function<Object, String> getNullSafeToString(String defaultValue)
{
return Functions.nullSafe(TO_STRING_FUNCTION, defaultValue);
}
public static <T> SerializableComparator<T> toBooleanComparator(BooleanFunction<T> function)
{
return new BooleanFunctionComparator<>(function);
}
public static <T> SerializableComparator<T> toByteComparator(ByteFunction<T> function)
{
return new ByteFunctionComparator<>(function);
}
public static <T> SerializableComparator<T> toCharComparator(CharFunction<T> function)
{
return new CharFunctionComparator<>(function);
}
public static <T> SerializableComparator<T> toFloatComparator(FloatFunction<T> function)
{
return new FloatFunctionComparator<>(function);
}
public static <T> SerializableComparator<T> toShortComparator(ShortFunction<T> function)
{
return new ShortFunctionComparator<>(function);
}
public static <T> SerializableComparator<T> toIntComparator(IntFunction<T> function)
{
return new IntFunctionComparator<>(function);
}
public static <T> SerializableComparator<T> toDoubleComparator(DoubleFunction<T> function)
{
return new DoubleFunctionComparator<>(function);
}
public static <T> SerializableComparator<T> toLongComparator(LongFunction<T> function)
{
return new LongFunctionComparator<>(function);
}
public static Function<String, Integer> getStringToInteger()
{
return STRING_TO_INTEGER_FUNCTION;
}
public static <T, V> Function<T, V> withDefault(Function<? super T, ? extends V> function, V defaultValue)
{
return new DefaultFunction<>(function, defaultValue);
}
public static <T, V> Function<T, V> nullSafe(Function<? super T, ? extends V> function)
{
return new NullSafeFunction<>(function, null);
}
public static <T, V> Function<T, V> nullSafe(Function<? super T, ? extends V> function, V nullValue)
{
return new NullSafeFunction<>(function, nullValue);
}
public static <V1> Function<Pair<V1, ?>, V1> firstOfPair()
{
return (Function<Pair<V1, ?>, V1>) (Function<?, ?>) FIRST_OF_PAIR_FUNCTION;
}
public static <V2> Function<Pair<?, V2>, V2> secondOfPair()
{
return (Function<Pair<?, V2>, V2>) (Function<?, ?>) SECOND_OF_PAIR_FUNCTION;
}
/**
* Swap the input pair and return the swapped pair.
*
* @return A function that gets the swapped pair {@code Iterable}
*/
public static <S, T> Function<Pair<S, T>, Pair<T, S>> swappedPair()
{
return (Function<Pair<S, T>, Pair<T, S>>) (Function<?, ?>) SWAPPED_PAIR_FUNCTION;
}
/**
* Bind the parameter passed to a Function2 into a new Function.
*
* @param function The Function2 to delegate the invocation to.
* @param parameter The parameter the use in the invocation of the delegate function.
* @return A new Function
*/
public static <T, P, R> Function<T, R> bind(Function2<? super T, ? super P, ? extends R> function, P parameter)
{
return new BindFunction2<>(function, parameter);
}
/**
* Bind the input of a Procedure to the result of a function, returning a new Procedure.
*
* @param delegate The Procedure to delegate the invocation to.
* @param function The Function that will create the input for the delegate
* @return A new Procedure
*/
public static <T1, T2> Procedure<T1> bind(
Procedure<? super T2> delegate,
Function<? super T1, T2> function)
{
return new BindProcedure<>(delegate, function);
}
/**
* Bind the input of a ObjectIntProcedure to the result of a function, returning a new ObjectIntProcedure.
*
* @param delegate The ObjectIntProcedure to delegate the invocation to.
* @param function The Function that will create the input for the delegate
* @return A new ObjectIntProcedure
*/
public static <T1, T2> ObjectIntProcedure<T1> bind(
ObjectIntProcedure<? super T2> delegate,
Function<? super T1, T2> function)
{
return new BindObjectIntProcedure<>(delegate, function);
}
/**
* Bind the input of the first argument of a Procedure2 to the result of a function, returning a new Procedure2.
*
* @param delegate The Procedure2 to delegate the invocation to.
* @param function The Function that will create the input for the delegate
* @return A new Procedure2
*/
public static <T1, T2, T3> Procedure2<T1, T3> bind(
Procedure2<? super T2, T3> delegate, Function<? super T1, T2> function)
{
return new BindProcedure2<>(delegate, function);
}
public static Function<Integer, Integer> squaredInteger()
{
return SQUARED_INTEGER;
}
public static <T, V> Function<T, V> firstNotNullValue(Function<T, V>... functions)
{
return new FirstNotNullFunction<>(functions);
}
public static <T> Function<T, String> firstNotEmptyStringValue(
Function<T, String>... functions)
{
return new FirstNotEmptyStringFunction<>(functions);
}
public static <T1, T2, I extends Iterable<T2>> Function<T1, I> firstNotEmptyCollectionValue(
Function<T1, I>... functions)
{
return new FirstNotEmptyCollectionFunction<>(functions);
}
public static <T, V> Function<T, V> ifTrue(
Predicate<? super T> predicate,
Function<? super T, ? extends V> function)
{
return new IfFunction<>(predicate, function);
}
public static <T, V> Function<T, V> ifElse(
Predicate<? super T> predicate,
Function<? super T, ? extends V> trueFunction,
Function<? super T, ? extends V> falseFunction)
{
return new IfFunction<>(predicate, trueFunction, falseFunction);
}
public static <T extends Comparable<? super T>, V> CaseFunction<T, V> caseDefault(
Function<? super T, ? extends V> defaultFunction)
{
return new CaseFunction<T, V>(defaultFunction);
}
public static <T extends Comparable<? super T>, V> CaseFunction<T, V> caseDefault(
Function<? super T, ? extends V> defaultFunction,
Predicate<? super T> predicate,
Function<? super T, ? extends V> function)
{
CaseFunction<T, V> caseFunction = Functions.<T, V>caseDefault(defaultFunction);
return caseFunction.addCase(predicate, function);
}
public static <T, V> Function<T, V> synchronizedEach(Function<T, V> function)
{
return new SynchronizedFunction<>(function);
}
public static Function<String, Class<?>> classForName()
{
return CLASS_FOR_NAME;
}
private static final class FirstNotNullFunction<T, V> implements Function<T, V>
{
private static final long serialVersionUID = 1L;
private final Function<T, V>[] functions;
private FirstNotNullFunction(Function<T, V>... functions)
{
this.functions = functions;
}
@Override
public V valueOf(T object)
{
for (Function<T, V> function : this.functions)
{
V result = function.valueOf(object);
if (result != null)
{
return result;
}
}
return null;
}
}
private static final class FirstNotEmptyStringFunction<T> implements Function<T, String>
{
private static final long serialVersionUID = 1L;
private final Function<T, String>[] functions;
private FirstNotEmptyStringFunction(Function<T, String>... functions)
{
this.functions = functions;
}
@Override
public String valueOf(T object)
{
for (Function<T, String> function : this.functions)
{
String result = function.valueOf(object);
if (StringIterate.notEmpty(result))
{
return result;
}
}
return null;
}
}
private static final class FirstNotEmptyCollectionFunction<T1, T2, I extends Iterable<T2>> implements Function<T1, I>
{
private static final long serialVersionUID = 1L;
private final Function<T1, I>[] functions;
private FirstNotEmptyCollectionFunction(Function<T1, I>[] functions)
{
this.functions = functions;
}
@Override
public I valueOf(T1 object)
{
for (Function<T1, I> function : this.functions)
{
I result = function.valueOf(object);
if (Iterate.notEmpty(result))
{
return result;
}
}
return null;
}
}
private static final class SynchronizedFunction<T, V> implements Function<T, V>
{
private static final long serialVersionUID = 1L;
private final Function<T, V> function;
private SynchronizedFunction(Function<T, V> function)
{
this.function = function;
}
@Override
public V valueOf(T each)
{
synchronized (each)
{
return this.function.valueOf(each);
}
}
}
public static <T1, T2, T3> FunctionChain<T1, T2, T3> chain(Function<T1, T2> function1, Function<? super T2, T3> function2)
{
return new FunctionChain<>(function1, function2);
}
public static <T1, T2> BooleanFunctionChain<T1, T2> chainBoolean(Function<T1, T2> function1, BooleanFunction<? super T2> function2)
{
return new BooleanFunctionChain<>(function1, function2);
}
public static <T1, T2> ByteFunctionChain<T1, T2> chainByte(Function<T1, T2> function1, ByteFunction<? super T2> function2)
{
return new ByteFunctionChain<>(function1, function2);
}
public static <T1, T2> CharFunctionChain<T1, T2> chainChar(Function<T1, T2> function1, CharFunction<? super T2> function2)
{
return new CharFunctionChain<>(function1, function2);
}
public static <T1, T2> DoubleFunctionChain<T1, T2> chainDouble(Function<T1, T2> function1, DoubleFunction<? super T2> function2)
{
return new DoubleFunctionChain<>(function1, function2);
}
public static <T1, T2> FloatFunctionChain<T1, T2> chainFloat(Function<T1, T2> function1, FloatFunction<? super T2> function2)
{
return new FloatFunctionChain<>(function1, function2);
}
public static <T1, T2> IntFunctionChain<T1, T2> chainInt(Function<T1, T2> function1, IntFunction<? super T2> function2)
{
return new IntFunctionChain<>(function1, function2);
}
public static <T1, T2> LongFunctionChain<T1, T2> chainLong(Function<T1, T2> function1, LongFunction<? super T2> function2)
{
return new LongFunctionChain<>(function1, function2);
}
public static <T1, T2> ShortFunctionChain<T1, T2> chainShort(Function<T1, T2> function1, ShortFunction<? super T2> function2)
{
return new ShortFunctionChain<>(function1, function2);
}
private static class DoublePassThruFunction implements Function<Double, Double>, DoubleFunction<Double>
{
private static final long serialVersionUID = 1L;
@Override
public double doubleValueOf(Double each)
{
return each.doubleValue();
}
@Override
public Double valueOf(Double each)
{
return each;
}
@Override
public String toString()
{
return DoublePassThruFunction.class.getSimpleName();
}
}
private static class IntegerPassThruFunction implements Function<Integer, Integer>, IntFunction<Integer>
{
private static final long serialVersionUID = 1L;
@Override
public int intValueOf(Integer each)
{
return each.intValue();
}
@Override
public Integer valueOf(Integer each)
{
return each;
}
@Override
public String toString()
{
return IntegerPassThruFunction.class.getSimpleName();
}
}
private static class LongPassThruFunction implements Function<Long, Long>, LongFunction<Long>
{
private static final long serialVersionUID = 1L;
@Override
public long longValueOf(Long each)
{
return each.longValue();
}
@Override
public Long valueOf(Long each)
{
return each;
}
@Override
public String toString()
{
return LongPassThruFunction.class.getSimpleName();
}
}
private static final class DefaultFunction<T, V> implements Function<T, V>
{
private static final long serialVersionUID = 1L;
private final Function<? super T, ? extends V> function;
private final V defaultValue;
private DefaultFunction(Function<? super T, ? extends V> newFunction, V newDefaultValue)
{
this.function = newFunction;
this.defaultValue = newDefaultValue;
}
@Override
public V valueOf(T anObject)
{
V returnValue = this.function.valueOf(anObject);
if (returnValue == null)
{
return this.defaultValue;
}
return returnValue;
}
}
private static final class NullSafeFunction<T, V> implements Function<T, V>
{
private static final long serialVersionUID = 1L;
private final Function<? super T, ? extends V> function;
private final V nullValue;
private NullSafeFunction(Function<? super T, ? extends V> function, V nullValue)
{
this.function = function;
this.nullValue = nullValue;
}
@Override
public V valueOf(T object)
{
return object == null ? this.nullValue : this.function.valueOf(object);
}
}
public static <T, V1, V2> Function<T, Pair<V1, V2>> pair(
Function<? super T, V1> function1,
Function<? super T, V2> function2)
{
return t -> Tuples.pair(function1.valueOf(t), function2.valueOf(t));
}
/**
* @return A function that gets the key out of a {@link java.util.Map.Entry}
*/
@SuppressWarnings("UnnecessaryFullyQualifiedName")
public static <K> Function<Map.Entry<K, ?>, K> getKeyFunction()
{
return (Function<Map.Entry<K, ?>, K>) MAP_KEY_FUNCTION;
}
/**
* @return A function that gets the value out of a {@link java.util.Map.Entry}
*/
@SuppressWarnings("UnnecessaryFullyQualifiedName")
public static <V> Function<Map.Entry<?, V>, V> getValueFunction()
{
return (Function<Map.Entry<?, V>, V>) MAP_VALUE_FUNCTION;
}
private static class MapKeyFunction<K> implements Function<Map.Entry<K, ?>, K>
{
private static final long serialVersionUID = 1L;
@Override
public K valueOf(Map.Entry<K, ?> entry)
{
return entry.getKey();
}
}
private static class MapValueFunction<V> implements Function<Map.Entry<?, V>, V>
{
private static final long serialVersionUID = 1L;
@Override
public V valueOf(Map.Entry<?, V> entry)
{
return entry.getValue();
}
}
/**
* @return A function that gets the size of an {@code Iterable}
*/
public static Function<Iterable<?>, Integer> getSizeOf()
{
return SIZE_FUNCTION;
}
public static class SizeFunction extends IntegerFunctionImpl<Iterable<?>>
{
private static final long serialVersionUID = 1L;
@Override
public int intValueOf(Iterable<?> iterable)
{
return Iterate.sizeOf(iterable);
}
}
public static final class FunctionChain<T1, T2, T3> implements Function<T1, T3>
{
private static final long serialVersionUID = 1L;
private final Function<T1, T2> function1;
private final Function<? super T2, T3> function2;
private FunctionChain(Function<T1, T2> function1, Function<? super T2, T3> function2)
{
this.function1 = function1;
this.function2 = function2;
}
@Override
public T3 valueOf(T1 object)
{
return this.function2.valueOf(this.function1.valueOf(object));
}
public <T4> FunctionChain<T1, T3, T4> chain(Function<? super T3, T4> function)
{
return new FunctionChain<>(this, function);
}
public BooleanFunctionChain<T1, T3> chainBoolean(BooleanFunction<? super T3> function)
{
return new BooleanFunctionChain<>(this, function);
}
public ByteFunctionChain<T1, T3> chainByte(ByteFunction<? super T3> function)
{
return new ByteFunctionChain<>(this, function);
}
public CharFunctionChain<T1, T3> chainChar(CharFunction<? super T3> function)
{
return new CharFunctionChain<>(this, function);
}
public DoubleFunctionChain<T1, T3> chainDouble(DoubleFunction<? super T3> function)
{
return new DoubleFunctionChain<>(this, function);
}
public FloatFunctionChain<T1, T3> chainFloat(FloatFunction<? super T3> function)
{
return new FloatFunctionChain<>(this, function);
}
public IntFunctionChain<T1, T3> chainInt(IntFunction<? super T3> function)
{
return new IntFunctionChain<>(this, function);
}
public LongFunctionChain<T1, T3> chainLong(LongFunction<? super T3> function)
{
return new LongFunctionChain<>(this, function);
}
public ShortFunctionChain<T1, T3> chainShort(ShortFunction<? super T3> function)
{
return new ShortFunctionChain<>(this, function);
}
}
public static final class BooleanFunctionChain<T1, T2> implements BooleanFunction<T1>
{
private static final long serialVersionUID = 1L;
private final Function<T1, T2> function1;
private final BooleanFunction<? super T2> function2;
private BooleanFunctionChain(Function<T1, T2> function1, BooleanFunction<? super T2> function2)
{
this.function1 = function1;
this.function2 = function2;
}
@Override
public boolean booleanValueOf(T1 object)
{
return this.function2.booleanValueOf(this.function1.valueOf(object));
}
}
public static final class ByteFunctionChain<T1, T2> implements ByteFunction<T1>
{
private static final long serialVersionUID = 1L;
private final Function<T1, T2> function1;
private final ByteFunction<? super T2> function2;
private ByteFunctionChain(Function<T1, T2> function1, ByteFunction<? super T2> function2)
{
this.function1 = function1;
this.function2 = function2;
}
@Override
public byte byteValueOf(T1 object)
{
return this.function2.byteValueOf(this.function1.valueOf(object));
}
}
public static final class CharFunctionChain<T1, T2> implements CharFunction<T1>
{
private static final long serialVersionUID = 1L;
private final Function<T1, T2> function1;
private final CharFunction<? super T2> function2;
private CharFunctionChain(Function<T1, T2> function1, CharFunction<? super T2> function2)
{
this.function1 = function1;
this.function2 = function2;
}
@Override
public char charValueOf(T1 object)
{
return this.function2.charValueOf(this.function1.valueOf(object));
}
}
public static final class DoubleFunctionChain<T1, T2> implements DoubleFunction<T1>
{
private static final long serialVersionUID = 1L;
private final Function<T1, T2> function1;
private final DoubleFunction<? super T2> function2;
private DoubleFunctionChain(Function<T1, T2> function1, DoubleFunction<? super T2> function2)
{
this.function1 = function1;
this.function2 = function2;
}
@Override
public double doubleValueOf(T1 object)
{
return this.function2.doubleValueOf(this.function1.valueOf(object));
}
}
public static final class FloatFunctionChain<T1, T2> implements FloatFunction<T1>
{
private static final long serialVersionUID = 1L;
private final Function<T1, T2> function1;
private final FloatFunction<? super T2> function2;
private FloatFunctionChain(Function<T1, T2> function1, FloatFunction<? super T2> function2)
{
this.function1 = function1;
this.function2 = function2;
}
@Override
public float floatValueOf(T1 object)
{
return this.function2.floatValueOf(this.function1.valueOf(object));
}