TAMAYA-274 Added Java 8 supplier based methods.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/48f3f7b3 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/48f3f7b3 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/48f3f7b3 Branch: refs/heads/master Commit: 48f3f7b36b0f2a0d6e25484a02db0c700d807cb6 Parents: 05b1faa Author: Anatole Tresch <[email protected]> Authored: Sun Nov 12 21:24:28 2017 +0100 Committer: Anatole Tresch <[email protected]> Committed: Tue Nov 14 10:26:04 2017 +0100 ---------------------------------------------------------------------- .../apache/tamaya/inject/api/DynamicValue.java | 60 +++++++------- .../tamaya/inject/spi/BaseDynamicValue.java | 84 ++++++++++---------- .../inject/internal/DefaultDynamicValue.java | 19 +++++ 3 files changed, 91 insertions(+), 72 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/48f3f7b3/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/DynamicValue.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/DynamicValue.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/DynamicValue.java index d56e29c..54ab10a 100644 --- a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/DynamicValue.java +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/api/DynamicValue.java @@ -19,6 +19,7 @@ package org.apache.tamaya.inject.api; import java.beans.PropertyChangeListener; +import java.util.function.Supplier; /** @@ -128,35 +129,34 @@ public interface DynamicValue<T> { */ T orElse(T other); - // TODO enable with Java 8 -// /** -// * Return the value if present, otherwise invoke {@code other} and return -// * the result of that invocation. -// * -// * @param other a {@code ConfiguredItemSupplier} whose result is returned if no value -// * is present -// * @return the value if present otherwise the result of {@code other.get()} -// * @throws NullPointerException if value is not present and {@code other} is -// * null -// */ -// T orElseGet(ConfiguredItemSupplier<? extends T> other); -// -// /** -// * Return the contained value, if present, otherwise throw an exception -// * to be created by the provided supplier. -// * -// * NOTE A method reference to the exception constructor with an empty -// * argument list can be used as the supplier. For example, -// * {@code IllegalStateException::new} -// * -// * @param <X> Type of the exception to be thrown -// * @param exceptionSupplier The supplier which will return the exception to -// * be thrown -// * @return the present value -// * @throws X if there is no value present -// * @throws NullPointerException if no value is present and -// * {@code exceptionSupplier} is null -// */ -// <X extends Throwable> T orElseThrow(ConfiguredItemSupplier<? extends X> exceptionSupplier) throws X; + /** + * Return the value if present, otherwise invoke {@code other} and return + * the result of that invocation. + * + * @param other a {@code ConfiguredItemSupplier} whose result is returned if no value + * is present + * @return the value if present otherwise the result of {@code other.get()} + * @throws NullPointerException if value is not present and {@code other} is + * null + */ + T orElseGet(Supplier<? extends T> other); + + /** + * Return the contained value, if present, otherwise throw an exception + * to be created by the provided supplier. + * + * NOTE A method reference to the exception constructor with an empty + * argument list can be used as the supplier. For example, + * {@code IllegalStateException::new} + * + * @param <X> Type of the exception to be thrown + * @param exceptionSupplier The supplier which will return the exception to + * be thrown + * @return the present value + * @throws X if there is no value present + * @throws NullPointerException if no value is present and + * {@code exceptionSupplier} is null + */ + <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/48f3f7b3/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/BaseDynamicValue.java ---------------------------------------------------------------------- diff --git a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/BaseDynamicValue.java b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/BaseDynamicValue.java index a23a85c..1d0e75e 100644 --- a/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/BaseDynamicValue.java +++ b/modules/injection/injection-api/src/main/java/org/apache/tamaya/inject/spi/BaseDynamicValue.java @@ -21,6 +21,7 @@ package org.apache.tamaya.inject.spi; import org.apache.tamaya.inject.api.DynamicValue; import java.io.Serializable; +import java.util.function.Supplier; /** * Basic abstract implementation skeleton for a {@link DynamicValue}. This can be used to support values that may @@ -80,47 +81,46 @@ public abstract class BaseDynamicValue<T> implements DynamicValue<T>, Serializab return value; } - // TODO: Enable with Java 8 support. -// /** -// * Return the value if present, otherwise invoke {@code other} and return -// * the result of that invocation. -// * -// * @param other a {@code ConfiguredItemSupplier} whose result is returned if no value -// * is present -// * @return the value if present otherwise the result of {@code other.get()} -// * @throws NullPointerException if value is not present and {@code other} is -// * null -// */ -// public T orElseGet(Supplier<? extends T> other) { -// T value = get(); -// if (value == null) { -// return other.get(); -// } -// return value; -// } -// -// /** -// * Return the contained value, if present, otherwise throw an exception -// * to be created by the provided supplier. -// * <p> -// * NOTE A method reference to the exception constructor with an empty -// * argument list can be used as the supplier. For example, -// * {@code IllegalStateException::new} -// * -// * @param <X> Type of the exception to be thrown -// * @param exceptionSupplier The supplier which will return the exception to -// * be thrown -// * @return the present value -// * @throws X if there is no value present -// * @throws NullPointerException if no value is present and -// * {@code exceptionSupplier} is null -// */ -// public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X { -// T value = get(); -// if (value == null) { -// throw exceptionSupplier.get(); -// } -// return value; -// } + /** + * Return the value if present, otherwise invoke {@code other} and return + * the result of that invocation. + * + * @param other a {@code ConfiguredItemSupplier} whose result is returned if no value + * is present + * @return the value if present otherwise the result of {@code other.get()} + * @throws NullPointerException if value is not present and {@code other} is + * null + */ + public T orElseGet(Supplier<? extends T> other) { + T value = get(); + if (value == null) { + return other.get(); + } + return value; + } + + /** + * Return the contained value, if present, otherwise throw an exception + * to be created by the provided supplier. + * <p> + * NOTE A method reference to the exception constructor with an empty + * argument list can be used as the supplier. For example, + * {@code IllegalStateException::new} + * + * @param <X> Type of the exception to be thrown + * @param exceptionSupplier The supplier which will return the exception to + * be thrown + * @return the present value + * @throws X if there is no value present + * @throws NullPointerException if no value is present and + * {@code exceptionSupplier} is null + */ + public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X { + T value = get(); + if (value == null) { + throw exceptionSupplier.get(); + } + return value; + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/48f3f7b3/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java ---------------------------------------------------------------------- diff --git a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java index a6b8c06..621399d 100644 --- a/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java +++ b/modules/injection/standalone/src/main/java/org/apache/tamaya/inject/internal/DefaultDynamicValue.java @@ -45,6 +45,7 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Objects; +import java.util.function.Supplier; import java.util.logging.Logger; /** @@ -409,6 +410,24 @@ final class DefaultDynamicValue<T> extends BaseDynamicValue<T> { return nv; } + @Override + public T orElseGet(Supplier<? extends T> other) { + T t = evaluateValue(); + if(t==null){ + return other.get(); + } + return t; + } + + @Override + public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X { + T t = evaluateValue(); + if(t==null){ + throw exceptionSupplier.get(); + } + return t; + } + /** * Serialization implementation that strips away the non serializable Optional part.
