Example usage for com.google.common.base Splitter split

List of usage examples for com.google.common.base Splitter split

Introduction

In this page you can find the example usage for com.google.common.base Splitter split.

Prototype

@CheckReturnValue
public Iterable<String> split(final CharSequence sequence) 

Source Link

Document

Splits sequence into string components and makes them available through an Iterator , which may be lazily evaluated.

Usage

From source file:org.apache.druid.java.util.common.parsers.ParserUtils.java

public static Function<String, Object> getMultiValueFunction(final String listDelimiter,
        final Splitter listSplitter) {
    return (input) -> {
        if (input != null && input.contains(listDelimiter)) {
            return StreamSupport.stream(listSplitter.split(input).spliterator(), false)
                    .map(NullHandling::emptyToNullIfNeeded).collect(Collectors.toList());
        } else {//from   ww w .ja  va 2s  .c o m
            return NullHandling.emptyToNullIfNeeded(input);
        }
    };
}

From source file:org.opendaylight.restconf.utils.parser.ParserIdentifier.java

/**
 * Make a {@link QName} from identifier/*from  w w  w  .ja v a2  s  .c  o  m*/
 *
 * @param identifier
 *            - path parameter
 * @return {@link QName}
 */
public static QName makeQNameFromIdentifier(final String identifier) {
    // check if more than one slash is not used as path separator
    if (identifier.contains(
            String.valueOf(RestconfConstants.SLASH).concat(String.valueOf(RestconfConstants.SLASH)))) {
        LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
        throw new RestconfDocumentedException(
                "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'",
                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
    }

    final int mountIndex = identifier.indexOf(RestconfConstants.MOUNT);
    String moduleNameAndRevision = "";
    if (mountIndex >= 0) {
        moduleNameAndRevision = identifier.substring(mountIndex + RestconfConstants.MOUNT.length())
                .replaceFirst(String.valueOf(RestconfConstants.SLASH), "");
    } else {
        moduleNameAndRevision = identifier;
    }

    final Splitter splitter = Splitter.on(RestconfConstants.SLASH);
    final Iterable<String> split = splitter.split(moduleNameAndRevision);
    final List<String> pathArgs = Lists.newArrayList(split);
    if (pathArgs.size() != 2) {
        LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
        throw new RestconfDocumentedException(
                "URI has bad format. End of URI should be in format \'moduleName/yyyy-MM-dd\'",
                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
    }

    try {
        final String moduleName = pathArgs.get(0);
        final String revision = pathArgs.get(1);
        final Date moduleRevision = RestconfConstants.REVISION_FORMAT.parse(revision);

        return QName.create(null, moduleRevision, moduleName);
    } catch (final ParseException e) {
        LOG.debug("URI has bad format. It should be \'moduleName/yyyy-MM-dd\' " + identifier);
        throw new RestconfDocumentedException("URI has bad format. It should be \'moduleName/yyyy-MM-dd\'",
                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
    }
}

From source file:org.asoem.greyfish.utils.math.ImmutableMarkovChain.java

public static ImmutableMarkovChain<String> parse(final String rule) {
    final ChainBuilder<String> builder = builder();

    final Splitter splitter = Splitter.onPattern("\r?\n|;").trimResults();
    final Iterable<String> lines = splitter.split(rule);

    final Pattern pattern = Pattern.compile("^(.+)->(.+):(.+)$");
    for (final String line : lines) {
        if (line.isEmpty()) {
            continue;
        }/*from   w w w. j  a  v a  2  s .c  om*/
        final Matcher matcher = pattern.matcher(line);
        if (matcher.matches()) {
            final String state1 = matcher.group(1).trim();
            final String state2 = matcher.group(2).trim();
            final String p = matcher.group(3).trim();

            builder.put(state1, state2, Double.parseDouble(p));
        } else {
            throw new IllegalArgumentException("Rule has errors at " + line);
        }
    }

    return builder.build();
}

From source file:org.eclipse.xtext.tasks.PreferenceTaskTagProvider.java

public static List<TaskTag> parseTags(final String names, final String priorities) {
    final Splitter splitter = Splitter.on(",").omitEmptyStrings().trimResults();
    final List<String> tags = IterableExtensions.<String>toList(splitter.split(names));
    final List<String> prios = IterableExtensions.<String>toList(splitter.split(priorities));
    final ArrayList<TaskTag> elements = CollectionLiterals.<TaskTag>newArrayList();
    int _size = tags.size();
    ExclusiveRange _doubleDotLessThan = new ExclusiveRange(0, _size, true);
    for (final Integer i : _doubleDotLessThan) {
        TaskTag _taskTag = new TaskTag();
        final Procedure1<TaskTag> _function = (TaskTag it) -> {
            it.setName(tags.get((i).intValue()));
            Priority _xifexpression = null;
            int _size_1 = prios.size();
            boolean _greaterEqualsThan = (_size_1 >= (i).intValue());
            if (_greaterEqualsThan) {
                Priority _xtrycatchfinallyexpression = null;
                try {
                    _xtrycatchfinallyexpression = Priority.valueOf(prios.get((i).intValue()));
                } catch (final Throwable _t) {
                    if (_t instanceof IllegalArgumentException) {
                        final IllegalArgumentException e = (IllegalArgumentException) _t;
                        _xtrycatchfinallyexpression = Priority.NORMAL;
                    } else {
                        throw Exceptions.sneakyThrow(_t);
                    }//from  ww  w. ja va2  s. co m
                }
                _xifexpression = _xtrycatchfinallyexpression;
            } else {
                _xifexpression = Priority.NORMAL;
            }
            it.setPriority(_xifexpression);
        };
        TaskTag _doubleArrow = ObjectExtensions.<TaskTag>operator_doubleArrow(_taskTag, _function);
        elements.add(_doubleArrow);
    }
    return elements;
}

From source file:io.prestosql.plugin.redis.RedisConnectorConfig.java

public static ImmutableSet<HostAddress> parseNodes(String nodes) {
    Splitter splitter = Splitter.on(',').omitEmptyStrings().trimResults();
    return ImmutableSet.copyOf(transform(splitter.split(nodes), RedisConnectorConfig::toHostAddress));
}

From source file:org.asoem.greyfish.core.utils.EvaluatingMarkovChain.java

public static EvaluatingMarkovChain<String> parse(final String rule, final ExpressionFactory factory) {
    checkNotNull(rule);/*from w w w.ja v  a  2  s . c  om*/
    checkNotNull(factory);

    final ChainBuilder<String> builder = builder(factory);

    final Splitter splitter = Splitter.onPattern("\r?\n|;").trimResults();
    final Iterable<String> lines = splitter.split(rule);

    for (final String line : lines) {
        if (line.isEmpty()) {
            continue;
        }
        final Matcher matcher = PATTERN.matcher(line);
        if (matcher.matches()) {
            final String state1 = matcher.group(1).trim();
            final String state2 = matcher.group(2).trim();
            final String p = matcher.group(3).trim();

            builder.put(state1, state2, p);
        } else {
            throw new IllegalArgumentException("Rule has errors at " + line);
        }
    }

    return builder.build();
}

From source file:hmi.animation.RenamingMap.java

public static BiMap<String, String> renamingMap(String renaming) {
    BiMap<String, String> renamingMap = HashBiMap.create();
    Splitter split = Splitter
            .on(CharMatcher.WHITESPACE/*from w w  w . j  ava 2s .  c om*/
                    .or(CharMatcher.is('\n').or(CharMatcher.is('\r').or(CharMatcher.is('\f')))))
            .trimResults().omitEmptyStrings();
    String[] splittedStr = Iterables.toArray(split.split(renaming), String.class);
    if (splittedStr.length % 2 > 0) {
        throw new IllegalArgumentException("renaming string does not contain an even amount of names");
    }
    for (int i = 0; i < splittedStr.length / 2; i++) {
        renamingMap.put(splittedStr[i * 2], splittedStr[i * 2 + 1]);
    }
    return renamingMap;
}

From source file:com.mattc.argus2.io.Configs.java

/**
 * Format a String of either a single line or multiple lines into <br />
 * comment format, similar to the Comment Format used in
 * {@link java.util.Properties}. <br />
 * /*from   w  w  w  .j a  v  a 2s.  c o  m*/
 * @param comment
 *            Unformatted or Formatted Comment Text
 * @param includeTime
 *            If true, print the current time as a separate comment line.
 * @return Comment Text formatted properly
 */
public static String formatComments(String comment, boolean includeTime) {
    if (includeTime) {
        comment += String.format("%n%n%s", new DateTime().toString(dtFormatter));
    }

    final Splitter splitter = Splitter.onPattern("\r?\n").trimResults();
    final StringBuilder sb = new StringBuilder();
    Iterable<String> iterable = splitter.split(comment);

    iterable = Iterables.transform(iterable, new Function<String, String>() {
        @Override
        public String apply(String input) {
            if (input.startsWith("#"))
                return input;
            else if (input.isEmpty())
                return "#";
            else
                return "# " + input;
        }
    });

    for (final String s : iterable) {
        sb.append(s).append(System.getProperty("line.separator"));
    }

    sb.append(System.getProperty("line.separator"));
    return sb.toString();
}

From source file:org.opendaylight.ovsdb.openstack.netvirt.it.SouthboundMapper.java

public static DatapathId createDatapathId(String dpid) {
    Preconditions.checkNotNull(dpid);/*from ww  w.  j ava2s  .com*/
    DatapathId datapath;
    if (dpid.matches("^[0-9a-fA-F]{16}")) {
        Splitter splitter = Splitter.fixedLength(2);
        Joiner joiner = Joiner.on(":");
        datapath = new DatapathId(joiner.join(splitter.split(dpid)));
    } else {
        datapath = new DatapathId(dpid);
    }
    return datapath;
}

From source file:com.google.gct.login.GoogleLoginPrefs.java

/**
 * Retrieves the persistently stored list of users.
 * @return the stored list of users./*from w ww .  j av a  2s  .  c  om*/
 */
@NotNull
public static List<String> getStoredUsers() {
    Preferences prefs = getPrefs();
    String allUsersString = prefs.get(USERS, "");
    List<String> allUsers = new ArrayList<String>();
    if (allUsersString.isEmpty()) {
        return allUsers;
    }

    Splitter splitter = Splitter.on(DELIMITER).omitEmptyStrings();
    for (String aUser : splitter.split(allUsersString)) {
        allUsers.add(aUser);
    }
    return allUsers;
}