Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Parsatron3000 {
- public static Object parse(String input) {
- List<Object> results = ...;
- List<Exception> exceptions = ...;
- for (Type type : setOfTypes) {
- Result result = type.parse(input);
- if (result.exception == null) {
- results.add(result.success);
- } else {
- exceptions.add(result.exception);
- }
- }
- if (result.isEmpty()) {
- // throw "could not parse"-exception
- // possibly print/log exceptions from exceptions list
- }
- if (result.size() > 1) {
- // throw "ambiguous input"-exception
- }
- return result.get(0);
- }
- public static class Result {
- public final Object success;
- public final Exception exception;
- public Result(Object o) {
- success = o;
- exception = null;
- }
- public Result(Exception e) {
- success = null;
- exception = e;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement