From 827beebe1b7ef4efbaadd74f704ce816dddc879d Mon Sep 17 00:00:00 2001 From: Vlad Kostyukov Date: Sun, 21 May 2023 23:17:24 -0700 Subject: [PATCH] Introduce ContextAwareRunnable|Callable --- .../common/ContextAwareBiConsumer.java | 50 ++++++++++++++ .../common/ContextAwareBiFunction.java | 50 ++++++++++++++ .../armeria/common/ContextAwareCallable.java | 50 ++++++++++++++ .../armeria/common/ContextAwareConsumer.java | 50 ++++++++++++++ .../armeria/common/ContextAwareFunction.java | 50 ++++++++++++++ .../armeria/common/ContextAwareRunnable.java | 48 ++++++++++++++ .../common/DefaultContextAwareBiConsumer.java | 50 ++++++++++++++ .../common/DefaultContextAwareBiFunction.java | 51 ++++++++++++++ .../common/DefaultContextAwareCallable.java | 50 ++++++++++++++ .../common/DefaultContextAwareConsumer.java | 50 ++++++++++++++ .../common/DefaultContextAwareFunction.java | 51 ++++++++++++++ .../common/DefaultContextAwareRunnable.java | 48 ++++++++++++++ .../armeria/common/RequestContext.java | 66 +++++-------------- 13 files changed, 616 insertions(+), 48 deletions(-) create mode 100644 core/src/main/java/com/linecorp/armeria/common/ContextAwareBiConsumer.java create mode 100644 core/src/main/java/com/linecorp/armeria/common/ContextAwareBiFunction.java create mode 100644 core/src/main/java/com/linecorp/armeria/common/ContextAwareCallable.java create mode 100644 core/src/main/java/com/linecorp/armeria/common/ContextAwareConsumer.java create mode 100644 core/src/main/java/com/linecorp/armeria/common/ContextAwareFunction.java create mode 100644 core/src/main/java/com/linecorp/armeria/common/ContextAwareRunnable.java create mode 100644 core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareBiConsumer.java create mode 100644 core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareBiFunction.java create mode 100644 core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareCallable.java create mode 100644 core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareConsumer.java create mode 100644 core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareFunction.java create mode 100644 core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareRunnable.java diff --git a/core/src/main/java/com/linecorp/armeria/common/ContextAwareBiConsumer.java b/core/src/main/java/com/linecorp/armeria/common/ContextAwareBiConsumer.java new file mode 100644 index 00000000000..b2d514f10bb --- /dev/null +++ b/core/src/main/java/com/linecorp/armeria/common/ContextAwareBiConsumer.java @@ -0,0 +1,50 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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 com.linecorp.armeria.common; + +import java.util.function.BiConsumer; + +import com.linecorp.armeria.common.annotation.UnstableApi; + +/** + * A delegating {@link BiConsumer} that makes sure an underlying BiConsumer is + * executed within the {@link RequestContext}. + */ +@UnstableApi +public interface ContextAwareBiConsumer extends BiConsumer, ContextHolder { + + /** + * Returns a new {@link ContextAwareBiConsumer} that sets the specified {@link RequestContext} + * before executing an underlying {@link BiConsumer}. + */ + static ContextAwareBiConsumer of(RequestContext context, BiConsumer action) { + return new DefaultContextAwareBiConsumer(context, action); + } + + /** + * Returns the {@link RequestContext} that was specified when creating + * this {@link ContextAwareBiConsumer}. + */ + @Override + RequestContext context(); + + /** + * Returns the {@link BiConsumer} that's executed without setting + * the {@link RequestContext}. + */ + BiConsumer withoutContext(); +} diff --git a/core/src/main/java/com/linecorp/armeria/common/ContextAwareBiFunction.java b/core/src/main/java/com/linecorp/armeria/common/ContextAwareBiFunction.java new file mode 100644 index 00000000000..4061b0c5bc7 --- /dev/null +++ b/core/src/main/java/com/linecorp/armeria/common/ContextAwareBiFunction.java @@ -0,0 +1,50 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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 com.linecorp.armeria.common; + +import java.util.function.BiFunction; + +import com.linecorp.armeria.common.annotation.UnstableApi; + +/** + * A delegating {@link BiFunction} that makes sure an underlying BiFunction is + * executed within the {@link RequestContext}. + */ +@UnstableApi +public interface ContextAwareBiFunction extends BiFunction, ContextHolder { + + /** + * Returns a new {@link ContextAwareBiFunction} that sets the specified {@link RequestContext} + * before executing an underlying {@link BiFunction}. + */ + static ContextAwareBiFunction of(RequestContext context, BiFunction function) { + return new DefaultContextAwareBiFunction(context, function); + } + + /** + * Returns the {@link RequestContext} that was specified when creating + * this {@link ContextAwareBiFunction}. + */ + @Override + RequestContext context(); + + /** + * Returns the {@link BiFunction} that's executed without setting + * the {@link RequestContext}. + */ + BiFunction withoutContext(); +} diff --git a/core/src/main/java/com/linecorp/armeria/common/ContextAwareCallable.java b/core/src/main/java/com/linecorp/armeria/common/ContextAwareCallable.java new file mode 100644 index 00000000000..a505269e3dd --- /dev/null +++ b/core/src/main/java/com/linecorp/armeria/common/ContextAwareCallable.java @@ -0,0 +1,50 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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 com.linecorp.armeria.common; + +import java.util.concurrent.Callable; + +import com.linecorp.armeria.common.annotation.UnstableApi; + +/** + * A delegating {@link Callable} that makes sure an underlying Callable is + * executed within the {@link RequestContext}. + */ +@UnstableApi +public interface ContextAwareCallable extends Callable, ContextHolder { + + /** + * Returns a new {@link ContextAwareCallable} that sets the specified {@link RequestContext} + * before executing an underlying {@link Callable}. + */ + static ContextAwareCallable of(RequestContext context, Callable callable) { + return new DefaultContextAwareCallable(context, callable); + } + + /** + * Returns the {@link RequestContext} that was specified when creating + * this {@link ContextAwareCallable}. + */ + @Override + RequestContext context(); + + /** + * Returns the {@link Callable} that's executed without setting + * the {@link RequestContext}. + */ + Callable withoutContext(); +} diff --git a/core/src/main/java/com/linecorp/armeria/common/ContextAwareConsumer.java b/core/src/main/java/com/linecorp/armeria/common/ContextAwareConsumer.java new file mode 100644 index 00000000000..1e7a65101db --- /dev/null +++ b/core/src/main/java/com/linecorp/armeria/common/ContextAwareConsumer.java @@ -0,0 +1,50 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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 com.linecorp.armeria.common; + +import java.util.function.Consumer; + +import com.linecorp.armeria.common.annotation.UnstableApi; + +/** + * A delegating {@link Consumer} that makes sure an underlying Consumer is + * executed within the {@link RequestContext}. + */ +@UnstableApi +public interface ContextAwareConsumer extends Consumer, ContextHolder { + + /** + * Returns a new {@link ContextAwareConsumer} that sets the specified {@link RequestContext} + * before executing an underlying {@link Consumer}. + */ + static ContextAwareConsumer of(RequestContext context, Consumer action) { + return new DefaultContextAwareConsumer(context, action); + } + + /** + * Returns the {@link RequestContext} that was specified when creating + * this {@link ContextAwareConsumer}. + */ + @Override + RequestContext context(); + + /** + * Returns the {@link Consumer} that's executed without setting + * the {@link RequestContext}. + */ + Consumer withoutContext(); +} diff --git a/core/src/main/java/com/linecorp/armeria/common/ContextAwareFunction.java b/core/src/main/java/com/linecorp/armeria/common/ContextAwareFunction.java new file mode 100644 index 00000000000..8ea2ed4eca7 --- /dev/null +++ b/core/src/main/java/com/linecorp/armeria/common/ContextAwareFunction.java @@ -0,0 +1,50 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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 com.linecorp.armeria.common; + +import java.util.function.Function; + +import com.linecorp.armeria.common.annotation.UnstableApi; + +/** + * A delegating {@link Function} that makes sure an underlying Function is + * executed within the {@link RequestContext}. + */ +@UnstableApi +public interface ContextAwareFunction extends Function, ContextHolder { + + /** + * Returns a new {@link ContextAwareFuture} that sets the specified {@link RequestContext} + * before executing an underlying {@link Function}. + */ + static ContextAwareFunction of(RequestContext context, Function function) { + return new DefaultContextAwareFunction(context, function); + } + + /** + * Returns the {@link RequestContext} that was specified when creating + * this {@link ContextAwareFunction}. + */ + @Override + RequestContext context(); + + /** + * Returns the {@link Function} that's executed without setting + * the {@link RequestContext}. + */ + Function withoutContext(); +} diff --git a/core/src/main/java/com/linecorp/armeria/common/ContextAwareRunnable.java b/core/src/main/java/com/linecorp/armeria/common/ContextAwareRunnable.java new file mode 100644 index 00000000000..0d3d061c242 --- /dev/null +++ b/core/src/main/java/com/linecorp/armeria/common/ContextAwareRunnable.java @@ -0,0 +1,48 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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 com.linecorp.armeria.common; + +import com.linecorp.armeria.common.annotation.UnstableApi; + +/** + * A delegating {@link Runnable} that makes sure an underlying Runnable is + * executed within the {@link RequestContext}. + */ +@UnstableApi +public interface ContextAwareRunnable extends Runnable, ContextHolder { + + /** + * Returns a new {@link ContextAwareRunnable} that sets the specified {@link RequestContext} + * before executing an underlying {@link Runnable}. + */ + static ContextAwareRunnable of(RequestContext context, Runnable runnable) { + return new DefaultContextAwareRunnable(context, runnable); + } + + /** + * Returns the {@link RequestContext} that was specified when creating + * this {@link ContextAwareRunnable}. + */ + @Override + RequestContext context(); + + /** + * Returns the {@link Runnable} that's executed without setting + * the {@link RequestContext}. + */ + Runnable withoutContext(); +} diff --git a/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareBiConsumer.java b/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareBiConsumer.java new file mode 100644 index 00000000000..00a9dce93b6 --- /dev/null +++ b/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareBiConsumer.java @@ -0,0 +1,50 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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 com.linecorp.armeria.common; + +import static java.util.Objects.requireNonNull; + +import java.util.function.BiConsumer; + +import com.linecorp.armeria.common.util.SafeCloseable; + +final class DefaultContextAwareBiConsumer implements ContextAwareBiConsumer { + private final RequestContext context; + private final BiConsumer action; + + DefaultContextAwareBiConsumer(RequestContext context, BiConsumer action) { + this.context = requireNonNull(context, "context"); + this.action = requireNonNull(action, "action"); + } + + @Override + public RequestContext context() { + return context; + } + + @Override + public BiConsumer withoutContext() { + return action; + } + + @Override + public void accept(T t, U u) { + try (SafeCloseable ignored = context.push()) { + action.accept(t, u); + } + } +} diff --git a/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareBiFunction.java b/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareBiFunction.java new file mode 100644 index 00000000000..10ec70e28e1 --- /dev/null +++ b/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareBiFunction.java @@ -0,0 +1,51 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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 com.linecorp.armeria.common; + +import static java.util.Objects.requireNonNull; + +import java.util.function.BiFunction; + +import com.linecorp.armeria.common.util.SafeCloseable; + +final class DefaultContextAwareBiFunction implements ContextAwareBiFunction { + + private final RequestContext context; + private final BiFunction function; + + DefaultContextAwareBiFunction(RequestContext context, BiFunction function) { + this.context = requireNonNull(context, "context"); + this.function = requireNonNull(function, "function"); + } + + @Override + public RequestContext context() { + return context; + } + + @Override + public BiFunction withoutContext() { + return function; + } + + @Override + public R apply(T t, U u) { + try (SafeCloseable ignored = context.push()) { + return function.apply(t, u); + } + } +} diff --git a/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareCallable.java b/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareCallable.java new file mode 100644 index 00000000000..86b8f5f619e --- /dev/null +++ b/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareCallable.java @@ -0,0 +1,50 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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 com.linecorp.armeria.common; + +import static java.util.Objects.requireNonNull; + +import java.util.concurrent.Callable; + +import com.linecorp.armeria.common.util.SafeCloseable; + +final class DefaultContextAwareCallable implements ContextAwareCallable { + private final RequestContext context; + private final Callable callable; + + DefaultContextAwareCallable(RequestContext context, Callable callable) { + this.context = requireNonNull(context, "context"); + this.callable = requireNonNull(callable, "callable"); + } + + @Override + public RequestContext context() { + return context; + } + + @Override + public Callable withoutContext() { + return callable; + } + + @Override + public T call() throws Exception { + try (SafeCloseable ignored = context.push()) { + return callable.call(); + } + } +} diff --git a/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareConsumer.java b/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareConsumer.java new file mode 100644 index 00000000000..b980f664b4c --- /dev/null +++ b/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareConsumer.java @@ -0,0 +1,50 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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 com.linecorp.armeria.common; + +import static java.util.Objects.requireNonNull; + +import java.util.function.Consumer; + +import com.linecorp.armeria.common.util.SafeCloseable; + +final class DefaultContextAwareConsumer implements ContextAwareConsumer { + private final RequestContext context; + private final Consumer action; + + DefaultContextAwareConsumer(RequestContext context, Consumer action) { + this.context = requireNonNull(context, "context"); + this.action = requireNonNull(action, "action"); + } + + @Override + public RequestContext context() { + return context; + } + + @Override + public Consumer withoutContext() { + return action; + } + + @Override + public void accept(T t) { + try (SafeCloseable ignored = context.push()) { + action.accept(t); + } + } +} diff --git a/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareFunction.java b/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareFunction.java new file mode 100644 index 00000000000..054d556c8b2 --- /dev/null +++ b/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareFunction.java @@ -0,0 +1,51 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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 com.linecorp.armeria.common; + +import static java.util.Objects.requireNonNull; + +import java.util.function.Function; + +import com.linecorp.armeria.common.util.SafeCloseable; + +final class DefaultContextAwareFunction implements ContextAwareFunction { + + private final RequestContext context; + private final Function function; + + DefaultContextAwareFunction(RequestContext context, Function function) { + this.context = requireNonNull(context, "context"); + this.function = requireNonNull(function, "function"); + } + + @Override + public RequestContext context() { + return context; + } + + @Override + public Function withoutContext() { + return function; + } + + @Override + public R apply(T t) { + try (SafeCloseable ignored = context.push()) { + return function.apply(t); + } + } +} diff --git a/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareRunnable.java b/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareRunnable.java new file mode 100644 index 00000000000..d1dc7993fbf --- /dev/null +++ b/core/src/main/java/com/linecorp/armeria/common/DefaultContextAwareRunnable.java @@ -0,0 +1,48 @@ +/* + * Copyright 2023 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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 com.linecorp.armeria.common; + +import static java.util.Objects.requireNonNull; + +import com.linecorp.armeria.common.util.SafeCloseable; + +final class DefaultContextAwareRunnable implements ContextAwareRunnable { + private final RequestContext context; + private final Runnable runnable; + + DefaultContextAwareRunnable(RequestContext context, Runnable runnable) { + this.context = requireNonNull(context, "context"); + this.runnable = requireNonNull(runnable, "runnable"); + } + + @Override + public RequestContext context() { + return context; + } + + @Override + public Runnable withoutContext() { + return runnable; + } + + @Override + public void run() { + try (SafeCloseable ignored = context.push()) { + runnable.run(); + } + } +} diff --git a/core/src/main/java/com/linecorp/armeria/common/RequestContext.java b/core/src/main/java/com/linecorp/armeria/common/RequestContext.java index e89d6c4b75d..280e1d113e7 100644 --- a/core/src/main/java/com/linecorp/armeria/common/RequestContext.java +++ b/core/src/main/java/com/linecorp/armeria/common/RequestContext.java @@ -594,81 +594,51 @@ default ContextAwareBlockingTaskExecutor makeContextAware(BlockingTaskExecutor e } /** - * Returns a {@link Callable} that makes sure the current {@link RequestContext} is set and then invokes - * the input {@code callable}. + * Returns a {@link ContextAwareCallable} that makes sure the current {@link RequestContext} is + * set and then invokes the input {@code callable}. */ default Callable makeContextAware(Callable callable) { - requireNonNull(callable, "callable"); - return () -> { - try (SafeCloseable ignored = push()) { - return callable.call(); - } - }; + return ContextAwareCallable.of(this, callable); } /** - * Returns a {@link Runnable} that makes sure the current {@link RequestContext} is set and then invokes - * the input {@code runnable}. + * Returns a {@link ContextAwareRunnable} that makes sure the current {@link RequestContext} is + * set and then invokes the input {@code runnable}. */ default Runnable makeContextAware(Runnable runnable) { - requireNonNull(runnable, "runnable"); - return () -> { - try (SafeCloseable ignored = push()) { - runnable.run(); - } - }; + return ContextAwareRunnable.of(this, runnable); } /** - * Returns a {@link Function} that makes sure the current {@link RequestContext} is set and then invokes - * the input {@code function}. + * Returns a {@link ContextAwareFunction} that makes sure the current {@link RequestContext} is + * set and then invokes the input {@code function}. */ default Function makeContextAware(Function function) { - requireNonNull(function, "function"); - return t -> { - try (SafeCloseable ignored = push()) { - return function.apply(t); - } - }; + return ContextAwareFunction.of(this, function); } /** - * Returns a {@link BiFunction} that makes sure the current {@link RequestContext} is set and then invokes - * the input {@code function}. + * Returns a {@link ContextAwareBiFunction} that makes sure the current {@link RequestContext} is + * set and then invokes the input {@code function}. */ default BiFunction makeContextAware(BiFunction function) { - requireNonNull(function, "function"); - return (t, u) -> { - try (SafeCloseable ignored = push()) { - return function.apply(t, u); - } - }; + return ContextAwareBiFunction.of(this, function); } /** - * Returns a {@link Consumer} that makes sure the current {@link RequestContext} is set and then invokes - * the input {@code action}. + * Returns a {@link ContextAwareConsumer} that makes sure the current {@link RequestContext} is + * set and then invokes the input {@code action}. */ default Consumer makeContextAware(Consumer action) { - requireNonNull(action, "action"); - return t -> { - try (SafeCloseable ignored = push()) { - action.accept(t); - } - }; + return ContextAwareConsumer.of(this, action); } /** - * Returns a {@link BiConsumer} that makes sure the current {@link RequestContext} is set and then invokes - * the input {@code action}. + * Returns a {@link ContextAwareBiConsumer} that makes sure the current {@link RequestContext} is + * set and then invokes the input {@code action}. */ default BiConsumer makeContextAware(BiConsumer action) { - requireNonNull(action, "action"); - return (t, u) -> { - try (SafeCloseable ignored = push()) { - action.accept(t, u); - } - }; + return ContextAwareBiConsumer.of(this, action); } /**