Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. public class Parsatron3000 {
  2.     public static Object parse(String input) {
  3.         List<Object> results = ...;
  4.         List<Exception> exceptions = ...;
  5.  
  6.         for (Type type : setOfTypes) {
  7.             Result result = type.parse(input);
  8.             if (result.exception == null) {
  9.                 results.add(result.success);
  10.             } else {
  11.                 exceptions.add(result.exception);
  12.             }
  13.         }
  14.  
  15.         if (result.isEmpty()) {
  16.             // throw "could not parse"-exception
  17.             // possibly print/log exceptions from exceptions list
  18.         }
  19.         if (result.size() > 1) {
  20.             // throw "ambiguous input"-exception
  21.         }
  22.         return result.get(0);
  23.     }
  24.  
  25.     public static class Result {
  26.         public final Object success;
  27.         public final Exception exception;
  28.  
  29.         public Result(Object o) {
  30.             success = o;
  31.             exception = null;
  32.         }
  33.  
  34.         public Result(Exception e) {
  35.             success = null;
  36.             exception = e;
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement