From fba195afc7fcfbb8969da5171d60076e17ca1d69 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Thu, 28 Nov 2024 16:35:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20apply=20=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=90=8D=E4=B8=BA=20transformValue=EF=BC=9B=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=20int=E3=80=81long=E3=80=81double=20=E7=9A=84=20checkValue=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=9B=E6=96=B0=E5=A2=9E=E7=9B=B8=E4=BA=92?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E7=9A=84=E6=96=B9=E6=B3=95=EF=BC=9BXxxRef=20?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E8=AE=BE=E4=B8=BA=20final=20=E7=B1=BB?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/base/BoolRef.java | 12 ++++- .../zhouxy/plusone/commons/base/ByteRef.java | 4 +- .../zhouxy/plusone/commons/base/CharRef.java | 4 +- .../plusone/commons/base/DoubleRef.java | 9 +++- .../zhouxy/plusone/commons/base/IntRef.java | 9 +++- .../zhouxy/plusone/commons/base/LongRef.java | 9 +++- .../xyz/zhouxy/plusone/commons/base/Ref.java | 54 +++++++++++++++++-- .../zhouxy/plusone/commons/util/RefTests.java | 11 ++-- 8 files changed, 93 insertions(+), 19 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/BoolRef.java b/src/main/java/xyz/zhouxy/plusone/commons/base/BoolRef.java index 0527d74..69e16c4 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/BoolRef.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/BoolRef.java @@ -22,7 +22,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; import xyz.zhouxy.plusone.commons.function.BoolUnaryOperator; @Beta -public class BoolRef { +public final class BoolRef { private boolean value; @@ -43,10 +43,18 @@ public class BoolRef { this.value = value; } - public void apply(BoolUnaryOperator operator) { + public void transformValue(BoolUnaryOperator operator) { this.value = operator.applyAsBool(this.value); } + public boolean isTrue() { + return getValue(); + } + + public boolean isFalse() { + return !value; + } + @Override public String toString() { return String.format("BoolRef[%s]", value); diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/ByteRef.java b/src/main/java/xyz/zhouxy/plusone/commons/base/ByteRef.java index 4727db1..9fb0254 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/ByteRef.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/ByteRef.java @@ -22,7 +22,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; import xyz.zhouxy.plusone.commons.function.ByteUnaryOperator; @Beta -public class ByteRef { +public final class ByteRef { private byte value; @@ -43,7 +43,7 @@ public class ByteRef { this.value = value; } - public void apply(ByteUnaryOperator operator) { + public void transformValue(ByteUnaryOperator operator) { this.value = operator.applyAsByte(this.value); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/CharRef.java b/src/main/java/xyz/zhouxy/plusone/commons/base/CharRef.java index 35d409c..1e31f43 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/CharRef.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/CharRef.java @@ -22,7 +22,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; import xyz.zhouxy.plusone.commons.function.CharUnaryOperator; @Beta -public class CharRef { +public final class CharRef { private char value; @@ -43,7 +43,7 @@ public class CharRef { this.value = value; } - public void apply(CharUnaryOperator operator) { + public void transformValue(CharUnaryOperator operator) { this.value = operator.applyAsChar(this.value); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/DoubleRef.java b/src/main/java/xyz/zhouxy/plusone/commons/base/DoubleRef.java index addecb7..a370544 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/DoubleRef.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/DoubleRef.java @@ -16,6 +16,7 @@ package xyz.zhouxy.plusone.commons.base; +import java.util.function.DoublePredicate; import java.util.function.DoubleUnaryOperator; import com.google.common.annotations.Beta; @@ -23,7 +24,7 @@ import com.google.common.annotations.Beta; import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; @Beta -public class DoubleRef { +public final class DoubleRef { private double value; @@ -44,10 +45,14 @@ public class DoubleRef { this.value = value; } - public void apply(DoubleUnaryOperator operator) { + public void transformValue(DoubleUnaryOperator operator) { this.value = operator.applyAsDouble(this.value); } + public boolean checkValue(DoublePredicate predicate) { + return predicate.test(this.value); + } + @Override public String toString() { return String.format("DoubleRef[%s]", value); diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/IntRef.java b/src/main/java/xyz/zhouxy/plusone/commons/base/IntRef.java index 32b9b2b..bf67864 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/IntRef.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/IntRef.java @@ -16,6 +16,7 @@ package xyz.zhouxy.plusone.commons.base; +import java.util.function.IntPredicate; import java.util.function.IntUnaryOperator; import com.google.common.annotations.Beta; @@ -23,7 +24,7 @@ import com.google.common.annotations.Beta; import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; @Beta -public class IntRef { +public final class IntRef { private int value; @@ -44,10 +45,14 @@ public class IntRef { this.value = value; } - public void apply(IntUnaryOperator operator) { + public void transformValue(IntUnaryOperator operator) { this.value = operator.applyAsInt(this.value); } + public boolean checkValue(IntPredicate predicate) { + return predicate.test(this.value); + } + @Override public String toString() { return String.format("IntRef[%s]", value); diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/LongRef.java b/src/main/java/xyz/zhouxy/plusone/commons/base/LongRef.java index 4f4dd48..38d44fa 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/LongRef.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/LongRef.java @@ -16,6 +16,7 @@ package xyz.zhouxy.plusone.commons.base; +import java.util.function.LongPredicate; import java.util.function.LongUnaryOperator; import com.google.common.annotations.Beta; @@ -23,7 +24,7 @@ import com.google.common.annotations.Beta; import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; @Beta -public class LongRef { +public final class LongRef { private long value; @@ -44,10 +45,14 @@ public class LongRef { this.value = value; } - public void apply(LongUnaryOperator operator) { + public void transformValue(LongUnaryOperator operator) { this.value = operator.applyAsLong(this.value); } + public boolean checkValue(LongPredicate predicate) { + return predicate.test(this.value); + } + @Override public String toString() { return String.format("LongRef[%s]", value); 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 28f6067..1e70712 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/Ref.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/Ref.java @@ -18,6 +18,8 @@ package xyz.zhouxy.plusone.commons.base; import java.util.Objects; import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; import java.util.function.UnaryOperator; import com.google.common.annotations.Beta; @@ -55,10 +57,22 @@ public final class Ref { this.value = value; } - public void transform(UnaryOperator operator) { + public void transformValue(UnaryOperator operator) { this.value = operator.apply(this.value); } + public Ref transform(Function function) { + return Ref.of(function.apply(this.value)); + } + + public boolean checkValue(Predicate predicate) { + return predicate.test(this.value); + } + + public void execute(Consumer consumer) { + consumer.accept(value); + } + public boolean isNull() { return this.value == null; } @@ -67,10 +81,44 @@ public final class Ref { return this.value != null; } - public void execute(Consumer consumer) { - consumer.accept(value); + // ================================ + // #region - from + // ================================ + + @StaticFactoryMethod(Ref.class) + public static Ref from(BoolRef boolRef) { + return new Ref<>(boolRef.getValue()); } + @StaticFactoryMethod(Ref.class) + public static Ref from(ByteRef byteRef) { + return new Ref<>(byteRef.getValue()); + } + + @StaticFactoryMethod(Ref.class) + public static Ref from(CharRef charRef) { + return new Ref<>(charRef.getValue()); + } + + @StaticFactoryMethod(Ref.class) + public static Ref from(DoubleRef doubleRef) { + return new Ref<>(doubleRef.getValue()); + } + + @StaticFactoryMethod(Ref.class) + public static Ref from(IntRef intRef) { + return new Ref<>(intRef.getValue()); + } + + @StaticFactoryMethod(Ref.class) + public static Ref from(LongRef longRef) { + return new Ref<>(longRef.getValue()); + } + + // ================================ + // #endregion - from + // ================================ + @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 34e9fa3..660523e 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/RefTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/RefTests.java @@ -34,9 +34,12 @@ class RefTests { @Test void testRef() { Ref strRef = new Ref<>("ZhouXY"); + assertTrue(strRef.checkValue("ZhouXY"::equals)); apply(strRef); assertEquals("Hello ZhouXY", strRef.getValue()); log.info("strRef: {}", strRef); + + assertTrue(strRef.checkValue(str -> str.length() == 12)); } void apply(Ref strRef) { @@ -69,7 +72,7 @@ class RefTests { } void apply(boolean condition, CharRef charRef) { - charRef.apply(c -> condition ? '1' : '0'); + charRef.transformValue(c -> condition ? '1' : '0'); } @Test @@ -81,7 +84,7 @@ class RefTests { } void apply(double num, DoubleRef doubleRef) { - doubleRef.apply(d -> d * num); + doubleRef.transformValue(d -> d * num); } @Test @@ -93,7 +96,7 @@ class RefTests { } void apply(int num, IntRef intRef) { - intRef.apply(d -> d - num); + intRef.transformValue(d -> d - num); } @Test @@ -105,6 +108,6 @@ class RefTests { } void apply(long num, LongRef longRef) { - longRef.apply(d -> d + num); + longRef.transformValue(d -> d + num); } }