2424import java .util .Locale ;
2525import java .util .Optional ;
2626import java .util .function .Predicate ;
27-
28- import org .jooby .util .ExSupplier ;
29- import org .jooby .util .Switch ;
27+ import java .util .function .Supplier ;
3028
3129import com .google .common .base .CharMatcher ;
3230import com .google .common .base .Splitter ;
3331import com .google .common .base .Strings ;
3432import com .typesafe .config .Config ;
3533
34+ import javaslang .control .Match ;
35+ import javaslang .control .Match .MatchValue ;
36+
3637/**
3738 * Allows to optimize, customize or apply defaults values for services.
3839 *
@@ -231,15 +232,35 @@ default String resolve(final String text, final Config source,
231232 * @param fn A callback function.
232233 * @param <T> A resulting type.
233234 * @return A resulting object.
234- * @throws Exception If something fails.
235235 */
236- default <T > Optional <T > ifMode (final String name , final ExSupplier <T > fn )
237- throws Exception {
238- return when (name , fn ).value ();
236+ default <T > Optional <T > ifMode (final String name , final Supplier <T > fn ) {
237+ if (name ().equals (name )) {
238+ return Optional .of (fn .get ());
239+ }
240+ return Optional .empty ();
241+ }
242+
243+ /**
244+ * Produces a {@link Match} of the current {@link Env}.
245+ *
246+ * <pre>
247+ * String accessKey = env.match()"dev", () {@literal ->} "1234")
248+ * .when("stage", () {@literal ->} "4321")
249+ * .when("prod", () {@literal ->} "abc")
250+ * .get();
251+ * </pre>
252+ *
253+ * @param name A name to test for.
254+ * @param fn A callback function.
255+ * @param <T> A resulting type.
256+ * @return A new matcher.
257+ */
258+ default MatchValue .Of <String > match () {
259+ return Match .of (name ());
239260 }
240261
241262 /**
242- * Produces a {@link Switch } of the current {@link Env}.
263+ * Produces a {@link Match } of the current {@link Env}.
243264 *
244265 * <pre>
245266 * String accessKey = env.when("dev", () {@literal ->} "1234")
@@ -251,14 +272,14 @@ default <T> Optional<T> ifMode(final String name, final ExSupplier<T> fn)
251272 * @param name A name to test for.
252273 * @param fn A callback function.
253274 * @param <T> A resulting type.
254- * @return A new switch .
275+ * @return A new matcher .
255276 */
256- default <T > Switch <String , T > when (final String name , final ExSupplier <T > fn ) {
257- return Switch .< T > newSwitch (name ()). when ( name , fn );
277+ default <T > MatchValue . Then <String , T > when (final String name , final Supplier <T > fn ) {
278+ return match (). whenIs (name ). then ( fn );
258279 }
259280
260281 /**
261- * Produces a {@link Switch } of the current {@link Env}.
282+ * Produces a {@link Match } of the current {@link Env}.
262283 *
263284 * <pre>
264285 * String accessKey = env.when("dev", "1234")
@@ -270,14 +291,14 @@ default <T> Switch<String, T> when(final String name, final ExSupplier<T> fn) {
270291 * @param name A name to test for.
271292 * @param result A constant value to return.
272293 * @param <T> A resulting type.
273- * @return A new switch .
294+ * @return A new matcher .
274295 */
275- default <T > Switch <String , T > when (final String name , final T result ) {
276- return Switch .< T > newSwitch (name ()). when ( name , result );
296+ default <T > MatchValue . Then <String , T > when (final String name , final T result ) {
297+ return match (). whenIs (name ). then ( result );
277298 }
278299
279300 /**
280- * Produces a {@link Switch } of the current {@link Env}.
301+ * Produces a {@link Match } of the current {@link Env}.
281302 *
282303 * <pre>
283304 * String accessKey = env.when("dev", () {@literal ->} "1234")
@@ -289,10 +310,10 @@ default <T> Switch<String, T> when(final String name, final T result) {
289310 * @param predicate A predicate to use.
290311 * @param result A constant value to return.
291312 * @param <T> A resulting type.
292- * @return A new switch .
313+ * @return A new matcher .
293314 */
294- default <T > Switch <String , T > when (final Predicate <String > predicate ,
295- final T result ) {
296- return Switch .<T > newSwitch (name ()).when (predicate , result );
315+ default <T > MatchValue .Then <String , T > when (final Predicate <String > predicate , final T result ) {
316+ return match ().when (e -> predicate .test (e .toString ())).then (result );
297317 }
318+
298319}
0 commit comments