public static String coerceStr(Object v, Pattern regex) { if (v == null) { throw new ArgumentException("Expected string, got null"); } String s = v.toString(); if (regex != null && !regex.matcher(s).matches()) { throw new ArgumentException("Expected string matching '" + regex + "', got '" + s + "'"); } return s; }
public static <T> T coerceObj(Object v, Class<T> clazz, String msg) { if (v == null) { throw new ArgumentException(msg + ": expected " + clazz.getName() + ", got null"); } if (!clazz.isInstance(v)) { throw new ArgumentException(msg + ": expected " + clazz.getName() + ", got " + v.getClass().getName()); } return (T)v; }
public static int coerceInt(Object v) { if (v == null) { throw new ArgumentException("Expected integer, got null."); } else if (v instanceof Number) { return ((Number)v).intValue(); } else if (v.getClass() == String.class) { try { return Integer.parseInt((String) v); } catch (NumberFormatException e) { throw new ArgumentException("Expected integer, got '" + v + "'"); } } throw new ArgumentException("Expected integer, got " + v.getClass().getName()); }
public static HttpStreamClient fromMap(Map<String,String> conf) { String url = conf.get("http.url"); Matcher m = RE_URL.matcher(url); if (!m.matches()) { throw new ArgumentException("Invalid URL: " + url); } HttpConfig httpConfig = new HttpConfig(); if ("https".equalsIgnoreCase(m.group(REG_URL_PROTO))) { httpConfig.setSslContext(TlsContextBuilder.fromMap("http.", conf)); } return new HttpStreamClient(httpConfig, url); }
val = me.val(); } else { throw new ArgumentException("Map element of invalid datatype: " + kv.getClass());