Long story short, it's a utility for creating transform&validation sequence for values.
- Create your own
ActionsExecutor
object
ActionsExecutor<String, Integer> actionsExecutor = new ActionsExecutor<>();
- Add few
RuleAction
actions for check value
actionsExecutor
.add(new NotNullRule<String>("ALARMA! Value cannot be null!"))
.add(new NotEmptyRule("String must not be empty! Check input value."))
...
- Add data type converter
actionsExecutor.add(new StringToIntConverter());
- And some more rules
actionsExecutor.add(new RangeRule(6, 12, "Value must be in [6, 12] range"));
- And finally, start your sequence by calling
chainExecutor.run(value)
with source value to fetch the converted result
ActionsResult<Integer> result = actionsExecutor.run(value);
- To check if the sequence was completed sequentially call
result.isCorrect()
.
To extract converted value useresult.getValue()
. Or take error message list byresult.getFailedMessages()
.
Also you can create your own data converters and validators actions by expanding ChainCheckAction<T>
and ChainConvertAction<A, B>
classes.
checkActionsExecutor = new ActionsExecutor<String, Integer>()
.add(new NotNullRule<String>())
.add(new NotEmptyRule())
.add(new LengthRule(2, 6))
.add(new StringToIntConverter("Must contains only digits!"))
.add(new RangeRule<>(2, 20, false))
.add(new IntToStringConverter())
.setListener(new AbstractActionsListener() {
@Override
public void onSuccess(Integer value) {
Toast.makeText(DemoActivity.this, String.format("It's all good! Value %d", value), Toast.LENGTH_LONG).show();
}
@Override
public void onError(ChainActionException convertException) {
Toast.makeText(DemoActivity.this,String.format("Sequence failed! %s", convertException.getErrorMessage()), Toast.LENGTH_LONG).show();
}
})
.run("123");
Or follow the alternative way
...
ActionsResult<Integer> result = actionsExecutor.run("123");
if (result.isCorrect()) {
System.out.println(String.format("It's all good! Value %d", result.getValue()));
} else {
System.out.println(String.format("Sequence failed! %s", result.getErrorMessage()));
}
In case you need to run a single action.
int value = ...;
ActionsResult<Integer> result = ActionsExecutor.single(new RangeRule<>(2, 20, false), value)
if (result.isCorrect()) {
// todo: if value in range [2, 20]
}
For simple converting there are few default converters:
RegexReplaceConverter
- replace all sequence of characters that were defined by regex to some replacementIntToStringConverter
- converting Int to StringStringToIntConverter
- converting String to Int
Default data checkers:
RegexRule
- checking is it string matching a regex templateLengthRule
- checking the length of a stringNotEmptyRule
- checking string for emptinessRangeRule
- check ifComparable<T>
value is between range bordersNotNullRule
- checking object for null
Add it in your root build.gradle:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
Add the dependency to your project build.gradle file:
dependencies {
implementation "com.github.wiskiw:Value-Validator:{$version}"
}
Replace version
with last release version
Have any questions or suggestions? Chat me telegram or mail to ayablonski23@gmail.com