/** returns true iff the target matches the given pattern, * under simplified bash rules -- viz permitting * and ? and comma delimited patterns inside curly braces, * as well as things like {1,2,5-10} (and also {01,02,05-10} to keep leading 0) * @throws InvalidPatternException */ public boolean isGlobMatchedNumeric(String globPattern, String targetText) throws InvalidPatternException { List<String> patterns = expand(globPattern); for (String p : patterns) { if (isNoBraceGlobMatched(p, targetText)) return true; } return false; }
/** as {@link #unwrapJavaString(String)} if the given string is wrapped in double quotes; * otherwise just returns the given string */ public static String unwrapJavaStringIfWrapped(String s) { if (!StringEscapes.isWrappedInDoubleQuotes(s)) return s; return unwrapJavaString(s); }
/** * @return null if var is null or empty string, otherwise return var */ public static String emptyToNull(String var) { if (isNonEmpty(var)) { return var; } else { return null; } }
private String getExpectedCredentials() { if (Strings.isEmpty(password)) { return username; } else { return username + ":" + password; } }
/** As {@link #chain(Collection)}, but explicitly using ( ) grouping characters * to ensure exits are caught. */ public static String chainSubshell(Collection<String> commands) { // the spaces are not required, but it might be possible that a (( expr )) is interpreted differently // (won't hurt to have the spaces in any case!) return "( " + Strings.join(commands, " && ") + " )"; }
@Override public List<String> apply(final String input) { return JavaStringEscapes.unwrapJsonishListIfPossible(input); } });
@Test public void testNumLeadZero() throws InvalidPatternException { assertTrue(newGlobExpander().isGlobMatchedNumeric("a{01-03,05}", "a01")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{ 01 - 03 , 05 }", "a01")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{ 01 - 03 , 05 }", "a02")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{ 01 - 03 , 05 }", "a03")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{ 01 - 03 , 05 }", "a 05 ")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{01-03,05}", "a05")); assertFalse(newGlobExpander().isGlobMatchedNumeric("a{01-03,05}", "a04")); assertFalse(newGlobExpander().isGlobMatchedNumeric("a{01-03,05}", "a3")); }
/** as {@link #maxlenWithEllipsis(String, int, String) with "..." as the ellipsis */ public static String maxlenWithEllipsis(String s, int maxlen) { return maxlenWithEllipsis(s, maxlen, "..."); } /** as {@link #maxlenWithEllipsis(String, int) but replacing the last few chars with the given ellipsis */
/** * Format the {@literal size} bytes as a String. */ public String makeSizeString(long size) { return makeSizeString(size, precision); }
@Override public String getOsgiVersionString() { return version==null ? version : BrooklynVersionSyntax.toValidOsgiVersion(version); }
private void testResultingTokens(String input, String quoteChars, boolean includeQuotes, String delimiterChars, boolean includeDelimiters, String... expectedTokens) { QuotedStringTokenizer tok = new QuotedStringTokenizer(input, quoteChars, includeQuotes, delimiterChars, includeDelimiters); testResultingTokens(input, tok, expectedTokens); }
protected String makeBigString(int size) { // prefer non-random so can't be compressed return Identifiers.makeRandomBase64Id(size); // return com.google.common.base.Strings.repeat("x", size); } }
/** returns a list with no curly braces in any entries, * and guaranteeing order such that any {..,X,..,Y,..} will result in X being before Y in the resulting list; * e.g. given a{,b,c} gives a ab and ac; no special treatment of numeric ranges, quotes, or parentheses * (see SpecialistGlobExpander for that) */ public static List<String> getGlobsAfterBraceExpansion(String pattern) throws InvalidPatternException { return getGlobsAfterBraceExpansion(pattern, false, PhraseTreatment.NOT_A_SPECIAL_CHAR, PhraseTreatment.NOT_A_SPECIAL_CHAR); }
/** * For versions we currently work with any non-empty string that does not contain a ':' or whitespace. * However we discourage things that are not OSGi versions; see {@link #isValidOsgiVersion(String)}. * In some places (eg bundles) the use of OSGi version syntax may be enforced. */ public static boolean isUsableVersion(String candidate) { return isUsable(candidate); }
/** Creates a string satisfying {@link #isGoodBrooklynVersion(String)} based on the input. * For input satisfying {@link #isGoodBrooklynVersion(String)} the input will be returned unchanged. * For input satisfying {@link #isValidOsgiVersion(String)} the qualifier separator will be changed to "-", * and {@link #toValidOsgiVersion(String)} can be used to reverse the input (modulo insertion of "0"'s for minor/patch numbers if missing). * Precise behaviour for other input is not guaranteed but callers can expect output which resembles the input, * with any major/minor/patch string at the front preserved and internal contiguous alphanumeric sequences preserved. */ public static String toGoodBrooklynVersion(String input) { return toGoodVersion(input, "-", false); }
/** * Given e.g. "hello %s %s" returns a function which will insert an Iterable of two strings into that pattern * * @since 0.9.0 */ public static Function<Iterable<?>, String> formatterForIterable(final String pattern) { return new FormatterForIterableFunction(pattern); }
/** * Returns a sequence of chained commands that runs until one of them fails (i.e. joined by '&&') * This currently runs as a subshell (so exits are swallowed) but behaviour may be changed imminently. * (Use {@link #chainGroup(Collection)} or {@link #chainSubshell(Collection)} to be clear.) */ public static String chain(Collection<String> commands) { return "( " + Strings.join(commands, " && ") + " )"; }
@Test public void testNum() throws InvalidPatternException { assertTrue(newGlobExpander().isGlobMatchedNumeric("{1-3}", "1")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{1-3}", "a1")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{1-3,5}", "a1")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{1-3,5}", "a3")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{1-3,5}", "a5")); assertFalse(newGlobExpander().isGlobMatchedNumeric("a{1-3,5}", "a4")); assertFalse(newGlobExpander().isGlobMatchedNumeric("a{1-3,5}", "a01")); }
/** * Splits a comma-separated list of locations (names or specs) into an explicit list. * The splitting is very careful to handle commas embedded within specs, to split correctly. */ public static List<String> expandCommaSeparateLocations(String locations) { return WildcardGlobs.getGlobsAfterBraceExpansion("{"+locations+"}", false, PhraseTreatment.INTERIOR_NOT_EXPANDABLE, PhraseTreatment.INTERIOR_NOT_EXPANDABLE); // don't do this, it tries to expand commas inside parentheses which is not good! // QuotedStringTokenizer.builder().addDelimiterChars(",").buildList((String)id); }