From 725283c8290b7e2db25fdb92010a147e4902fde9 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 3 Mar 2024 01:06:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20Ref?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/base/BoolRef.java | 52 ++++++++++ .../zhouxy/plusone/commons/base/CharRef.java | 52 ++++++++++ .../plusone/commons/base/DoubleRef.java | 55 +++++++++++ .../zhouxy/plusone/commons/base/IntRef.java | 52 ++++++++++ .../zhouxy/plusone/commons/base/LongRef.java | 52 ++++++++++ .../xyz/zhouxy/plusone/commons/base/Ref.java | 54 +++++++++++ .../commons/function/BoolUnaryOperator.java | 13 +++ .../commons/function/CharUnaryOperator.java | 9 ++ .../zhouxy/plusone/commons/util/RefTests.java | 94 +++++++++++++++++++ 9 files changed, 433 insertions(+) create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/base/BoolRef.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/base/CharRef.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/base/DoubleRef.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/base/IntRef.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/base/LongRef.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/base/Ref.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/function/BoolUnaryOperator.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/function/CharUnaryOperator.java create mode 100644 src/test/java/xyz/zhouxy/plusone/commons/util/RefTests.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/BoolRef.java b/src/main/java/xyz/zhouxy/plusone/commons/base/BoolRef.java new file mode 100644 index 0000000..90de3a4 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/BoolRef.java @@ -0,0 +1,52 @@ +package xyz.zhouxy.plusone.commons.base; + +import com.google.common.annotations.Beta; + +import xyz.zhouxy.plusone.commons.function.BoolUnaryOperator; + +@Beta +public class BoolRef { + + private boolean value; + + public BoolRef(boolean value) { + this.value = value; + } + + public boolean getValue() { + return value; + } + + public void setValue(boolean value) { + this.value = value; + } + + public void apply(BoolUnaryOperator operator) { + this.value = operator.applyAsBool(this.value); + } + + @Override + public String toString() { + return String.format("BoolRef[%s]", value); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (value ? 1231 : 1237); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + return value == ((BoolRef) obj).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 new file mode 100644 index 0000000..5a31cb1 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/CharRef.java @@ -0,0 +1,52 @@ +package xyz.zhouxy.plusone.commons.base; + +import com.google.common.annotations.Beta; + +import xyz.zhouxy.plusone.commons.function.CharUnaryOperator; + +@Beta +public class CharRef { + + private char value; + + public CharRef(char value) { + this.value = value; + } + + public char getValue() { + return value; + } + + public void setValue(char value) { + this.value = value; + } + + public void apply(CharUnaryOperator operator) { + this.value = operator.applyAsChar(this.value); + } + + @Override + public String toString() { + return String.format("CharRef[%s]", value); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + value; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + return value == ((CharRef) obj).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 new file mode 100644 index 0000000..dae7792 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/DoubleRef.java @@ -0,0 +1,55 @@ +package xyz.zhouxy.plusone.commons.base; + +import java.util.function.DoubleUnaryOperator; + +import com.google.common.annotations.Beta; + +@Beta +public class DoubleRef { + + private double value; + + public DoubleRef(double value) { + this.value = value; + } + + public double getValue() { + return value; + } + + public void setValue(double value) { + this.value = value; + } + + public void apply(DoubleUnaryOperator operator) { + this.value = operator.applyAsDouble(this.value); + } + + @Override + public String toString() { + return String.format("DoubleRef[%s]", value); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(value); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final DoubleRef other = (DoubleRef) obj; + return Double.doubleToLongBits(value) == Double.doubleToLongBits(other.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 new file mode 100644 index 0000000..97a680a --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/IntRef.java @@ -0,0 +1,52 @@ +package xyz.zhouxy.plusone.commons.base; + +import java.util.function.IntUnaryOperator; + +import com.google.common.annotations.Beta; + +@Beta +public class IntRef { + + private int value; + + public IntRef(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public void apply(IntUnaryOperator operator) { + this.value = operator.applyAsInt(this.value); + } + + @Override + public String toString() { + return String.format("IntRef[%s]", value); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + value; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + return value == ((IntRef) obj).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 new file mode 100644 index 0000000..a6b1029 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/LongRef.java @@ -0,0 +1,52 @@ +package xyz.zhouxy.plusone.commons.base; + +import java.util.function.LongUnaryOperator; + +import com.google.common.annotations.Beta; + +@Beta +public class LongRef { + + private long value; + + public LongRef(long value) { + this.value = value; + } + + public long getValue() { + return value; + } + + public void setValue(long value) { + this.value = value; + } + + public void apply(LongUnaryOperator operator) { + this.value = operator.applyAsLong(this.value); + } + + @Override + public String toString() { + return String.format("LongRef[%s]", value); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (int) (value ^ (value >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + return value == ((LongRef) obj).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 new file mode 100644 index 0000000..b33e7b4 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/Ref.java @@ -0,0 +1,54 @@ +package xyz.zhouxy.plusone.commons.base; + +import java.util.Objects; +import java.util.function.UnaryOperator; + +import com.google.common.annotations.Beta; + +@Beta +public final class Ref { + + private T value; + + public Ref(T value) { + this.value = value; + } + + public T getValue() { + return value; + } + + public void setValue(T value) { + this.value = value; + } + + public void apply(UnaryOperator operator) { + this.value = operator.apply(this.value); + } + + @Override + public String toString() { + return String.format("Ref[%s]", value); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((value == null) ? 0 : value.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Ref other = (Ref) obj; + return Objects.equals(this.value, other.value); + } + +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/function/BoolUnaryOperator.java b/src/main/java/xyz/zhouxy/plusone/commons/function/BoolUnaryOperator.java new file mode 100644 index 0000000..740f000 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/function/BoolUnaryOperator.java @@ -0,0 +1,13 @@ +package xyz.zhouxy.plusone.commons.function; + +import com.google.common.annotations.Beta; + +@Beta +@FunctionalInterface +public interface BoolUnaryOperator { + boolean applyAsBool(boolean operand); + + static BoolUnaryOperator not() { + return b -> !b; + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/function/CharUnaryOperator.java b/src/main/java/xyz/zhouxy/plusone/commons/function/CharUnaryOperator.java new file mode 100644 index 0000000..f5e83f9 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/function/CharUnaryOperator.java @@ -0,0 +1,9 @@ +package xyz.zhouxy.plusone.commons.function; + +import com.google.common.annotations.Beta; + +@Beta +@FunctionalInterface +public interface CharUnaryOperator { + char applyAsChar(char operand); +} diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/RefTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/RefTests.java new file mode 100644 index 0000000..6f76bd2 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/RefTests.java @@ -0,0 +1,94 @@ +package xyz.zhouxy.plusone.commons.util; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +import lombok.extern.slf4j.Slf4j; +import xyz.zhouxy.plusone.commons.base.BoolRef; +import xyz.zhouxy.plusone.commons.base.CharRef; +import xyz.zhouxy.plusone.commons.base.DoubleRef; +import xyz.zhouxy.plusone.commons.base.IntRef; +import xyz.zhouxy.plusone.commons.base.LongRef; +import xyz.zhouxy.plusone.commons.base.Ref; + +@Slf4j +class RefTests { + + @Test + void testRef() { + Ref strRef = new Ref<>("ZhouXY"); + apply(strRef); + assertEquals("Hello ZhouXY", strRef.getValue()); + log.info("strRef: {}", strRef); + } + + void apply(Ref strRef) { + strRef.apply(str -> "Hello " + str); + } + + @Test + void testBoolRef() { + BoolRef boolRef = new BoolRef(false); + apply(boolRef); + assertTrue(boolRef.getValue()); + log.info("boolRef: {}", boolRef); + } + + void apply(BoolRef boolRef) { + boolRef.setValue(true); + } + + @Test + void testCharRef() { + CharRef charRef = new CharRef('T'); + + apply(false, charRef); + assertEquals('0', charRef.getValue()); + log.info("charRef: {}", charRef); + + apply(true, charRef); + assertEquals('1', charRef.getValue()); + log.info("charRef: {}", charRef); + } + + void apply(boolean condition, CharRef charRef) { + charRef.apply(c -> condition ? '1' : '0'); + } + + @Test + void testDoubleRef() { + DoubleRef doubleRef = new DoubleRef(2.33); + apply(88.108, doubleRef); + assertEquals(2.33 * 88.108, doubleRef.getValue()); + log.info("doubleRef: {}", doubleRef); + } + + void apply(double num, DoubleRef doubleRef) { + doubleRef.apply(d -> d * num); + } + + @Test + void testIntRef() { + IntRef intRef = new IntRef(108); + apply(88, intRef); + assertEquals(108 - 88, intRef.getValue()); + log.info("intRef: {}", intRef); + } + + void apply(int num, IntRef intRef) { + intRef.apply(d -> d - num); + } + + @Test + void testLongRef() { + LongRef longRef = new LongRef(108L); + apply(88L, longRef); + assertEquals(108L + 88L, longRef.getValue()); + log.info("intRef: {}", longRef); + } + + void apply(long num, LongRef longRef) { + longRef.apply(d -> d + num); + } +}