1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
package ${package}.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ValidationException extends RuntimeException {
private final List<ValidationException> children;
private String bullet = "";
private String currentMessage;
public ValidationException(final String message) {
this(message, (List<ValidationException>) null);
}
public ValidationException(final String message, final ValidationException child) {
this(message, Arrays.asList(child));
}
public ValidationException(final String message, final List<ValidationException> children_) {
super(message);
this.currentMessage = message;
final List<ValidationException> children = new ArrayList<ValidationException>();
if (children_ != null) {
for (final ValidationException child : children_) {
children.addAll(child.simplify());
}
}
this.children = children;
}
public ValidationException withBullet(final String bullet) {
this.bullet = bullet;
return this;
}
public List<ValidationException> simplify() {
if (getMessage().length() > 0) {
return Arrays.asList(this);
} else {
return this.children;
}
}
public String summary(final int level, final boolean withBullet) {
final int indentPerLevel = 2;
final String spaces = new String(new char[level * indentPerLevel]).replace("\0", " ");
final String bullet;
if (this.bullet.length() > 0 && withBullet) {
bullet = this.bullet;
} else {
bullet = "";
}
return spaces + bullet + this.currentMessage;
}
public String prettyStr(final Integer level_) {
Integer level = level_;
if (level == null) {
level = 0;
}
final List<String> parts = new ArrayList<String>();
int nextLevel;
if (this.currentMessage != null && this.currentMessage.length() > 0) {
parts.add(this.summary(level, true));
nextLevel = level + 1;
} else {
nextLevel = level;
}
for (final ValidationException child : this.children) {
parts.add(child.prettyStr(nextLevel));
}
final String ret = String.join("\n", parts);
return ret;
}
public String getMessage() {
return this.prettyStr(null);
}
}
|