@@ -111,6 +111,101 @@ static <L, R> Either<L, R> narrow(Either<? extends L, ? extends R> either) {
111
111
return (Either <L , R >) either ;
112
112
}
113
113
114
+ /**
115
+ * Decides which {@code Either<L, R>} to return, depending on the test value -
116
+ * if it's true, the result will be a {@link Either.Right},
117
+ * if it's false - the result will be a {@link Either.Left}
118
+ *
119
+ * @param test A {@code Boolean} value to evaluate
120
+ * @param right A {@code Supplier<? extends R>} supplier of right value, called if test is true
121
+ * @param left A {@code Supplier<? extends L>} supplier of left value, called if test is false
122
+ * @param <L> Type of left value
123
+ * @param <R> Type of right value
124
+ *
125
+ * @return {@code Either<L, R>} with right or left value, depending on the test condition evaluation
126
+ *
127
+ * @throws NullPointerException if any of the arguments is null
128
+ * @author Adam Kopeć
129
+ */
130
+ static <L , R > Either <L , R > cond (Boolean test , Supplier <? extends R > right , Supplier <? extends L > left ) {
131
+ Objects .requireNonNull (test , "test is null" );
132
+ Objects .requireNonNull (right , "right is null" );
133
+ Objects .requireNonNull (left , "left is null" );
134
+
135
+ return test ? right (right .get ()) : left (left .get ());
136
+ }
137
+
138
+ /**
139
+ * Decides which {@code Either<L, R>} to return, depending on the test value -
140
+ * if it's true, the result will be a {@link Either.Right},
141
+ * if it's false - the result will be a {@link Either.Left}
142
+ *
143
+ * @param test A {@code Boolean} value to evaluate
144
+ * @param right A n{@code R} right value, returned if test is true
145
+ * @param left A {@code L} left value, returned if test is false
146
+ * @param <L> Type of left value
147
+ * @param <R> Type of right value
148
+ *
149
+ * @return {@code Either<L, R>} with right or left value, depending on the test condition evaluation
150
+ *
151
+ * @throws NullPointerException if any of the arguments is null
152
+ * @author Adam Kopeć
153
+ */
154
+ static <L , R > Either <L , R > cond (Boolean test , R right , L left ) {
155
+ Objects .requireNonNull (test , "test is null" );
156
+ Objects .requireNonNull (right , "right is null" );
157
+ Objects .requireNonNull (left , "left is null" );
158
+
159
+ return cond (test , () -> right , () -> left );
160
+ }
161
+
162
+ /**
163
+ * Decides which {@code Either<L, R>} to return, depending on the test value -
164
+ * if it's true, the result will be a {@link Either.Right},
165
+ * if it's false - the result will be a {@link Either.Left}
166
+ *
167
+ * @param test A {@code Boolean} value to evaluate
168
+ * @param right A {@code Supplier<? extends R>} supplier of right value, called if test is true
169
+ * @param left A {@code L} left value, returned if test is false
170
+ * @param <L> Type of left value
171
+ * @param <R> Type of right value
172
+ *
173
+ * @return {@code Either<L, R>} with right or left value, depending on the test condition evaluation
174
+ *
175
+ * @throws NullPointerException if any of the arguments is null
176
+ * @author Adam Kopeć
177
+ */
178
+ static <L , R > Either <L , R > cond (Boolean test , Supplier <? extends R > right , L left ) {
179
+ Objects .requireNonNull (test , "test is null" );
180
+ Objects .requireNonNull (right , "right is null" );
181
+ Objects .requireNonNull (left , "left is null" );
182
+
183
+ return cond (test , right , () -> left );
184
+ }
185
+
186
+ /**
187
+ * Decides which {@code Either<L, R>} to return, depending on the test value -
188
+ * if it's true, the result will be a {@link Either.Right},
189
+ * if it's false - the result will be a {@link Either.Left}
190
+ *
191
+ * @param test A {@code Boolean} value to evaluate
192
+ * @param right A {@code R} right value, returned if test is true
193
+ * @param left A {@code Supplier<? extends L>} supplier of left value, called if test is false
194
+ * @param <L> Type of left value
195
+ * @param <R> Type of right value
196
+ *
197
+ * @return {@code Either<L, R>} with right or left value, depending on the test condition evaluation
198
+ *
199
+ * @throws NullPointerException if any of the arguments is null
200
+ * @author Adam Kopeć
201
+ */
202
+ static <L , R > Either <L , R > cond (Boolean test , R right , Supplier <? extends L > left ) {
203
+ Objects .requireNonNull (test , "test is null" );
204
+ Objects .requireNonNull (right , "right is null" );
205
+ Objects .requireNonNull (left , "left is null" );
206
+
207
+ return cond (test , () -> right , left );
208
+ }
114
209
/**
115
210
* Returns the left value.
116
211
*
0 commit comments