diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/IWithCode.java b/src/main/java/xyz/zhouxy/plusone/commons/base/IWithCode.java index 015fdc3..b8bc1bb 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/IWithCode.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/IWithCode.java @@ -34,4 +34,15 @@ public interface IWithCode { default boolean equalsCode(T code) { return Objects.equals(getCode(), code); } + default boolean equalsCode(IWithCode obj) { // TODO 单元测试 + return obj != null && obj.getCode().equals(getCode()); + } + + default boolean equalsCode(IWithIntCode obj) { // TODO 单元测试 + return obj != null && getCode().equals(obj.getCode()); + } + + default boolean equalsCode(IWithLongCode obj) { // TODO 单元测试 + return obj != null && getCode().equals(obj.getCode()); + } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/IWithIntCode.java b/src/main/java/xyz/zhouxy/plusone/commons/base/IWithIntCode.java index 7deac35..5a5f36c 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/IWithIntCode.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/IWithIntCode.java @@ -29,4 +29,16 @@ public interface IWithIntCode { default boolean equalsCode(int code) { return getCode() == code; } + + default boolean equalsCode(IWithCode obj) { // TODO 单元测试 + return obj != null && obj.getCode().equals(getCode()); + } + + default boolean equalsCode(IWithIntCode obj) { // TODO 单元测试 + return obj != null && getCode() == obj.getCode(); + } + + default boolean equalsCode(IWithLongCode obj) { // TODO 单元测试 + return obj != null && getCode() == obj.getCode(); + } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/IWithLongCode.java b/src/main/java/xyz/zhouxy/plusone/commons/base/IWithLongCode.java index 1bd8d20..7550358 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/IWithLongCode.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/base/IWithLongCode.java @@ -29,4 +29,16 @@ public interface IWithLongCode { default boolean equalsCode(long code) { return getCode() == code; } + + default boolean equalsCode(IWithCode obj) { // TODO 单元测试 + return obj != null && obj.getCode().equals(getCode()); + } + + default boolean equalsCode(IWithIntCode obj) { // TODO 单元测试 + return obj != null && getCode() == obj.getCode(); + } + + default boolean equalsCode(IWithLongCode obj) { // TODO 单元测试 + return obj != null && getCode() == obj.getCode(); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/base/IWithCodeTests.java b/src/test/java/xyz/zhouxy/plusone/commons/base/IWithCodeTests.java new file mode 100644 index 0000000..693d1b6 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/commons/base/IWithCodeTests.java @@ -0,0 +1,57 @@ +package xyz.zhouxy.plusone.commons.base; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import javax.annotation.Nonnull; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import xyz.zhouxy.plusone.commons.util.AssertTools; + +class IWithCodeTests { + + private static class WithCodeImpl implements IWithCode { + @Nonnull + private final String code; + + WithCodeImpl(String code) { + AssertTools.checkNotNull(code); + this.code = code; + } + + @Override + @Nonnull + public String getCode() { + return code; + } + } + + private WithCodeImpl instance; + + @BeforeEach + void setUp() { + instance = new WithCodeImpl("testCode"); + } + + @Test + void equalsCode_SameCode_ReturnsTrue() { + assertTrue(instance.equalsCode("testCode")); + } + + @Test + void equalsCode_DifferentCode_ReturnsFalse() { + assertFalse(instance.equalsCode("wrongCode")); + } + + @Test + void equalsCode_NullCode_ReturnsFalse() { + assertFalse(instance.equalsCode((String) null)); + } + + @Test + void equalsCode_NullObject_ReturnsFalse() { + assertFalse(instance.equalsCode((String) null)); + } +} diff --git a/src/test/java/xyz/zhouxy/plusone/commons/base/IWithIntCodeTests.java b/src/test/java/xyz/zhouxy/plusone/commons/base/IWithIntCodeTests.java new file mode 100644 index 0000000..60fa5ab --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/commons/base/IWithIntCodeTests.java @@ -0,0 +1,38 @@ +package xyz.zhouxy.plusone.commons.base; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +class IWithIntCodeTests { + @Test + void test() { + IWithIntCode instance1 = new WithIntCodeImpl(10); + IWithIntCode instance2 = new WithIntCodeImpl(20); + IWithIntCode instance3 = new WithIntCodeImpl(10); + + // Test for equalsCode method + assertTrue(instance1.equalsCode(10)); + assertFalse(instance1.equalsCode(20)); + assertTrue(instance2.equalsCode(20)); + assertTrue(instance3.equalsCode(10)); + + // Test for distinct instances with same code + assertTrue(instance1.equalsCode(instance3.getCode())); // because they have the same code + assertFalse(instance1.equalsCode(instance2.getCode())); // because they have different codes + } +} + +class WithIntCodeImpl implements IWithIntCode { + private int code; + + public WithIntCodeImpl(int code) { + this.code = code; + } + + @Override + public int getCode() { + return code; + } +}