@Override public <B> Either<A, B> left(B right) { return Either.right(right); }
@Override public <B> Either<B, A> right(B left) { return Either.right(a); }
/** Parsing of <code>src</code> without namespaces. */ public static Either<Exception, Document> parse(InputSource src) { try { DocumentBuilder docBuilder = dbf.newDocumentBuilder(); return right(docBuilder.parse(src)); } catch (Exception e) { return left(e); } }
@Override public Option<B> apply(Either<A, B> either) { return either.right().toOption(); } };
@Override public Either<A, B> apply(Either<A, B> e) { return e.right().either(); } });
/** Flatten nested Eithers. */ public static <A, B> Either<A, B> flatten(Either<A, Either<A, B>> e) { return e.right().bind(new Function<Either<A, B>, Either<A, B>>() { @Override public Either<A, B> apply(Either<A, B> e) { return e.right().either(); } }); } }
/** Namespace aware parsing of <code>src</code>. */ public static Either<Exception, Document> parseNs(InputSource src) { try { DocumentBuilder docBuilder = nsDbf.newDocumentBuilder(); return right(docBuilder.parse(src)); } catch (Exception e) { return left(e); } }
@Override public Either<Exception, B> apply(A a) { try { return right(xapply(a)); } catch (Exception e) { return left(e); } } };
@Override public Either<Exception, AccessControlList> apply(String s) { try { return right(parseAcl(s)); } catch (Exception e) { return left(e); } } };
@Override public List<AccessControlEntry> apply(List<AccessControlEntry> sum, Either<AccessControlEntry, List<AccessControlEntry>> current) { if (current.isLeft()) sum.add(current.left().value()); else sum.addAll(current.right().value()); return sum; } });
/** Create a list of access control entries for a given role. */ public static Either<AccessControlEntry, List<AccessControlEntry>> entries(final String role, Tuple<String, Boolean>... actions) { final List<AccessControlEntry> entries = mlist(actions).map( new Function<Tuple<String, Boolean>, AccessControlEntry>() { @Override public AccessControlEntry apply(Tuple<String, Boolean> action) { return new AccessControlEntry(role, action.getA(), action.getB()); } }).value(); return right(entries); }
@Override public final Either<Err, B> apply(A a) { try { return right(xapply(a)); } catch (Exception e) { return left(toErr.apply(e)); } }
@Override public <A> Either<F, A> tx(Function<EntityManager, A> transactional) { try { return Either.right(penv.tx(transactional)); } catch (Exception e) { return Either.left(exHandler.apply(e)); } }
@Override public Either<C, B> apply(A a) { try { B b = f.apply(a); return right(b); } catch (Exception e) { C c = handler.apply(e); return left(c); } } };
/** * Load the SMIL document identified by <code>mpe</code>. Throws an exception if it does not exist or cannot be loaded * by any reason. * * @return the document */ public static Document loadSmilDocument(InputStream in, MediaPackageElement mpe) { try { Either<Exception, org.w3c.dom.Document> eitherDocument = XmlUtil.parseNs(new InputSource(in)); if (eitherDocument.isRight()) return eitherDocument.right().value(); throw eitherDocument.left().value(); } catch (Exception e) { logger.warn("Unable to load smil document from catalog '{}'", mpe, e); return Misc.chuck(e); } }
@Override public Either<Exception, A> run(Function<HttpResponse, A> f) { HttpResponse response = null; try { response = client.execute(req); return right(f.apply(response)); } catch (Exception e) { return left(e); } finally { if (response != null) { client.close(response); } } } };
@Override public Either<Exception, A> apply(Function<HttpResponse, A> responseHandler) { HttpResponse response = null; try { response = client.execute(httpUriRequest); return right(responseHandler.apply(response)); } catch (Exception e) { return left(e); } finally { if (response != null) { client.close(response); } } } };
/** Get the value or throw the exception. */ public static <A> A getOrError(Either<Exception, A> response) { if (response.isRight()) { return response.right().value(); } else { return chuck(response.left().value()); } }
/** * Handle a closeable resource inside <code>f</code> and ensure that <code>r</code> gets closed properly. * * @param r * resource creation function * @param toErr * error handler transforming an exception into something else * @param f * resource handler */ public static <A, Err, B extends Closeable> Either<Err, A> withResource(Function0<B> r, Function<Exception, Err> toErr, Function<B, A> f) { B b = null; try { b = r.apply(); return right(f.apply(b)); } catch (Exception e) { return left(toErr.apply(e)); } finally { IoSupport.closeQuietly(b); } }
public static String getContentOn(final RequestRunner<String> runner, final int... status) { final Either<Exception, String> res = runner.run(getContentOn(status)); if (res.isRight()) { return res.right().value(); } else { return chuck(res.left().value()); } }