@Override public B apply(Throwable throwable) { return chuck(throwable); } };
@Override public A apply(Object o) { return cast(o, ev); } };
/** * Throw a checked exception like a RuntimeException removing any needs to declare a throws clause. * * This technique has been described by James Iry at * http://james-iry.blogspot.de/2010/08/on-removing-java-checked-exceptions-by.html */ public static <A> A chuck(Throwable t) { return Misc.<RuntimeException, A> castGeneric(t); }
/** URI constructor function without checked exceptions. */ public static URI uri(String uri) { try { return new URI(uri); } catch (Exception e) { return chuck(e); } }
@Override public A apply(Object o) { return cast(o, ev); } };
/** URL constructor function without checked exceptions. */ public static URL url(String url) { try { return new URL(url); } catch (Exception e) { return chuck(e); } }
/** * Get mandatory value of type <code>ev</code>. * * @return the requested value if it exists and has the required type * @deprecated */ public <A> A get(Class<A> ev, String key) { final Object v = json.get(key); if (v != null) { try { return cast(v, ev); } catch (ClassCastException e) { throw new RuntimeException(format("Key %s has not required type %s but %s", key, ev.getName(), v.getClass() .getName())); } } else { throw new RuntimeException(format("Key %s does not exist", key)); } }
/** Like {@link IOUtils#toString(java.net.URL, String)} but without checked exception. */ public static String readToString(URL url, String encoding) { try { return IOUtils.toString(url, encoding); } catch (IOException e) { return chuck(e); } }
/** * Get optional value of type <code>ev</code>. * * @return some if the value exists and has the required type, none otherwise * @deprecated */ public <A> Option<A> opt(Class<A> ev, String key) { final Object v = json.get(key); if (v != null) { try { return some(cast(v, ev)); } catch (ClassCastException e) { return none(); } } else { return none(); } }
/** URL constructor function without checked exceptions. */ public static URL url(String protocol, String host, int port) { try { return new URL(protocol, host, port, "/"); } catch (Exception e) { return chuck(e); } }
/** * URL constructor function without checked exceptions. * * @see URL */ public static URL url(URL context, String spec) { try { return new URL(context, spec); } catch (Exception e) { return chuck(e); } }
private static Map parse(String json) { try { return (Map) new JSONParser().parse(json); } catch (ParseException e) { return chuck(e); } }
/** * Creates an xml document root and returns it. * * @return the document */ public static Document newDocument() { try { return nsDbf.newDocumentBuilder().newDocument(); } catch (ParserConfigurationException e) { return chuck(e); } }
private JAXBContext init(String contextPath) { try { return JAXBContext.newInstance(contextPath, this.getClass().getClassLoader()); } catch (JAXBException e) { return chuck(e); } }
/** Create a new DOM document. */ private static Document newDocument() { final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); try { return docBuilderFactory.newDocumentBuilder().newDocument(); } catch (ParserConfigurationException e) { return chuck(e); } }
/** * Forward exception <code>t</code> directly or wrap it into an instance of <code>wrapper</code> * if it is not of the wrapper's type. Class <code>wrapper</code> needs a constructor taking just * a {@link Throwable}. */ public static <T extends Throwable, A> A forward(Throwable t, Class<T> wrapper) throws T { if (wrapper.isAssignableFrom(t.getClass())) { throw (T) t; } else { try { throw wrapper.getConstructor(Exception.class).newInstance(t); } catch (Exception e) { return chuck(e); } } } }
/** Throw <code>none</code> if none. */ public <T extends Throwable> Option<A> orError(Class<T> none) throws T { if (isSome()) return this; else { T t; try { t = none.newInstance(); } catch (InstantiationException e) { return chuck(new Error("Error creating exception", e)); } catch (IllegalAccessException e) { return chuck(new Error("Error creating exception", e)); } throw t; } }
/** Same like {@link #parseAcl(String)} but throws runtime exceptions in case of an error. */ public static AccessControlList parseAclSilent(String serializedForm) { try { return parseAcl(serializedForm); } catch (Exception e) { return chuck(e); } }
@Override public Date apply(String a) { final Date date = EncodingSchemeUtils.decodeDate(a); return date != null ? date : Misc.<Date>chuck(new RuntimeException(a + " does not conform to W3C-DTF encoding scheme.")); } });
/** Serializes a media package to a {@link Document} without any further processing. */ public static Document getAsXmlDocument(MediaPackage mp) { try { final Marshaller marshaller = MediaPackageImpl.context.createMarshaller(); final Document doc = newDocument(); marshaller.marshal(mp, doc); return doc; } catch (JAXBException e) { return chuck(e); } }