/** * Returns <jk>true</jk> if specified string is not <jk>null</jk> or empty. * * @param s The string to check. * @return <jk>true</jk> if specified string is not <jk>null</jk> or empty. */ public static boolean isNotEmpty(String s) { return ! isEmpty(s); }
/** * Returns the first non-null, non-empty string in the list. * * @param s The strings to test. * @return The first non-empty string in the list, or <jk>null</jk> if they were all <jk>null</jk> or empty. */ public static String firstNonEmpty(String...s) { for (String ss : s) if (isNotEmpty(ss)) return ss; return null; }
/** * Converts a value to a String. * * @param o The value to convert. * @return The converted value, or <jk>null</jk> if the input was null. */ public static String toStringVal(Object o) { return StringUtils.asString(o); }
/** * Constructor. * * @param message Message to send as the response. * @param uri URI containing {@link MessageFormat}-style arguments. * @param uriArgs {@link MessageFormat}-style arguments. */ public SeeOther(String message, String uri, Object uriArgs) { this(message, toURI(format(uri.toString(), uriArgs))); }
final static Number toNumber(String s) { try { if (isNotEmpty(s)) return parseNumber(s, Number.class); return null; } catch (ParseException e) { throw new RuntimeException(e); } }
private String resolve(Object uri, UriResolution res) { String s = asString(uri); if (isAbsoluteUri(s)) return hasDotSegments(s) && res != NONE ? normalize(s) : s; if (res == ROOT_RELATIVE && startsWith(s, '/')) return hasDotSegments(s) ? normalize(s) : s; if (res == NONE && ! isSpecialUri(s)) return s; return append(new StringBuilder(), s).toString(); }
/** * Constructor. * * @param message The {@link MessageFormat}-style message. * @param args Optional {@link MessageFormat}-style arguments. */ public FormattedException(String message, Object...args) { super(format(message, args)); }
/** * Shortcut for calling <code>split(s, <js>','</js>)</code> * * @param s The string to split. Can be <jk>null</jk>. * @return The tokens, or <jk>null</jk> if the string was null. */ public static String[] split(String s) { return split(s, ','); }
@Override /* Object */ public String toString() { return StringUtils.join(typeRanges, ','); } }
/** * Tests two strings for non-equality, but gracefully handles nulls. * * @param s1 String 1. * @param s2 String 2. * @return <jk>true</jk> if the strings are not equal. */ public static boolean isNotEquals(String s1, String s2) { return ! isEquals(s1, s2); }
/** * Fluent setter. * * @param uri The URI of the request. * @return This object (for method chaining). */ @Override /* MockHttpRequest */ public MockServletRequest uri(String uri) { this.uri = emptyIfNull(uri); return this; }
@Override /* Parameter */ public String resolve(VarResolverSession session, String key) { return urlEncode(key); } }
final static Set<String> toSet(String s) { if (isEmpty(s)) return null; Set<String> set = new ASet<>(); try { for (Object o : StringUtils.parseListOrCdl(s)) set.add(o.toString()); } catch (ParseException e) { throw new RuntimeException(e); } return set; }
private static String message(Throwable cause, String msg, Object...args) { if (msg == null && cause != null) return firstNonEmpty(cause.getLocalizedMessage(), cause.getClass().getName()); return format(msg, args); }
/** * Constructor. * * @param restUrl The absolute URL of the REST interface backing the interface proxy. * @param m The Java method. */ public RemoteInterfaceMethod(final String restUrl, Method m) { this.method = m; this.path = m.getName() + '/' + HttpUtils.getMethodArgsSignature(m, true); this.url = trimSlashes(restUrl) + '/' + urlEncode(path); }
@Test public void testSplitEqually() { assertNull(null, splitEqually(null, 3)); assertEquals("", join(splitEqually("", 3), '|')); assertEquals("a", join(splitEqually("a", 3), '|')); assertEquals("ab", join(splitEqually("ab", 3), '|')); assertEquals("abc", join(splitEqually("abc", 3), '|')); assertEquals("abc|d", join(splitEqually("abcd", 3), '|')); }
public static final void assertContains(Exception e, String...substrings) { for (String substring : substrings) { Throwable e2 = e; boolean found = false; while (e2 != null && ! found) { found |= contains(e2.getMessage(), substring); e2 = e2.getCause(); } if (! found) { e.printStackTrace(); throw new ComparisonFailure("Exception message did not contain expected substring.", toString(substring), StringUtils.getStackTrace(e)); } } }
private String resolve(Object uri, UriResolution res) { String s = asString(uri); if (isAbsoluteUri(s)) return hasDotSegments(s) && res != NONE ? normalize(s) : s; if (res == ROOT_RELATIVE && startsWith(s, '/')) return hasDotSegments(s) ? normalize(s) : s; if (res == NONE && ! isSpecialUri(s)) return s; return append(new StringBuilder(), s).toString(); }