org.simpleflatmapper.map.Result Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfm-map Show documentation
Show all versions of sfm-map Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
The newest version!
package org.simpleflatmapper.map;
import org.simpleflatmapper.converter.Context;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.util.Function;
import java.util.ArrayList;
import java.util.List;
public class Result {
private final T value;
private final List> errors;
private Result(T value, List> errors) {
this.value = value;
this.errors = errors;
}
public T getValue() {
return value;
}
public List> getErrors() {
return errors;
}
public boolean hasErrors() {
return !errors.isEmpty();
}
@Override
public String toString() {
return "Result{" +
"value=" + value +
", errors=" + errors +
'}';
}
public static class FieldError {
private final K key;
private final Throwable error;
public FieldError(K key, Throwable error) {
this.key = key;
this.error = error;
}
public K getKey() {
return key;
}
public Throwable getError() {
return error;
}
@Override
public String toString() {
return "FieldError{" +
"key=" + key +
", error=" + error +
'}';
}
}
@ReflectionService.PassThrough
public static class ResultBuilder {
private T value;
private final ArrayList> errors;
public ResultBuilder(Context context) {
this.errors = context.context(0);
}
public void setValue(T value) {
this.value = value;
}
public Result build() {
ArrayList> fieldErrors = new ArrayList>(errors);
errors.clear();
return new Result(value, fieldErrors);
}
}
public static Function, Result> buildingFunction() {
return new Function, Result>() {
@Override
public Result apply(ResultBuilder tkResultBuilder) {
return tkResultBuilder.build();
}
};
}
}