/
LBiByteFunctionBuilder.java
142 lines (120 loc) · 5.75 KB
/
LBiByteFunctionBuilder.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
/*
* This file is part of "lunisolar-magma".
*
* (C) Copyright 2014-2023 Lunisolar (http://lunisolar.eu/).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eu.lunisolar.magma.func.build.function.from;
import eu.lunisolar.magma.basics.Null;
import eu.lunisolar.magma.func.build.*;
import eu.lunisolar.magma.func.Function4U; // NOSONAR
import eu.lunisolar.magma.basics.builder.*; // NOSONAR
import javax.annotation.Nonnull; // NOSONAR
import javax.annotation.Nullable; // NOSONAR
import eu.lunisolar.magma.basics.exceptions.*; // NOSONAR
import eu.lunisolar.magma.basics.meta.*; // NOSONAR
import eu.lunisolar.magma.basics.meta.functional.*; // NOSONAR
import eu.lunisolar.magma.basics.meta.functional.type.*; // NOSONAR
import eu.lunisolar.magma.basics.meta.functional.domain.*; // NOSONAR
import java.util.function.*;
import eu.lunisolar.magma.func.action.*; // NOSONAR
import eu.lunisolar.magma.func.consumer.*; // NOSONAR
import eu.lunisolar.magma.func.consumer.primitives.*; // NOSONAR
import eu.lunisolar.magma.func.consumer.primitives.bi.*; // NOSONAR
import eu.lunisolar.magma.func.consumer.primitives.obj.*; // NOSONAR
import eu.lunisolar.magma.func.consumer.primitives.tri.*; // NOSONAR
import eu.lunisolar.magma.func.function.*; // NOSONAR
import eu.lunisolar.magma.func.function.conversion.*; // NOSONAR
import eu.lunisolar.magma.func.function.from.*; // NOSONAR
import eu.lunisolar.magma.func.function.to.*; // NOSONAR
import eu.lunisolar.magma.func.operator.binary.*; // NOSONAR
import eu.lunisolar.magma.func.operator.ternary.*; // NOSONAR
import eu.lunisolar.magma.func.operator.unary.*; // NOSONAR
import eu.lunisolar.magma.func.predicate.*; // NOSONAR
import eu.lunisolar.magma.func.supplier.*; // NOSONAR
/**
* Builder for LBiByteFunction.
*/
public final class LBiByteFunctionBuilder<R> extends PerCaseBuilderWithProduct.Base<LBiByteFunctionBuilder<R>, LBiBytePredicate, LBiByteFunction<R>, R> {
// extends PER_CASE_BUILDER<BUILDER_NAME func.B(the_case.class_args_ref), CASE_PREDICATE func.B(the_case.domain_class_argsX_ref), the_case.name_ref RRR> {
private Consumer<LBiByteFunction<R>> consumer;
private @Nullable HandlingInstructions handling;
public static final LBiByteFunction OTHERWISE_THROW = LBiByteFunction.biByteFunc((a1, a2) -> {
throw new IllegalStateException("There is no case configured for the arguments (if any).");
});
public LBiByteFunctionBuilder(@Nullable Consumer<LBiByteFunction<R>> consumer) {
super(OTHERWISE_THROW, LBiByteFunction::constant, () -> new LBiByteFunctionBuilder(null));
this.consumer = consumer;
}
/** One of ways of creating builder. In most cases (considering all _functional_ builders) it requires to provide generic parameters (in most cases redundantly) */
public LBiByteFunctionBuilder() {
this(null);
}
/** One of ways of creating builder. In most cases (considering all _functional_ builders) it requires to provide generic parameters (in most cases redundantly) */
@Nonnull
public static <R> LBiByteFunctionBuilder<R> biByteFunction() {
return new LBiByteFunctionBuilder();
}
/** One of ways of creating builder. This is possibly the least verbose way where compiler should be able to guess the generic parameters. */
@Nonnull
public static <R> LBiByteFunction<R> biByteFunctionFrom(Consumer<LBiByteFunctionBuilder<R>> buildingFunction) {
LBiByteFunctionBuilder builder = new LBiByteFunctionBuilder();
buildingFunction.accept(builder);
return builder.build();
}
/** One of ways of creating builder. This might be the only way (considering all _functional_ builders) that might be utilize to specify generic params only once. */
@Nonnull
public static <R> LBiByteFunctionBuilder<R> biByteFunction(Consumer<LBiByteFunction<R>> consumer) {
return new LBiByteFunctionBuilder(consumer);
}
/** One of ways of creating builder. In most cases (considering all _functional_ builders) it requires to provide generic parameters (in most cases redundantly) */
@Nonnull
public final LBiByteFunctionBuilder<R> withHandling(@Nonnull HandlingInstructions<Throwable, RuntimeException> handling) {
Null.nonNullArg(handling, "handling");
if (this.handling != null) {
throw new UnsupportedOperationException("Handling is already set for this builder.");
}
this.handling = handling;
return fluentCtx();
}
/** Builds the functional interface implementation and if previously provided calls the consumer. */
@Nonnull
public final LBiByteFunction<R> build() {
final LBiByteFunction<R> otherwiseFinal = this.otherwise;
LBiByteFunction<R> retval;
final Case<LBiBytePredicate, LBiByteFunction<R>>[] casesArray = cases.toArray(new Case[cases.size()]);
retval = LBiByteFunction.<R> biByteFunc((a1, a2) -> {
try {
for (Case<LBiBytePredicate, LBiByteFunction<R>> aCase : casesArray) {
if (aCase.casePredicate().test(a1, a2)) {
return aCase.caseFunction().apply(a1, a2);
}
}
return otherwiseFinal.apply(a1, a2);
} catch (Error e) { // NOSONAR
throw e;
} catch (Throwable e) { // NOSONAR
throw Handler.handleOrPropagate(e, handling);
}
});
if (consumer != null) {
consumer.accept(retval);
}
return retval;
}
public final LBiByteFunction<R> build(@Nonnull HandlingInstructions<Throwable, RuntimeException> handling) {
this.withHandling(handling);
return build();
}
}