com.relogiclabs.json.schema.tree.ExceptionRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of relogiclabs-json-schema Show documentation
Show all versions of relogiclabs-json-schema Show documentation
The New JSON Schema prioritizes simplicity, conciseness, and readability, making
it user-friendly and accessible without the need for extensive prior knowledge.
It offers efficient read-write facilities, precise JSON document definition
through various data types and functions, and extensibility to meet modern web
service diverse requirements.
package com.relogiclabs.json.schema.tree;
import lombok.Getter;
import lombok.Setter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Supplier;
public class ExceptionRegistry implements Iterable {
private int disableException;
private final List exceptions;
@Getter private final List tryBuffer;
@Getter @Setter private boolean throwException;
@Getter @Setter private int cutoffLimit = 500;
public ExceptionRegistry(boolean throwException) {
this.throwException = throwException;
this.exceptions = new LinkedList<>();
this.tryBuffer = new LinkedList<>();
}
private boolean addException(List list, Exception exception) {
if(list.size() <= cutoffLimit) list.add(exception);
return false;
}
public boolean failWith(RuntimeException exception) {
exception.fillInStackTrace();
if(disableException > 0) return addException(tryBuffer, exception);
if(throwException) throw exception;
return addException(exceptions, exception);
}
public T tryExecute(Supplier function) {
try {
disableException += 1;
return function.get();
} finally {
if(disableException >= 1) disableException -= 1;
else throw new IllegalStateException("Invalid runtime state");
}
}
public int getCount() {
return exceptions.size();
}
@Override
public Iterator iterator() {
return exceptions.iterator();
}
public void clear() {
exceptions.clear();
tryBuffer.clear();
}
}