Example usage for com.google.common.base CharMatcher retainFrom

List of usage examples for com.google.common.base CharMatcher retainFrom

Introduction

In this page you can find the example usage for com.google.common.base CharMatcher retainFrom.

Prototype

@CheckReturnValue
public String retainFrom(CharSequence sequence) 

Source Link

Document

Returns a string containing all matching characters of a character sequence, in order.

Usage

From source file:org.openhealthtools.mdht.mdmi.util.DateUtil.java

/**
 * For outputting dates, tries to match the output precision with the input precision.
 * This has been tested with quoted and unquoted literals and supports varying expressions
 * of the same precision./*from w  w w. j  a va2 s. c  o  m*/
 * TODO: Differing expressions of precision (e.g., from day as digit to day as name)
 *  has not been tested because the engine has a bug in which it always passes the destination
 *  format string, not the source.
 *
 * @param outFormat The maximal precision output format.
 * @param originalFormat The orginalFormat of the date, from the date wrapper.
 * @return A truncation of outFormat to match the precision of the originalFormat
 */
public static String matchPrecision(String outFormat, String originalFormat) {
    // Check for nulls
    if (originalFormat == null || originalFormat.equals("") || originalFormat.equals("DATE"))
        return outFormat;

    // Use our static data and a string matcher to add all the weird precision overlaps
    originalFormat = originalFormat.replaceAll("'.*'", ""); // Remove quoted literals
    String newOF_src = outFormat.replaceAll("'.*'", "");
    StringBuilder newOF_trg = new StringBuilder();
    for (int i = 0; i < dateParts.length; i++) {
        if (dateParts[i].matchesAnyOf(originalFormat))
            newOF_trg.append(dateParts[i].retainFrom(newOF_src));
    }

    // Use Guava's charmatcher to deal with everything but single-quoted literals
    CharMatcher matcher = CharMatcher.anyOf(newOF_trg).or(CharMatcher.JAVA_LETTER.negate());
    // bugfix 2/9/15 - remove trailing non-format chars (e.g. :)
    if (!outFormat.contains("'"))
        return CharMatcher.JAVA_LETTER.negate().trimTrailingFrom(matcher.retainFrom(outFormat));
    else {
        // There are single-quoted literals. Yuck.
        StringBuilder newOut = new StringBuilder();
        boolean instring = false;
        for (int i = 0; i < outFormat.length(); i++) {
            char current = outFormat.charAt(i);
            if (current == '\'') {
                if (instring == false)
                    instring = true;
                else
                    instring = false;
            }
            if (instring || matcher.matches(current))
                newOut.append(current);
        }
        return newOut.toString();
    }
}

From source file:net.orpiske.sfs.filter.simple.CharacterFilter.java

@Override
public String filter(String source) {
    CharMatcher legalChars = CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z'));

    return legalChars.retainFrom(source.subSequence(0, source.length()));
}

From source file:net.orpiske.sfs.filter.simple.AlphaNumericFilter.java

@Override
public String filter(String source) {
    CharMatcher legalChars = CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z'))
            .or(CharMatcher.inRange('0', '9'));

    return legalChars.retainFrom(source.subSequence(0, source.length()));
}

From source file:com.google.dart.engine.services.internal.correction.CorrectionUtils.java

/**
 * @return the possible names for variable with initializer of the given {@link StringLiteral}.
 *///w w w . j a  v  a 2  s  .c  om
public static String[] getVariableNameSuggestions(String text, Set<String> excluded) {
    // filter out everything except of letters and white spaces
    {
        CharMatcher matcher = CharMatcher.JAVA_LETTER.or(CharMatcher.WHITESPACE);
        text = matcher.retainFrom(text);
    }
    // make single camel-case text
    {
        String[] words = StringUtils.split(text);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            if (i > 0) {
                word = StringUtils.capitalize(word);
            }
            sb.append(word);
        }
        text = sb.toString();
    }
    // split camel-case into separate suggested names
    Set<String> res = Sets.newLinkedHashSet();
    addAll(excluded, res, getVariableNameSuggestions(text));
    return res.toArray(new String[res.size()]);
}