11 Commits

15 changed files with 299 additions and 481 deletions

View File

@@ -6,7 +6,7 @@
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-commons</artifactId>
<version>1.0.0-RC1</version>
<version>1.0.0-RC2</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@@ -1,68 +0,0 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed 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 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;
}
}

View File

@@ -1,68 +0,0 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed 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 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;
}
}

View File

@@ -1,71 +0,0 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed 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 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);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 the original author or authors.
* Copyright 2022-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package xyz.zhouxy.plusone.commons.base;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* 规定实现类带有 {@code getCode} 方法。
@@ -31,18 +32,19 @@ public interface IWithCode<T> {
@Nonnull
T getCode();
default boolean equalsCode(T code) {
default boolean isCodeEquals(@Nullable T code) {
return Objects.equals(getCode(), code);
}
default boolean equalsCode(IWithCode<?> obj) {
return obj != null && obj.getCode().equals(getCode());
default boolean isSameCodeAs(@Nullable IWithCode<?> other) {
return other != null && Objects.equals(getCode(), other.getCode());
}
default boolean equalsCode(IWithIntCode obj) {
return obj != null && getCode().equals(obj.getCode());
default boolean isSameCodeAs(@Nullable IWithIntCode other) {
return other != null && Objects.equals(getCode(), other.getCode());
}
default boolean equalsCode(IWithLongCode obj) {
return obj != null && getCode().equals(obj.getCode());
default boolean isSameCodeAs(@Nullable IWithLongCode other) {
return other != null && Objects.equals(getCode(), other.getCode());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 the original author or authors.
* Copyright 2022-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,10 @@
package xyz.zhouxy.plusone.commons.base;
import java.util.Objects;
import javax.annotation.Nullable;
/**
* 规定实现类带有 {@code getCode} 方法。
* 用于像自定义异常等需要带有 {@code code} 字段的类,
@@ -26,19 +30,19 @@ package xyz.zhouxy.plusone.commons.base;
public interface IWithIntCode {
int getCode();
default boolean equalsCode(int code) {
default boolean isCodeEquals(int code) {
return getCode() == code;
}
default boolean equalsCode(IWithCode<?> obj) {
return obj != null && obj.getCode().equals(getCode());
default boolean isSameCodeAs(@Nullable IWithCode<?> other) {
return other != null && Objects.equals(getCode(), other.getCode());
}
default boolean equalsCode(IWithIntCode obj) {
return obj != null && getCode() == obj.getCode();
default boolean isSameCodeAs(@Nullable IWithIntCode other) {
return other != null && getCode() == other.getCode();
}
default boolean equalsCode(IWithLongCode obj) {
return obj != null && getCode() == obj.getCode();
default boolean isSameCodeAs(@Nullable IWithLongCode other) {
return other != null && getCode() == other.getCode();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 the original author or authors.
* Copyright 2022-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,10 @@
package xyz.zhouxy.plusone.commons.base;
import java.util.Objects;
import javax.annotation.Nullable;
/**
* 规定实现类带有 {@code getCode} 方法。
* 用于像自定义异常等需要带有 {@code code} 字段的类,
@@ -26,19 +30,19 @@ package xyz.zhouxy.plusone.commons.base;
public interface IWithLongCode {
long getCode();
default boolean equalsCode(long code) {
default boolean isCodeEquals(long code) {
return getCode() == code;
}
default boolean equalsCode(IWithCode<?> obj) {
return obj != null && obj.getCode().equals(getCode());
default boolean isSameCodeAs(@Nullable IWithCode<?> other) {
return other != null && Objects.equals(getCode(), other.getCode());
}
default boolean equalsCode(IWithIntCode obj) {
return obj != null && getCode() == obj.getCode();
default boolean isSameCodeAs(@Nullable IWithIntCode other) {
return other != null && getCode() == other.getCode();
}
default boolean equalsCode(IWithLongCode obj) {
return obj != null && getCode() == obj.getCode();
default boolean isSameCodeAs(@Nullable IWithLongCode other) {
return other != null && getCode() == other.getCode();
}
}

View File

@@ -1,68 +0,0 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed 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 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;
}
}

View File

@@ -1,68 +0,0 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed 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 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;
}
}

View File

@@ -18,35 +18,100 @@ 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;
import javax.annotation.Nullable;
@Beta
/**
* {@link Ref} 包装了一个值,表示对该值的应用。
*
* <p>灵感来自于 C# 的 {@value ref} 参数修饰符。C# 允许通过以下方式,将值返回给调用端:</p>
* <pre>
* void Method(ref int refArgument)
* {
* refArgument = refArgument + 44;
* }
*
* int number = 1;
* Method(ref number);
* Console.WriteLine(number); // Output: 45
* </pre>
* {@link Ref} 使 Java 可以达到类似的效果,如:
* <pre>
* void method(final Ref&lt;Integer&gt; refArgument) {
* refArgument.transformValue(i -&gt; i + 44);
* }
*
* Ref&lt;Integer&gt; number = Ref.of(1);
* method(number);
* System.out.println(number.getValue()); // Output: 45
* </pre>
* <p>
* 当一个方法需要产生多个结果时,无法有多个返回值,可以使用 {@link Ref} 作为参数传入,方法内部修改 {@link Ref} 的值。
* 调用方在调用方法之后,使用 {@code getValue()} 获取结果。
* </p>
* <pre>
* String method(final Ref&lt;Integer&gt; intRefArgument, final Ref&lt;String&gt; strRefArgument) {
* intRefArgument.transformValue(i -&gt; i + 44);
* strRefArgument.setValue("Hello " + strRefArgument.getValue());
* return "Return string";
* }
*
* Ref&lt;Integer&gt; number = Ref.of(1);
* Ref&lt;String&gt; str = Ref.of("Java");
* String result = method(number, str);
* System.out.println(number.getValue()); // Output: 45
* System.out.println(str.getValue()); // Output: Hello Java
* System.out.println(result); // Output: Return string
* </pre>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public final class Ref<T> {
@Nullable
private T value;
public Ref() {
this.value = null;
}
public Ref(T value) {
private Ref(@Nullable T value) {
this.value = value;
}
public static <T> Ref<T> of(@Nullable T value) {
return new Ref<>(value);
}
public static <T> Ref<T> empty() {
return new Ref<>(null);
}
@Nullable
public T getValue() {
return value;
}
public void setValue(T value) {
public void setValue(@Nullable T value) {
this.value = value;
}
public void transform(UnaryOperator<T> operator) {
public void transformValue(UnaryOperator<T> operator) {
this.value = operator.apply(this.value);
}
public <R> Ref<R> transform(Function<? super T, R> function) {
return Ref.of(function.apply(this.value));
}
public boolean checkValue(Predicate<? super T> predicate) {
return predicate.test(this.value);
}
public void execute(Consumer<? super T> consumer) {
consumer.accept(value);
}
public boolean isNull() {
return this.value == null;
}
@@ -55,10 +120,6 @@ public final class Ref<T> {
return this.value != null;
}
public void execute(Consumer<? super T> consumer) {
consumer.accept(value);
}
@Override
public String toString() {
return String.format("Ref[%s]", value);
@@ -73,7 +134,7 @@ public final class Ref<T> {
}
@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
if (this == obj)
return true;
if (obj == null)

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed 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.
*/
/**
* 基础组件
*
* <h2>Ref</h2>
* <p>
* {@link Ref} 包装了一个值,表示对该值的应用。
* </p>
* <p>灵感来自于 C# 的 {@value ref} 参数修饰符。C# 允许通过以下方式,将值返回给调用端:</p>
* <pre>
* void Method(ref int refArgument)
* {
* refArgument = refArgument + 44;
* }
*
* int number = 1;
* Method(ref number);
* Console.WriteLine(number); // Output: 45
* </pre>
* {@link Ref} 使 Java 可以达到类似的效果,如:
* <pre>
* void method(Ref&lt;Integer&gt; refArgument) {
* refArgument.transformValue(i -&gt; i + 44);
* }
*
* Ref&lt;Integer&gt; number = Ref.of(1);
* method(number);
* System.out.println(number.getValue()); // Output: 45
* </pre>
* <p>
* 当一个方法需要产生多个结果时,无法有多个返回值,可以使用 {@link Ref} 作为参数传入,方法内部修改 {@link Ref} 的值。
* 调用方在调用方法之后,使用 {@code getValue()} 获取结果。
* </p>
* <pre>
* String method(Ref&lt;Integer&gt; intRefArgument, Ref&lt;String&gt; strRefArgument) {
* intRefArgument.transformValue(i -&gt; i + 44);
* strRefArgument.setValue("Hello " + strRefArgument.getValue());
* return "Return string";
* }
*
* Ref&lt;Integer&gt; number = Ref.of(1);
* Ref&lt;String&gt; str = Ref.of("Java");
* String result = method(number, str);
* System.out.println(number.getValue()); // Output: 45
* System.out.println(str.getValue()); // Output: Hello Java
* System.out.println(result); // Output: Return string
* </pre>
*
* <h2>IWithCode</h2>
* <p>
* 类似于枚举之类的类,通常需要设置固定的码值表示对应的含义。
* 可实现 {@link IWithCode}、{@link IWithIntCode}、{@link IWithLongCode},便于在需要的地方对这些接口的实现进行处理。
* </p>
*/
@CheckReturnValue
@ParametersAreNonnullByDefault
package xyz.zhouxy.plusone.commons.base;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.CheckReturnValue;

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed 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 xyz.zhouxy.plusone.commons.function;
@FunctionalInterface
public interface ThrowingFunction<T, R, E extends Throwable> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t) throws E;
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed 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.
*/
/**
* 函数式编程
*
* <h2>PredicateTools</h2>
* <p>
* {@link PredicateTools} 用于 {@link java.util.function.Predicate} 的相关操作。
* </p>
*
* <h2>Functional interfaces</h2>
* <p>
* 补充一些 JDK 没有,而项目中可能用得上的函数式接口:
* <pre>
* | Group | FunctionalInterface | method |
* | ------------- | -------------------- | -------------------------------- |
* | UnaryOperator | BoolUnaryOperator | boolean applyAsBool (boolean) |
* | UnaryOperator | CharUnaryOperator | char applyAsChar(char) |
* | Throwing | Executable | void execute() throws E |
* | Throwing | ThrowingConsumer | void accept(T) throws E |
* | Throwing | ThrowingFunction | R apply(T) throws E |
* | Throwing | ThrowingPredicate | boolean test(T) throws E |
* | Throwing | ThrowingSupplier | T get() throws E |
* | Optional | OptionalSupplier | Optional&lt;T&gt; get() throws E |
* | Optional | ToOptionalBiFunction | Optional&lt;R&gt; apply(T,U) |
* | Optional | ToOptionalFunction | Optional&lt;R&gt; apply(T) |
* </pre>
* </p>
*/
package xyz.zhouxy.plusone.commons.function;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2024 the original author or authors.
* Copyright 2024-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,57 +28,57 @@ class IWithCodeTests {
@Test
void equalsCode_SameCode_ReturnsTrue() {
assertTrue(WithCode.INSTANCE.equalsCode("testCode"));
assertTrue(WithCode.INSTANCE.isCodeEquals("testCode"));
Integer intCode = 0;
Long longCode = 0L;
assertTrue(WithIntCode.INSTANCE.equalsCode(intCode));
assertTrue(WithLongCode.INSTANCE.equalsCode(intCode));
assertTrue(WithLongCode.INSTANCE.equalsCode(longCode));
assertTrue(WithIntCode.INSTANCE.isCodeEquals(intCode));
assertTrue(WithLongCode.INSTANCE.isCodeEquals(intCode));
assertTrue(WithLongCode.INSTANCE.isCodeEquals(longCode));
assertTrue(WithCode.INSTANCE.equalsCode(WithCode.SAME_CODE_INSTANCE));
assertTrue(WithIntCode.INSTANCE.equalsCode(WithIntCode.SAME_CODE_INSTANCE));
assertTrue(WithIntCode.INSTANCE.equalsCode(WithLongCode.SAME_CODE_INSTANCE));
assertTrue(WithLongCode.INSTANCE.equalsCode(WithLongCode.SAME_CODE_INSTANCE));
assertTrue(WithLongCode.INSTANCE.equalsCode(WithIntCode.SAME_CODE_INSTANCE));
assertTrue(WithCode.INSTANCE.isSameCodeAs(WithCode.SAME_CODE_INSTANCE));
assertTrue(WithIntCode.INSTANCE.isSameCodeAs(WithIntCode.SAME_CODE_INSTANCE));
assertTrue(WithIntCode.INSTANCE.isSameCodeAs(WithLongCode.SAME_CODE_INSTANCE));
assertTrue(WithLongCode.INSTANCE.isSameCodeAs(WithLongCode.SAME_CODE_INSTANCE));
assertTrue(WithLongCode.INSTANCE.isSameCodeAs(WithIntCode.SAME_CODE_INSTANCE));
}
@Test
void equalsCode_DifferentCode_ReturnsFalse() {
assertFalse(WithCode.INSTANCE.equalsCode("wrongCode"));
assertFalse(WithCode.INSTANCE.isCodeEquals("wrongCode"));
Integer intCode = 108;
Long longCode = 108L;
assertFalse(WithIntCode.INSTANCE.equalsCode(intCode));
assertFalse(WithLongCode.INSTANCE.equalsCode(intCode));
assertFalse(WithLongCode.INSTANCE.equalsCode(longCode));
assertFalse(WithIntCode.INSTANCE.isCodeEquals(intCode));
assertFalse(WithLongCode.INSTANCE.isCodeEquals(intCode));
assertFalse(WithLongCode.INSTANCE.isCodeEquals(longCode));
assertFalse(WithCode.INSTANCE.equalsCode(WithCode.WRONG_CODE_INSTANCE));
assertFalse(WithIntCode.INSTANCE.equalsCode(WithIntCode.WRONG_CODE_INSTANCE));
assertFalse(WithIntCode.INSTANCE.equalsCode(WithLongCode.WRONG_CODE_INSTANCE));
assertFalse(WithLongCode.INSTANCE.equalsCode(WithLongCode.WRONG_CODE_INSTANCE));
assertFalse(WithLongCode.INSTANCE.equalsCode(WithIntCode.WRONG_CODE_INSTANCE));
assertFalse(WithCode.INSTANCE.isSameCodeAs(WithCode.WRONG_CODE_INSTANCE));
assertFalse(WithIntCode.INSTANCE.isSameCodeAs(WithIntCode.WRONG_CODE_INSTANCE));
assertFalse(WithIntCode.INSTANCE.isSameCodeAs(WithLongCode.WRONG_CODE_INSTANCE));
assertFalse(WithLongCode.INSTANCE.isSameCodeAs(WithLongCode.WRONG_CODE_INSTANCE));
assertFalse(WithLongCode.INSTANCE.isSameCodeAs(WithIntCode.WRONG_CODE_INSTANCE));
}
@Test
@SuppressWarnings("null")
void equalsCode_NullCode_ReturnsFalse() {
assertFalse(WithCode.INSTANCE.equalsCode((WithCode) null));
assertFalse(WithCode.INSTANCE.equalsCode((WithIntCode) null));
assertFalse(WithCode.INSTANCE.equalsCode((WithLongCode) null));
assertFalse(WithCode.INSTANCE.isSameCodeAs((WithCode) null));
assertFalse(WithCode.INSTANCE.isSameCodeAs((WithIntCode) null));
assertFalse(WithCode.INSTANCE.isSameCodeAs((WithLongCode) null));
assertFalse(WithIntCode.INSTANCE.equalsCode((WithCode) null));
assertFalse(WithIntCode.INSTANCE.equalsCode((WithIntCode) null));
assertFalse(WithIntCode.INSTANCE.equalsCode((WithLongCode) null));
assertFalse(WithIntCode.INSTANCE.isSameCodeAs((WithCode) null));
assertFalse(WithIntCode.INSTANCE.isSameCodeAs((WithIntCode) null));
assertFalse(WithIntCode.INSTANCE.isSameCodeAs((WithLongCode) null));
assertFalse(WithLongCode.INSTANCE.equalsCode((WithCode) null));
assertFalse(WithLongCode.INSTANCE.equalsCode((WithIntCode) null));
assertFalse(WithLongCode.INSTANCE.equalsCode((WithLongCode) null));
assertFalse(WithLongCode.INSTANCE.isSameCodeAs((WithCode) null));
assertFalse(WithLongCode.INSTANCE.isSameCodeAs((WithIntCode) null));
assertFalse(WithLongCode.INSTANCE.isSameCodeAs((WithLongCode) null));
assertFalse(WithCode.INSTANCE.equalsCode((String) null));
assertFalse(WithCode.INSTANCE.isCodeEquals((String) null));
Integer intCode = null;
Long longCode = null;
assertThrows(NullPointerException.class, () -> WithIntCode.INSTANCE.equalsCode(intCode));
assertThrows(NullPointerException.class, () -> WithLongCode.INSTANCE.equalsCode(intCode));
assertThrows(NullPointerException.class, () -> WithLongCode.INSTANCE.equalsCode(longCode));
assertThrows(NullPointerException.class, () -> WithIntCode.INSTANCE.isCodeEquals(intCode));
assertThrows(NullPointerException.class, () -> WithLongCode.INSTANCE.isCodeEquals(intCode));
assertThrows(NullPointerException.class, () -> WithLongCode.INSTANCE.isCodeEquals(longCode));
}
private static enum WithCode implements IWithCode<String> {

View File

@@ -27,78 +27,20 @@ class RefTests {
@Test
void testRef() {
Ref<String> strRef = new Ref<>("ZhouXY");
Ref<String> strRef = Ref.of("ZhouXY");
assertTrue(strRef.checkValue("ZhouXY"::equals));
assertFalse(strRef.checkValue("ZhouXY1"::equals));
apply(strRef);
assertEquals("Hello ZhouXY", strRef.getValue());
assertTrue(strRef.checkValue("Hello ZhouXY"::equals));
log.info("strRef: {}", strRef);
Ref<String> intStringRef = Ref.of("108");
Ref<Integer> integerRef = intStringRef.transform(Integer::parseInt);
assertEquals(108, integerRef.getValue());
}
void apply(Ref<String> strRef) {
strRef.transform(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);
strRef.transformValue(str -> "Hello " + str);
}
}