Java Code Examples for com.google.common.base.Splitter#on()

The following examples show how to use com.google.common.base.Splitter#on() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StringMapFunctions.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
default StringColumn tokenizeAndSort(String separator) {
  StringColumn newColumn = StringColumn.create(name() + "[sorted]", this.size());

  for (int r = 0; r < size(); r++) {
    String value = getString(r);

    Splitter splitter = Splitter.on(separator);
    splitter = splitter.trimResults();
    splitter = splitter.omitEmptyStrings();
    List<String> tokens = new ArrayList<>(splitter.splitToList(value));
    Collections.sort(tokens);
    value = String.join(separator, tokens);
    newColumn.set(r, value);
  }
  return newColumn;
}
 
Example 2
Source File: VinSampler.java    From log-synth with Apache License 2.0 6 votes vote down vote up
private static SetMultimap<String, String> multiMapResource(String name) throws IOException {
    final Splitter onTab = Splitter.on("\t");

    //noinspection UnstableApiUsage
    return Resources.readLines(Resources.getResource(name), Charsets.UTF_8, new LineProcessor<SetMultimap<String, String>>() {
        final SetMultimap<String, String> r = HashMultimap.create();

        @Override
        public boolean processLine(String line) {
            Iterator<String> pieces = onTab.split(line).iterator();
            String key = pieces.next();
            r.put(key, pieces.next());
            return true;
        }

        @Override
        public SetMultimap<String, String> getResult() {
            return r;
        }
    });
}
 
Example 3
Source File: XmlAttribute.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Handles tools: namespace attributes presence in both documents.
 * @param higherPriority the higherPriority attribute
 */
private void handleBothToolsAttributePresent(
        XmlAttribute higherPriority) {

    // do not merge tools:node attributes, the higher priority one wins.
    if (getName().getLocalName().equals(NodeOperationType.NODE_LOCAL_NAME)) {
        return;
    }

    // everything else should be merged, duplicates should be eliminated.
    Splitter splitter = Splitter.on(',');
    ImmutableSet.Builder<String> targetValues = ImmutableSet.builder();
    targetValues.addAll(splitter.split(higherPriority.getValue()));
    targetValues.addAll(splitter.split(getValue()));
    higherPriority.getXml().setValue(Joiner.on(',').join(targetValues.build()));
}
 
Example 4
Source File: AbstractUnleashMojo.java    From unleash-maven-plugin with Eclipse Public License 1.0 6 votes vote down vote up
@MojoProduces
@Named("releaseArgs")
@MojoInject
private Properties getReleaseArgs() {
  Properties args = new Properties();
  Splitter splitter = Splitter.on('=');
  if (this.releaseArgs != null) {
    for (String arg : this.releaseArgs) {
      List<String> split = splitter.splitToList(arg);
      if (split.size() == 2) {
        args.put(split.get(0), split.get(1));
      } else {
        args.put(split.get(0), "true");
        getLog().info("Detected release argument without an explicit value. Assuming '" + split.get(0)
            + "' to be a boolean property and setting it to true.");
      }
    }
  }

  // Add default property indicating that the unleash plugin is triggering the build
  args.put("isUnleashBuild", "true");
  return args;
}
 
Example 5
Source File: XmlAttribute.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Handles tools: namespace attributes presence in both documents.
 * @param higherPriority the higherPriority attribute
 */
private void handleBothToolsAttributePresent(
        XmlAttribute higherPriority) {

    // do not merge tools:node attributes, the higher priority one wins.
    if (getName().getLocalName().equals(NodeOperationType.NODE_LOCAL_NAME)) {
        return;
    }

    // everything else should be merged, duplicates should be eliminated.
    Splitter splitter = Splitter.on(',');
    ImmutableSet.Builder<String> targetValues = ImmutableSet.builder();
    targetValues.addAll(splitter.split(higherPriority.getValue()));
    targetValues.addAll(splitter.split(getValue()));
    higherPriority.getXml().setValue(Joiner.on(',').join(targetValues.build()));
}
 
Example 6
Source File: BlurClientTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
private List<String> getList(String controllerConnectionStr) {
  Splitter splitter = Splitter.on(',');
  List<String> results = new ArrayList<String>();
  for (String s : splitter.split(controllerConnectionStr)) {
    results.add(s);
  }
  return results;
}
 
Example 7
Source File: TopKNestedAggregator.java    From realtime-analytics with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void enter(Object value) {

    Object[] params = (Object[]) value;

    if (m_counter == null) {
        m_capacity = (Integer) params[0];
        m_topElementCnt = (Integer) params[1];
        m_counter = new StreamSummary<Object>(m_capacity.intValue());
    }

    if (splitter == null) {
        if (params[3] instanceof Character) {
            splitter = Splitter.on((Character) params[3]);
        } else if (params[3] instanceof String) {
            splitter = Splitter.on((String) params[3]);
        }
    }
    if (params[2] instanceof String && splitter != null) {
        Iterable<String> it = splitter.split((String) params[2]);
        for (String nested : it) {
            m_counter.offer(nested);
        }
    } else {
        m_counter.offer(params[2]);
    }
}
 
Example 8
Source File: StringToArrayConverter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiate with the array value separator and data type.
 *
 * @param conn Phoenix connection to target database
 * @param separatorString string used to separate incoming array values in strings
 * @param elementDataType datatype of the elements of arrays to be created
 */
public StringToArrayConverter(Connection conn, String separatorString,
        PDataType elementDataType) {
    this.conn = conn;
    this.splitter = Splitter.on(separatorString);
    this.elementDataType = elementDataType;
    this.elementConvertFunction = new CsvUpsertExecutor.SimpleDatatypeConversionFunction(elementDataType, this.conn);
}
 
Example 9
Source File: StreetNameSampler.java    From log-synth with Apache License 2.0 5 votes vote down vote up
public StreetNameSampler() {
    Splitter onTabs = Splitter.on("\t");
    try {
        for (String line : Resources.readLines(Resources.getResource("street-name-seeds"), Charsets.UTF_8)) {
            if (!line.startsWith("#")) {
                Iterator<Multinomial<String>> i = sampler.iterator();
                for (String name : onTabs.split(line)) {
                    i.next().add(name, 1);
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Couldn't read built-in resource", e);
    }
}
 
Example 10
Source File: InfoParserTest.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void cmdLine() throws IOException {
    File cmdLineFile = new File("/proc/" + pid, "cmdline");
    Splitter splitter = Splitter.on("\0");
    Joiner joiner = Joiner.on(" ");
    System.out.println(joiner.join(splitter.splitToList(Files.toString(cmdLineFile, Charsets.UTF_8))));
}
 
Example 11
Source File: StreamUtil.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
private static String findFileInClassFileUri(URI uri) {
  String classPath = System.getProperty(JAVA_CLASS_PATH);
  String pathSeparator = System.getProperty(PATH_SEPARATOR);
  Splitter splitter = Splitter.on(pathSeparator);
  Iterable<String> split = splitter.split(classPath);
  String path = uri.getPath();
  for (String s : split) {
    if (path.startsWith(s)) {
      return new File(s).getAbsolutePath();
    }
  }
  throw new RuntimeException("Uri [" + uri + "] was not found on the classpath.");
}
 
Example 12
Source File: StringToArrayConverter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiate with the array value separator and data type.
 *
 * @param conn Phoenix connection to target database
 * @param separatorString string used to separate incoming array values in strings
 * @param elementDataType datatype of the elements of arrays to be created
 */
public StringToArrayConverter(Connection conn, String separatorString,
        PDataType elementDataType) {
    this.conn = conn;
    this.splitter = Splitter.on(separatorString);
    this.elementDataType = elementDataType;
    this.elementConvertFunction = new CsvUpsertExecutor.SimpleDatatypeConversionFunction(elementDataType, this.conn);
}
 
Example 13
Source File: AbstractResourceRepository.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the sorted list of regions used in the resources with the given language.
 * @param currentLanguage the current language the region must be associated with.
 */
@NonNull
public SortedSet<String> getRegions(@NonNull String currentLanguage) {
    SortedSet<String> set = new TreeSet<String>();

    // As an optimization we could just look for values since that's typically where
    // the languages are defined -- not on layouts, menus, etc -- especially if there
    // are no translations for it
    Set<String> qualifiers = Sets.newHashSet();
    synchronized (ITEM_MAP_LOCK) {
        for (ListMultimap<String, ResourceItem> map : getMap().values()) {
            for (ResourceItem item : map.values()) {
                qualifiers.add(item.getQualifiers());
            }
        }
    }

    Splitter splitter = Splitter.on('-');
    for (String s : qualifiers) {
        boolean rightLanguage = false;
        for (String qualifier : splitter.split(s)) {
            if (currentLanguage.equals(qualifier)) {
                rightLanguage = true;
            } else if (rightLanguage
                    && qualifier.length() == 3
                    && qualifier.charAt(0) == 'r'
                    && Character.isUpperCase(qualifier.charAt(1))
                    && Character.isUpperCase(qualifier.charAt(2))) {
                set.add(qualifier.substring(1));
            }
        }
    }

    return set;
}
 
Example 14
Source File: AbstractResourceRepository.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the sorted list of languages used in the resources.
 */
@NonNull
public SortedSet<String> getLanguages() {
    SortedSet<String> set = new TreeSet<String>();

    // As an optimization we could just look for values since that's typically where
    // the languages are defined -- not on layouts, menus, etc -- especially if there
    // are no translations for it
    Set<String> qualifiers = Sets.newHashSet();

    synchronized (ITEM_MAP_LOCK) {
        for (ListMultimap<String, ResourceItem> map : getMap().values()) {
            for (ResourceItem item : map.values()) {
                qualifiers.add(item.getQualifiers());
            }
        }
    }

    Splitter splitter = Splitter.on('-');
    for (String s : qualifiers) {
        for (String qualifier : splitter.split(s)) {
            if (qualifier.length() == 2 && Character.isLetter(qualifier.charAt(0))
                    && Character.isLetter(qualifier.charAt(1))) {
                set.add(qualifier);
            }
        }
    }

    return set;
}
 
Example 15
Source File: StratumConnection.java    From besu with Apache License 2.0 5 votes vote down vote up
void handleBuffer(final Buffer buffer) {
  LOG.trace("Buffer received {}", buffer);
  Splitter splitter = Splitter.on('\n');
  boolean firstMessage = false;
  String messagesString;
  try {
    messagesString = buffer.toString(StandardCharsets.UTF_8);
  } catch (IllegalArgumentException e) {
    LOG.debug("Invalid message with non UTF-8 characters: " + e.getMessage(), e);
    closeHandle.run();
    return;
  }
  Iterator<String> messages = splitter.split(messagesString).iterator();
  while (messages.hasNext()) {
    String message = messages.next();
    if (!firstMessage) {
      message = incompleteMessage + message;
      firstMessage = true;
    }
    if (!messages.hasNext()) {
      incompleteMessage = message;
    } else {
      LOG.trace("Dispatching message {}", message);
      handleMessage(message);
    }
  }
}
 
Example 16
Source File: SplitBuilder.java    From kite with Apache License 2.0 4 votes vote down vote up
public Split(CommandBuilder builder, Config config, Command parent, Command child, MorphlineContext context) {
  super(builder, config, parent, child, context);      
  this.inputFieldName = getConfigs().getString(config, "inputField");
  
  this.outputFieldName = getConfigs().getString(config, "outputField", null);
  this.outputFieldNames = getConfigs().getStringList(config, "outputFields", null);
  if (outputFieldName == null && outputFieldNames == null) {
    throw new MorphlineCompilationException("Either outputField or outputFields must be defined", config);
  }
  if (outputFieldName != null && outputFieldNames != null) {
    throw new MorphlineCompilationException("Must not define both outputField and outputFields at the same time", config);
  }
  
  String separator = getConfigs().getString(config, "separator");
  boolean isRegex = getConfigs().getBoolean(config, "isRegex", false);
  GrokDictionaries dict = new GrokDictionaries(config, getConfigs());
  Splitter currentSplitter;
  if (isRegex) {
    currentSplitter = Splitter.on(dict.compileExpression(separator).pattern());
  } else if (separator.length() == 1) {
    currentSplitter = Splitter.on(separator.charAt(0));
  } else {
    currentSplitter = Splitter.on(separator);
  }
  
  int limit = getConfigs().getInt(config, "limit", -1);
  if (limit > 0) {
    currentSplitter = currentSplitter.limit(limit);
  }
  
  this.addEmptyStrings = getConfigs().getBoolean(config, "addEmptyStrings", false);      
  if (outputFieldNames == null && !addEmptyStrings) {
    currentSplitter = currentSplitter.omitEmptyStrings();
  }
  
  if (getConfigs().getBoolean(config, "trim", true)) {
    currentSplitter = currentSplitter.trimResults();
  }
  
  this.splitter = currentSplitter;
  validateArguments();
}
 
Example 17
Source File: FileDataModel.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
/**
 * @param delimiterRegex If your data file don't use '\t' or ',' as delimiters, you can specify 
 * user own using regex pattern.
 * @throws IOException
 */
public FileDataModel(File dataFile, boolean transpose,
        long minReloadIntervalMS, String delimiterRegex) throws IOException {

    this.dataFile = Preconditions.checkNotNull(dataFile.getAbsoluteFile());
    if (!dataFile.exists() || dataFile.isDirectory()) {
        throw new FileNotFoundException(dataFile.toString());
    }
    Preconditions
            .checkArgument(dataFile.length() > 0L, "dataFile is empty");
    Preconditions.checkArgument(minReloadIntervalMS >= 0L,
            "minReloadIntervalMs must be non-negative");

    log.info("Creating FileDataModel for file {}", dataFile);

    this.lastModified = dataFile.lastModified();
    this.lastUpdateFileModified = readLastUpdateFileModified();

    FileLineIterator iterator = new FileLineIterator(dataFile, false);
    String firstLine = iterator.peek();
    while (firstLine.isEmpty() || firstLine.charAt(0) == COMMENT_CHAR) {
        iterator.next();
        firstLine = iterator.peek();
    }
    Closeables.close(iterator, true);

    if (delimiterRegex == null) {
        delimiter = determineDelimiter(firstLine);
        delimiterPattern = Splitter.on(delimiter);
    } else {
        delimiter = '\0';
        delimiterPattern = Splitter.onPattern(delimiterRegex);
        if (!delimiterPattern.split(firstLine).iterator().hasNext()) {
            throw new IllegalArgumentException(
                    "Did not find a delimiter(pattern) in first line");
        }
    }
    List<String> firstLineSplit = Lists.newArrayList();
    for (String token : delimiterPattern.split(firstLine)) {
        firstLineSplit.add(token);
    }
    // If preference value exists and isn't empty then the file is specifying pref values
    hasPrefValues = firstLineSplit.size() >= 3
            && !firstLineSplit.get(2).isEmpty();

    this.reloadLock = new ReentrantLock();
    this.transpose = transpose;
    this.minReloadIntervalMS = minReloadIntervalMS;

    reload();
}
 
Example 18
Source File: ServiceURI.java    From pulsar with Apache License 2.0 4 votes vote down vote up
/**
 * Create a service uri instance from a {@link URI} instance.
 *
 * @param uri {@link URI} instance
 * @return a service uri instance
 * @throws NullPointerException if {@code uriStr} is null
 * @throws IllegalArgumentException if the given string violates RFC&nbsp;2396
 */
public static ServiceURI create(URI uri) {
    checkNotNull(uri, "service uri instance is null");

    String serviceName;
    final String[] serviceInfos;
    String scheme = uri.getScheme();
    if (null != scheme) {
        scheme = scheme.toLowerCase();
        final String serviceSep = "+";
        String[] schemeParts = StringUtils.split(scheme, serviceSep);
        serviceName = schemeParts[0];
        serviceInfos = new String[schemeParts.length - 1];
        System.arraycopy(schemeParts, 1, serviceInfos, 0, serviceInfos.length);
    } else {
        serviceName = null;
        serviceInfos = new String[0];
    }

    String userAndHostInformation = uri.getAuthority();
    checkArgument(!Strings.isNullOrEmpty(userAndHostInformation),
        "authority component is missing in service uri : " + uri);

    String serviceUser;
    List<String> serviceHosts;
    int atIndex = userAndHostInformation.indexOf('@');
    Splitter splitter = Splitter.on(CharMatcher.anyOf(",;"));
    if (atIndex > 0) {
        serviceUser = userAndHostInformation.substring(0, atIndex);
        serviceHosts = splitter.splitToList(userAndHostInformation.substring(atIndex + 1));
    } else {
        serviceUser = null;
        serviceHosts = splitter.splitToList(userAndHostInformation);
    }
    serviceHosts = serviceHosts
        .stream()
        .map(host -> validateHostName(serviceName, serviceInfos, host))
        .collect(Collectors.toList());

    String servicePath = uri.getPath();
    checkArgument(null != servicePath,
        "service path component is missing in service uri : " + uri);

    return new ServiceURI(
        serviceName,
        serviceInfos,
        serviceUser,
        serviceHosts.toArray(new String[serviceHosts.size()]),
        servicePath,
        uri);
}
 
Example 19
Source File: PurchaseLog.java    From log-synth with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    Options opts = new Options();
    CmdLineParser parser = new CmdLineParser(opts);
    try {
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        System.err.println("Usage: -count <number>G|M|K [ -users number ]  log-file user-profiles");
        return;
    }

    Joiner withTab = Joiner.on("\t");

    // first generate lots of user definitions
    //noinspection UnstableApiUsage
    SchemaSampler users = new SchemaSampler(Resources.asCharSource(Resources.getResource("user-schema.txt"), Charsets.UTF_8).read());
    File userFile = File.createTempFile("user", "tsv");
    BufferedWriter out = Files.newBufferedWriter(userFile.toPath(), Charsets.UTF_8);
    for (int i = 0; i < opts.users; i++) {
        out.write(withTab.join(users.sample()));
        out.newLine();
    }
    out.close();

    // now generate a session for each user
    Splitter onTabs = Splitter.on("\t");
    Splitter onComma = Splitter.on(",");

    Random gen = new Random();
    //noinspection UnstableApiUsage
    SchemaSampler intermediate = new SchemaSampler(Resources.asCharSource(Resources.getResource("hit_step.txt"), Charsets.UTF_8).read());

    final int COUNTRY = Iterables.indexOf(users.getFieldNames(), "country"::equals);
    final int CAMPAIGN = Iterables.indexOf(intermediate.getFieldNames(), "campaign_list"::equals);
    final int SEARCH_TERMS = Iterables.indexOf(intermediate.getFieldNames(),"search_keywords"::equals);
    Preconditions.checkState(COUNTRY >= 0, "Need country field in user schema");
    Preconditions.checkState(CAMPAIGN >= 0, "Need campaign_list field in step schema");
    Preconditions.checkState(SEARCH_TERMS >= 0, "Need search_keywords field in step schema");

    out = Files.newBufferedWriter(new File(opts.out).toPath(), Charsets.UTF_8);

    for (String line : Files.readAllLines(userFile.toPath(), Charsets.UTF_8)) {
        long t = (long) (TimeUnit.MILLISECONDS.convert(30, TimeUnit.DAYS) * gen.nextDouble());
        List<String> user = Lists.newArrayList(onTabs.split(line));

        // pick session length
        int n = (int) Math.floor(-30 * Math.log(gen.nextDouble()));

        for (int i = 0; i < n; i++) {
            // time on page
            int dt = (int) Math.floor(-20000 * Math.log(gen.nextDouble()));
            t += dt;

            // hit specific values
            JsonNode step = intermediate.sample();

            // check for purchase
            double p = 0.01;
            List<String> campaigns = Lists.newArrayList(onComma.split(step.get("campaign_list").asText()));
            List<String> keywords = Lists.newArrayList(onComma.split(step.get("search_keywords").asText()));
            if ((user.get(COUNTRY).equals("us") && campaigns.contains("5")) ||
                    (user.get(COUNTRY).equals("jp") && campaigns.contains("7")) ||
                    keywords.contains("homer") || keywords.contains("simpson")) {
                p = 0.5;
            }

            String events = gen.nextDouble() < p ? "1" : "-";

            out.write(Long.toString(t));
            out.write("\t");
            out.write(line);
            out.write("\t");
            out.write(withTab.join(step));
            out.write("\t");
            out.write(events);
            out.write("\n");
        }
    }
    out.close();
}
 
Example 20
Source File: MoreStringUtil.java    From vjtools with Apache License 2.0 2 votes vote down vote up
/**
 * 使用多个可选的char作为分割符, 还可以设置omitEmptyStrings,trimResults等配置
 * 
 * 设置后的Splitter进行重用,不要每次创建
 * 
 * @param separatorChars 比如Unix/Windows的路径分割符 "/\\"
 * 
 * @see com.google.common.base.Splitter
 */
public static Splitter charsSplitter(final String separatorChars) {
	return Splitter.on(CharMatcher.anyOf(separatorChars));
}