Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added boolean data type as first class objects #81

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Boolean> {

@Override
public IInputValue<Boolean> build(String value) {
return new InputBooleanValue(value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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<Boolean>, Serializable {

private static final long serialVersionUID = -474469687493839730L;

private final Boolean value;

InputBooleanValue(String value) {
this.value = value == null || value.isEmpty() ? null : Boolean.valueOf(value);
}

@Override
public String getDataType() {
return Boolean.class.getName();
}

@Override
public Boolean getValue() {
return this.value;
}

@Override
public boolean equals(Object obj) {
IInputValue<Boolean> that = (IInputValue<Boolean>) 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) {
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) {
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 == null;
}

}
Original file line number Diff line number Diff line change
@@ -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<Boolean> 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 shouldReturnTrueForTRUEValue() {
IInputValue<Boolean> ivb = new DefaultBooleanInputBuilder().build("true");
assertEquals(Boolean.TRUE, ivb.getValue(), "A String True Value treated as true");
}

}