From aad8c70a65a32c40540dcb8d48fe795352a452c6 Mon Sep 17 00:00:00 2001 From: Ishan Arora Date: Tue, 22 Dec 2020 13:14:27 +0530 Subject: [PATCH 1/6] Added Input Boolean Value --- .../value/defaults/InputBooleanValue.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/InputBooleanValue.java diff --git a/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/InputBooleanValue.java b/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/InputBooleanValue.java new file mode 100644 index 0000000..6cb144f --- /dev/null +++ b/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/InputBooleanValue.java @@ -0,0 +1,42 @@ +package com.github.kislayverma.rulette.core.ruleinput.value.defaults; + +import java.io.Serializable; + +import com.github.kislayverma.rulette.core.ruleinput.value.IInputValue; + +public class InputBooleanValue implements IInputValue, Serializable { + + private static final long serialVersionUID = -474469687493839730L; + + private final Boolean value; + + InputBooleanValue(String value){ + this.value = Boolean.valueOf(value); + } + + @Override + public String getDataType() { + return Boolean.class.getName(); + } + + @Override + public Boolean getValue() { + return this.value; + } + + @Override + public int compareTo(String obj) { + return this.value.compareTo(Boolean.valueOf(obj)); + } + + @Override + public int compareTo(IInputValue obj) { + return this.compareTo(obj); + } + + @Override + public boolean isEmpty() { + return this.value == Boolean.FALSE; + } + +} From 46bd237bf44f6357ed446675f0f24493af9b771d Mon Sep 17 00:00:00 2001 From: Ishan Arora Date: Tue, 22 Dec 2020 13:16:11 +0530 Subject: [PATCH 2/6] Added Boolean as first class value and a default Boolean Builder --- .../value/defaults/DefaultBooleanInputBuilder.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/DefaultBooleanInputBuilder.java diff --git a/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/DefaultBooleanInputBuilder.java b/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/DefaultBooleanInputBuilder.java new file mode 100644 index 0000000..a7e540c --- /dev/null +++ b/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/DefaultBooleanInputBuilder.java @@ -0,0 +1,13 @@ +package com.github.kislayverma.rulette.core.ruleinput.value.defaults; + +import com.github.kislayverma.rulette.core.ruleinput.value.IInputValue; +import com.github.kislayverma.rulette.core.ruleinput.value.IInputValueBuilder; + +public class DefaultBooleanInputBuilder implements IInputValueBuilder { + + @Override + public IInputValue build(String value) { + return new InputBooleanValue(value); + } + +} From c756e97fb3ba9e970d9f9c9a2feb5dcc5c425131 Mon Sep 17 00:00:00 2001 From: Ishan Arora Date: Tue, 22 Dec 2020 13:17:55 +0530 Subject: [PATCH 3/6] Added Boolean Input Builder to the Default Builder Registry --- .../core/ruleinput/value/defaults/DefaultBuilderRegistry.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/DefaultBuilderRegistry.java b/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/DefaultBuilderRegistry.java index 17f56dc..961ec46 100644 --- a/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/DefaultBuilderRegistry.java +++ b/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/DefaultBuilderRegistry.java @@ -17,6 +17,8 @@ public DefaultBuilderRegistry() { this.builderRegister.put("STRING", new DefaultStringInputBuilder()); this.builderRegister.put("DATE", new DefaultDateInputBuilder()); this.builderRegister.put("NUMBER", new DefaultNumberInputBuilder()); + this.builderRegister.put("BOOLEAN", new DefaultBooleanInputBuilder()); + } public IInputValueBuilder getDefaultBuilder(String dataType) { From 4100a0c0b80fef4e0899936b84270b7136ea00b5 Mon Sep 17 00:00:00 2001 From: Ishan Arora Date: Tue, 22 Dec 2020 13:44:43 +0530 Subject: [PATCH 4/6] Added Input Boolean Value --- .../value/defaults/InputBooleanValue.java | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/InputBooleanValue.java b/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/InputBooleanValue.java index 6cb144f..999e11a 100644 --- a/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/InputBooleanValue.java +++ b/rulette-core/src/main/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/InputBooleanValue.java @@ -10,8 +10,8 @@ public class InputBooleanValue implements IInputValue, Serializable { private final Boolean value; - InputBooleanValue(String value){ - this.value = Boolean.valueOf(value); + InputBooleanValue(String value) { + this.value = value == null || value.isEmpty() ? null : Boolean.valueOf(value); } @Override @@ -24,19 +24,47 @@ public Boolean getValue() { return this.value; } + @Override + public boolean equals(Object obj) { + IInputValue that = (IInputValue) obj; + if (this.isEmpty() && that.isEmpty()) { + return true; + } else if (!this.isEmpty()) { + return this.value.equals(that.getValue()); + } else { + return that.getValue().equals(this.value); + } + } + @Override public int compareTo(String obj) { - return this.value.compareTo(Boolean.valueOf(obj)); + if ((obj == null || "".equals(obj)) && (this.value == null)) { + return 0; + } else if (obj == null || "".equals(obj)) { + return 1; + } else if (this.value == null) { + return -1; + } else { + return this.value.compareTo(Boolean.valueOf(obj)); + } } @Override - public int compareTo(IInputValue obj) { - return this.compareTo(obj); + public int compareTo(IInputValue obj) { + if ((obj == null) && (this.value == null)) { + return 0; + } else if (obj == null || obj.isEmpty()) { + return 1; + } else if (this.value == null) { + return -1; + } else { + return this.value.compareTo((Boolean)obj.getValue()); + } } @Override public boolean isEmpty() { - return this.value == Boolean.FALSE; + return this.value == null; } - + } From 2e52ca32854272ef75f7f2d7773e0e71a747c4b5 Mon Sep 17 00:00:00 2001 From: Ishan Arora Date: Tue, 22 Dec 2020 13:46:28 +0530 Subject: [PATCH 5/6] Added Test for the DefaultBooleanBuilder --- .../impl/DefaultBooleanInputBuilderTest.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 rulette-core/src/test/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/impl/DefaultBooleanInputBuilderTest.java diff --git a/rulette-core/src/test/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/impl/DefaultBooleanInputBuilderTest.java b/rulette-core/src/test/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/impl/DefaultBooleanInputBuilderTest.java new file mode 100644 index 0000000..e5bdede --- /dev/null +++ b/rulette-core/src/test/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/impl/DefaultBooleanInputBuilderTest.java @@ -0,0 +1,27 @@ +package com.github.kislayverma.rulette.core.ruleinput.value.defaults.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.github.kislayverma.rulette.core.ruleinput.value.IInputValue; +import com.github.kislayverma.rulette.core.ruleinput.value.defaults.DefaultBooleanInputBuilder; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class DefaultBooleanInputBuilderTest { + + @Test + @DisplayName("should return false for a random string") + void shouldReturnFalseForRandomValue() { + IInputValue ivb = new DefaultBooleanInputBuilder().build("xnanannkk"); + assertEquals(Boolean.FALSE, ivb.getValue(), "Random String values are treated as false"); + } + + @Test + @DisplayName("should return true for a String TRUE value") + void shouldReturnFalseForTRUEValue() { + IInputValue ivb = new DefaultBooleanInputBuilder().build("true"); + assertEquals(Boolean.TRUE, ivb.getValue(), "A String True Value treated as true"); + } + +} \ No newline at end of file From 4f4da4b3726e99be8c13ff85eae6553f9ea94d6b Mon Sep 17 00:00:00 2001 From: Ishan Arora Date: Tue, 22 Dec 2020 15:36:59 +0530 Subject: [PATCH 6/6] fix --- .../value/defaults/impl/DefaultBooleanInputBuilderTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rulette-core/src/test/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/impl/DefaultBooleanInputBuilderTest.java b/rulette-core/src/test/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/impl/DefaultBooleanInputBuilderTest.java index e5bdede..d92cd4c 100644 --- a/rulette-core/src/test/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/impl/DefaultBooleanInputBuilderTest.java +++ b/rulette-core/src/test/java/com/github/kislayverma/rulette/core/ruleinput/value/defaults/impl/DefaultBooleanInputBuilderTest.java @@ -19,7 +19,7 @@ void shouldReturnFalseForRandomValue() { @Test @DisplayName("should return true for a String TRUE value") - void shouldReturnFalseForTRUEValue() { + void shouldReturnTrueForTRUEValue() { IInputValue ivb = new DefaultBooleanInputBuilder().build("true"); assertEquals(Boolean.TRUE, ivb.getValue(), "A String True Value treated as true"); }