From 3d5a3ddbee58cd039a8cb54d28823b8d7b35c36b Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 15 Mar 2024 09:28:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E3=80=81=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xyz/zhouxy/plusone/commons/base/Ref.java | 19 ++++++++++++++++++- .../zhouxy/plusone/commons/util/RefTests.java | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/Ref.java b/src/main/java/xyz/zhouxy/plusone/commons/base/Ref.java index b33e7b4..2201f32 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/Ref.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/Ref.java @@ -1,6 +1,7 @@ package xyz.zhouxy.plusone.commons.base; import java.util.Objects; +import java.util.function.Consumer; import java.util.function.UnaryOperator; import com.google.common.annotations.Beta; @@ -10,6 +11,10 @@ public final class Ref { private T value; + public Ref() { + this.value = null; + } + public Ref(T value) { this.value = value; } @@ -22,10 +27,22 @@ public final class Ref { this.value = value; } - public void apply(UnaryOperator operator) { + public void transform(UnaryOperator operator) { this.value = operator.apply(this.value); } + public boolean isNull() { + return this.value == null; + } + + public boolean isNotNull() { + return this.value != null; + } + + public void execute(Consumer consumer) { + consumer.accept(value); + } + @Override public String toString() { return String.format("Ref[%s]", value); diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/RefTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/RefTests.java index 6f76bd2..c75bb83 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/RefTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/RefTests.java @@ -24,7 +24,7 @@ class RefTests { } void apply(Ref strRef) { - strRef.apply(str -> "Hello " + str); + strRef.transform(str -> "Hello " + str); } @Test