You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2020/09/01 11:31:09 UTC
[camel] 02/03: Add javadoc and remove lamda usage in utility methods
This is an automated email from the ASF dual-hosted git repository.
lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 67f7d6799933f1744e7bcfd835893492094590cf
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Tue Sep 1 11:56:30 2020 +0200
Add javadoc and remove lamda usage in utility methods
---
.../org/apache/camel/util/function/Suppliers.java | 97 +++++++++++++++++-----
.../camel/util/function/ThrowingBiConsumer.java | 16 ++++
.../camel/util/function/ThrowingBiFunction.java | 18 ++++
.../camel/util/function/ThrowingConsumer.java | 14 ++++
.../camel/util/function/ThrowingFunction.java | 16 ++++
.../apache/camel/util/function/ThrowingHelper.java | 68 ++++++++++-----
.../camel/util/function/ThrowingRunnable.java | 12 +++
.../camel/util/function/ThrowingSupplier.java | 15 ++++
.../util/function/ThrowingToLongFunction.java | 15 ++++
.../camel/util/function/ThrowingTriConsumer.java | 17 ++++
.../apache/camel/util/function/TriConsumer.java | 15 ++++
11 files changed, 262 insertions(+), 41 deletions(-)
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/function/Suppliers.java b/core/camel-util/src/main/java/org/apache/camel/util/function/Suppliers.java
index 61c18ab..1c2610d 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/function/Suppliers.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/function/Suppliers.java
@@ -27,44 +27,91 @@ public final class Suppliers {
private Suppliers() {
}
+ /**
+ * Returns a supplier which caches the result of the first call to {@link Supplier#get()}and returns that value on
+ * subsequent calls.
+ *
+ * @param supplier the delegate {@link Supplier}.
+ * @param <T> the type of results supplied by this supplier.
+ * @return the result fo the first call to the delegate's {@link Supplier#get()} method.
+ */
public static <T> Supplier<T> memorize(Supplier<T> supplier) {
final AtomicReference<T> valueHolder = new AtomicReference<>();
- return () -> {
- T supplied = valueHolder.get();
- if (supplied == null) {
- synchronized (valueHolder) {
- supplied = valueHolder.get();
- if (supplied == null) {
- supplied = Objects.requireNonNull(supplier.get(), "Supplier should not return null");
- valueHolder.lazySet(supplied);
+ return new Supplier<T>() {
+ @Override
+ public T get() {
+ T supplied = valueHolder.get();
+ if (supplied == null) {
+ synchronized (valueHolder) {
+ supplied = valueHolder.get();
+ if (supplied == null) {
+ supplied = Objects.requireNonNull(supplier.get(), "Supplier should not return null");
+ valueHolder.lazySet(supplied);
+ }
}
}
+ return supplied;
}
- return supplied;
};
}
+ /**
+ * Returns a supplier which caches the result of the first call to {@link Supplier#get()} and returns that value on
+ * subsequent calls.
+ *
+ * @param supplier the delegate {@link Supplier}.
+ * @param consumer a consumer for any exception thrown by the {@link ThrowingSupplier#get()}.
+ * @param <T> the type of results supplied by this supplier.
+ * @return the result fo the first call to the delegate's {@link Supplier#get()} method.
+ */
public static <T> Supplier<T> memorize(ThrowingSupplier<T, ? extends Exception> supplier, Consumer<Exception> consumer) {
final AtomicReference<T> valueHolder = new AtomicReference<>();
- return () -> {
- T supplied = valueHolder.get();
- if (supplied == null) {
- synchronized (valueHolder) {
- supplied = valueHolder.get();
- if (supplied == null) {
- try {
- supplied = Objects.requireNonNull(supplier.get(), "Supplier should not return null");
- valueHolder.lazySet(supplied);
- } catch (Exception e) {
- consumer.accept(e);
+ return new Supplier<T>() {
+ @Override
+ public T get() {
+ T supplied = valueHolder.get();
+ if (supplied == null) {
+ synchronized (valueHolder) {
+ supplied = valueHolder.get();
+ if (supplied == null) {
+ try {
+ supplied = Objects.requireNonNull(supplier.get(), "Supplier should not return null");
+ valueHolder.lazySet(supplied);
+ } catch (Exception e) {
+ consumer.accept(e);
+ }
}
}
}
+ return supplied;
+ }
+ };
+ }
+
+ /**
+ * Returns a supplier that return a constant value.
+ *
+ * @param value the constant value to return.
+ * @param <T> the type of results supplied by this supplier.
+ * @return the supplied {@code value}.
+ */
+ public static <T> Supplier<T> constant(T value) {
+ return new Supplier<T>() {
+ @Override
+ public T get() {
+ return value;
}
- return supplied;
};
}
+ /**
+ * Returns the first non null value provide by the given suppliers.
+ *
+ * @param suppliers a list of supplier.
+ * @param <T> the type of results supplied by this supplier.
+ * @return the optional computed value.
+ */
+ @SafeVarargs
public static <T> Optional<T> firstNotNull(ThrowingSupplier<T, Exception>... suppliers) throws Exception {
T answer = null;
@@ -78,6 +125,14 @@ public final class Suppliers {
return Optional.ofNullable(answer);
}
+ /**
+ * Returns the first value provide by the given suppliers that matches the given predicate.
+ *
+ * @param predicate the predicate used to evaluate the computed values.
+ * @param suppliers a list fo supplier.
+ * @param <T> the type of results supplied by this supplier.
+ * @return the optional matching value.
+ */
public static <T> Optional<T> firstMatching(Predicate<T> predicate, ThrowingSupplier<T, Exception>... suppliers)
throws Exception {
T answer = null;
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingBiConsumer.java b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingBiConsumer.java
index aca8938..5a956d5 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingBiConsumer.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingBiConsumer.java
@@ -16,7 +16,23 @@
*/
package org.apache.camel.util.function;
+/**
+ * Represents an operation that accepts two input arguments and returns no result and may thrown an exception.
+ *
+ * @param <I1> the type of the first argument to the operation
+ * @param <I2> the type of the second argument to the operation
+ * @param <T> the type of the exception the accept method may throw
+ *
+ * @see java.util.function.BiConsumer
+ */
@FunctionalInterface
public interface ThrowingBiConsumer<I1, I2, T extends Throwable> {
+ /**
+ * Performs this operation on the given arguments, potentially throwing an exception.
+ *
+ * @param i1 the first function argument
+ * @param i2 the first function argument
+ * @throws T the exception that may be thrown
+ */
void accept(I1 i1, I2 i2) throws T;
}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingBiFunction.java b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingBiFunction.java
index 937f1e1..e52e711 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingBiFunction.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingBiFunction.java
@@ -16,7 +16,25 @@
*/
package org.apache.camel.util.function;
+/**
+ * Represents a function that accepts two arguments, produces a result and may thrown an exception.
+ *
+ * @param <I1> the type of the first argument to the operation
+ * @param <I2> the type of the second argument to the operation
+ * @param <R> the type of the result of the function
+ * @param <T> the type of the exception the accept method may throw
+ *
+ * @see java.util.function.BiFunction
+ */
@FunctionalInterface
public interface ThrowingBiFunction<I1, I2, R, T extends Throwable> {
+ /**
+ * Applies this function to the given arguments, potentially throwing an exception.
+ *
+ * @param in1 the first function argument
+ * @param in2 the second function argument
+ * @return the function result
+ * @throws T the exception that may be thrown
+ */
R apply(I1 in1, I2 in2) throws T;
}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingConsumer.java b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingConsumer.java
index c13434d..a7f36cd 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingConsumer.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingConsumer.java
@@ -16,7 +16,21 @@
*/
package org.apache.camel.util.function;
+/**
+ * Represents an operation that accepts a single input argument and may thrown an exception.
+ *
+ * @param <I> the type of the input to the operation
+ * @param <T> the type of the exception the accept method may throw
+ *
+ * @see java.util.function.Consumer
+ */
@FunctionalInterface
public interface ThrowingConsumer<I, T extends Throwable> {
+ /**
+ * Performs this operation on the given argument, potentially throwing an exception.
+ *
+ * @param in the function argument
+ * @throws T the exception that may be thrown
+ */
void accept(I in) throws T;
}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingFunction.java b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingFunction.java
index 53bb9d4..eaf005a 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingFunction.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingFunction.java
@@ -16,7 +16,23 @@
*/
package org.apache.camel.util.function;
+/**
+ * Represents a function that accepts a single arguments, produces a result and may thrown an exception.
+ *
+ * @param <I> the type of the input of the function
+ * @param <R> the type of the result of the function
+ * @param <T> the type of the exception the accept method may throw
+ *
+ * @see java.util.function.Function
+ */
@FunctionalInterface
public interface ThrowingFunction<I, R, T extends Throwable> {
+ /**
+ * Applies this function to the given argument, potentially throwing an exception.
+ *
+ * @param in the function argument
+ * @return the function result
+ * @throws T the exception that may be thrown
+ */
R apply(I in) throws T;
}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingHelper.java
index 10236ca..4ebb39e 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingHelper.java
@@ -28,43 +28,71 @@ public final class ThrowingHelper {
private ThrowingHelper() {
}
+ /**
+ * Wrap a {@link ThrowingSupplier} to a standard {@link Suppliers} by throwing a {@link RuntimeException} in case of
+ * an exception is thrown by the delegated supplier.
+ */
public static <V, T extends Throwable> Supplier<V> wrapAsSupplier(ThrowingSupplier<V, T> supplier) {
- return () -> {
- try {
- return supplier.get();
- } catch (Throwable t) {
- throw new RuntimeException(t);
+ return new Supplier<V>() {
+ @Override
+ public V get() {
+ try {
+ return supplier.get();
+ } catch (Throwable t) {
+ throw new RuntimeException(t);
+ }
}
};
}
+ /**
+ * Wrap a {@link ThrowingConsumer} to a standard {@link Consumer} by throwing a {@link RuntimeException} in case of
+ * an exception is thrown by the delegated consumer.
+ */
public static <I, T extends Throwable> Consumer<I> wrapAsConsumer(ThrowingConsumer<I, T> consumer) {
- return in -> {
- try {
- consumer.accept(in);
- } catch (Throwable t) {
- throw new RuntimeException(t);
+ return new Consumer<I>() {
+ @Override
+ public void accept(I in) {
+ try {
+ consumer.accept(in);
+ } catch (Throwable t) {
+ throw new RuntimeException(t);
+ }
}
};
}
+ /**
+ * Wrap a {@link ThrowingBiConsumer} to a standard {@link BiConsumer} by throwing a {@link RuntimeException} in case
+ * of an exception is thrown by the delegated consumer.
+ */
public static <I1, I2, T extends Throwable> BiConsumer<I1, I2> wrapAsBiConsumer(ThrowingBiConsumer<I1, I2, T> consumer) {
- return (i1, i2) -> {
- try {
- consumer.accept(i1, i2);
- } catch (Throwable t) {
- throw new RuntimeException(t);
+ return new BiConsumer<I1, I2>() {
+ @Override
+ public void accept(I1 i1, I2 i2) {
+ try {
+ consumer.accept(i1, i2);
+ } catch (Throwable t) {
+ throw new RuntimeException(t);
+ }
}
};
}
+ /**
+ * Wrap a {@link ThrowingFunction} to a standard {@link Function} by throwing a {@link RuntimeException} in case of
+ * an exception is thrown by the delegated function.
+ */
public static <I, R, T extends Throwable> Function<I, R> wrapAsFunction(ThrowingFunction<I, R, T> function) {
- return in -> {
- try {
- return function.apply(in);
+ return new Function<I, R>() {
+ @Override
+ public R apply(I in) {
+ try {
+ return function.apply(in);
- } catch (Throwable t) {
- throw new RuntimeException(t);
+ } catch (Throwable t) {
+ throw new RuntimeException(t);
+ }
}
};
}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingRunnable.java b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingRunnable.java
index a709fee..43f0d58 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingRunnable.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingRunnable.java
@@ -16,7 +16,19 @@
*/
package org.apache.camel.util.function;
+/**
+ * Represents a {@link Runnable} like interface that may thrown an exception.
+ *
+ * @param <T> the type of the exception the accept method may throw
+ *
+ * @see Runnable
+ */
@FunctionalInterface
public interface ThrowingRunnable<T extends Throwable> {
+ /**
+ * Execute an action, potentially throwing an exception.
+ *
+ * @throws T the exception that may be thrown
+ */
void run() throws T;
}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingSupplier.java b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingSupplier.java
index 357b580..2fe54dc 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingSupplier.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingSupplier.java
@@ -16,7 +16,22 @@
*/
package org.apache.camel.util.function;
+import java.util.function.Supplier;
+
+/**
+ * Represents a supplier of results that may thrown an exception.
+ *
+ * @param <T> the type of the exception the accept method may throw.
+ *
+ * @see Supplier
+ */
@FunctionalInterface
public interface ThrowingSupplier<V, T extends Throwable> {
+ /**
+ * Get a result, potentially throwing an exception.
+ *
+ * @return the result
+ * @throws T the exception that may be thrown
+ */
V get() throws T;
}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingToLongFunction.java b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingToLongFunction.java
index 6cb9fa0..71f8312 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingToLongFunction.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingToLongFunction.java
@@ -16,7 +16,22 @@
*/
package org.apache.camel.util.function;
+/**
+ * Represents a function that produces a long-valued result and may thrown an exception.
+ *
+ * @param <I> the type of the input of the function
+ * @param <T> the type of the exception the accept method may throw
+ *
+ * @see java.util.function.ToLongFunction
+ */
@FunctionalInterface
public interface ThrowingToLongFunction<I, T extends Throwable> {
+ /**
+ * Applies this function to the given argument, potentially throwing an exception.
+ *
+ * @param in the function argument
+ * @return the function result
+ * @throws T the exception that may be thrown
+ */
long apply(I in) throws T;
}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingTriConsumer.java b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingTriConsumer.java
index e804ac7..31e3538 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingTriConsumer.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/function/ThrowingTriConsumer.java
@@ -16,7 +16,24 @@
*/
package org.apache.camel.util.function;
+/**
+ * Represents an operation that accepts three input arguments and returns no result and may thrown an exception.
+ *
+ * @param <I1> the type of the first argument to the operation
+ * @param <I2> the type of the second argument to the operation
+ * @param <I3> the type of the third argument to the operation
+ * @param <T> the type of the exception the accept method may throw
+ */
@FunctionalInterface
public interface ThrowingTriConsumer<I1, I2, I3, T extends Throwable> {
+ /**
+ * Applies this function to the given arguments, potentially throwing an exception.
+ *
+ * @param i1 the first argument
+ * @param i2 the second argument
+ * @param i3 the third argument
+ * @return the function result
+ * @throws T the exception that may be thrown
+ */
void accept(I1 i1, I2 i2, I3 i3) throws T;
}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/function/TriConsumer.java b/core/camel-util/src/main/java/org/apache/camel/util/function/TriConsumer.java
index 0028ae4..deb9032 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/function/TriConsumer.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/function/TriConsumer.java
@@ -16,7 +16,22 @@
*/
package org.apache.camel.util.function;
+/**
+ * Represents an operation that accepts three input arguments and returns no result.
+ *
+ * @param <I1> the type of the first argument to the operation
+ * @param <I2> the type of the second argument to the operation
+ * @param <I3> the type of the third argument to the operation
+ */
@FunctionalInterface
public interface TriConsumer<I1, I2, I3> {
+ /**
+ * Applies this function to the given arguments..
+ *
+ * @param i1 the first argument
+ * @param i2 the second argument
+ * @param i3 the third argument
+ * @return the function result
+ */
void accept(I1 i1, I2 i2, I3 i3);
}