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

The following examples show how to use com.google.common.base.Splitter#split() . These examples are extracted from open source projects. 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
private List<TextObject> partition() {
    List<TextObject> result = new ArrayList<>(this.k);
    if (!breakWords) {
        int maxLength = this.inputSample.length() / (this.k - 1);
        Pattern p = Pattern.compile("\\G\\s*(.{1," + maxLength + "})(?=\\s|$)", Pattern.DOTALL);
        Matcher m = p.matcher(this.inputSample);
        while (m.find())
            result.add(textObjectFactory.create().append(m.group(1)));
    } else {
        Splitter splitter = Splitter.fixedLength(this.k);
        for (String token : splitter.split(this.inputSample.toString())) {
            result.add(textObjectFactory.create().append(token));
        }
    }
    return result;
}
 
Example 2
Source Project: DataLink   File: CatMeta.java    License: Apache License 2.0 6 votes vote down vote up
public List<List<String>> processRequest(VoItf vo) {
    List<List<String>> results = Lists.newArrayList();

    final String resp = super.processRequest(vo, null);
    if (!StringUtils.isBlank(resp)) {

        if (resp.trim().startsWith("{")) {//正常响应不会是一个json串
            throw new RuntimeException("sth wrong:" + resp);
        }

        String[] rows = resp.split("\n");
        for (int i = 0; i < rows.length; i++) {
            List<String> eachRow = Lists.newArrayList();
            Splitter splitter = Splitter.on(" ").omitEmptyStrings().trimResults();
            Iterable<String> cols = splitter.split(rows[i]);
            Iterator<String> iterator = cols.iterator();
            while (iterator.hasNext()) {
                eachRow.add(iterator.next());
            }
            results.add(eachRow);
        }
    }
    return results;
}
 
Example 3
Source Project: DataLink   File: CatMeta.java    License: Apache License 2.0 6 votes vote down vote up
public List<List<String>> processRequest(VoItf vo) {
    List<List<String>> results = Lists.newArrayList();

    final String resp = super.processRequest(vo, null);
    if (!StringUtils.isBlank(resp)) {

        if (resp.trim().startsWith("{")) {//正常响应不会是一个json串
            throw new RuntimeException("sth wrong:" + resp);
        }

        String[] rows = resp.split("\n");
        for (int i = 0; i < rows.length; i++) {
            List<String> eachRow = Lists.newArrayList();
            Splitter splitter = Splitter.on(" ").omitEmptyStrings().trimResults();
            Iterable<String> cols = splitter.split(rows[i]);
            Iterator<String> iterator = cols.iterator();
            while (iterator.hasNext()) {
                eachRow.add(iterator.next());
            }
            results.add(eachRow);
        }
    }
    return results;
}
 
Example 4
public static void packProjectJars(SparkConf conf) throws IOException {
  String classPath = System.getProperty(JAVA_CLASS_PATH);
  String pathSeparator = System.getProperty(PATH_SEPARATOR);
  Splitter splitter = Splitter.on(pathSeparator);
  Iterable<String> split = splitter.split(classPath);
  List<String> list = toList(split);
  List<String> classPathThatNeedsToBeIncluded = removeSparkLibs(list);
  List<String> jars = new ArrayList<String>();
  for (String s : classPathThatNeedsToBeIncluded) {
    if (isJarFile(s)) {
      jars.add(s);
    } else {
      jars.add(createJar(s));
    }
  }
  conf.setJars(jars.toArray(new String[jars.size()]));
}
 
Example 5
/**
 * 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 6
Source Project: log-synth   File: StreetNameSampler.java    License: 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 7
Source Project: vespa   File: CompoundNameTestCase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public final void testSize() {
    Splitter s = Splitter.on('.');
    Iterable<String> i = s.split(NAME);
    int n = 0;
    for (@SuppressWarnings("unused") String x : i) {
        ++n;
    }
    assertEquals(n, cn.size());
}
 
Example 8
@Override
public XmlStringBuilder toXml(Element e) {
  XmlStringBuilder res = XmlStringBuilder.createEmpty();
  String value = e.getProperty("value");
  if (isEmptyOrWhitespace(value)) {
    res.append(XmlStringBuilder.createEmpty().wrap(LineContainers.LINE_TAGNAME));
  } else {
    Splitter splitter = Splitter.on("\n");
    for (String paragraph : splitter.split(value)) {
      res.append(XmlStringBuilder.createEmpty().wrap(LineContainers.LINE_TAGNAME));
      res.append(XmlStringBuilder.createText(paragraph));
    }
  }
  return res.wrap("textarea", "name", e.getProperty("name"));
}
 
Example 9
Source Project: bazel   File: SandboxModule.java    License: Apache License 2.0 5 votes vote down vote up
private static Path getPathToDockerClient(CommandEnvironment cmdEnv) {
  String path = cmdEnv.getClientEnv().getOrDefault("PATH", "");

  // TODO(philwo): Does this return the correct result if one of the elements intentionally ends
  // in white space?
  Splitter pathSplitter =
      Splitter.on(OS.getCurrent() == OS.WINDOWS ? ';' : ':').trimResults().omitEmptyStrings();

  FileSystem fs = cmdEnv.getRuntime().getFileSystem();

  for (String pathElement : pathSplitter.split(path)) {
    // Sometimes the PATH contains the non-absolute entry "." - this resolves it against the
    // current working directory.
    pathElement = new File(pathElement).getAbsolutePath();
    try {
      for (Path dentry : fs.getPath(pathElement).getDirectoryEntries()) {
        if (dentry.getBaseName().replace(".exe", "").equals("docker")) {
          return dentry;
        }
      }
    } catch (IOException e) {
      continue;
    }
  }

  return null;
}
 
Example 10
Source Project: swellrt   File: ElementSerializer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public XmlStringBuilder toXml(Element e) {
  XmlStringBuilder res = XmlStringBuilder.createEmpty();
  String value = e.getProperty("value");
  if (isEmptyOrWhitespace(value)) {
    res.append(XmlStringBuilder.createEmpty().wrap(LineContainers.LINE_TAGNAME));
  } else {
    Splitter splitter = Splitter.on("\n");
    for (String paragraph : splitter.split(value)) {
      res.append(XmlStringBuilder.createEmpty().wrap(LineContainers.LINE_TAGNAME));
      res.append(XmlStringBuilder.createText(paragraph));
    }
  }
  return res.wrap("textarea", "name", e.getProperty("name"));
}
 
Example 11
private static Table<Long, Long, BigDecimal> parsePerformanceTable() throws IOException {
	Table<Long, Long, BigDecimal> policyConcurrencyValueTable = TreeBasedTable.create();
	List<String> lines = Files.readLines(outputFile(), Charsets.UTF_8);
	Splitter splitter = Splitter.on(";");
	// the 0th. line contains the header, skipping that.
	for (int i = 1; i < lines.size(); i++) {
		Iterable<String> values = splitter.split(lines.get(i));
		Long policies = Long.valueOf(get(values, 0));
		Long concurrency = Long.valueOf(get(values, 1));
		BigDecimal averageValue = new BigDecimal(get(values, 2));
		policyConcurrencyValueTable.put(policies, concurrency, averageValue);
	}
	return policyConcurrencyValueTable;
}
 
Example 12
private static void populateMultimap(SetMultimap<Pattern, Pattern> multimap, String list) throws IOException {
  Splitter tokenSplitter = Splitter.on(",").omitEmptyStrings().trimResults();
  Splitter partSplitter = Splitter.on(".").omitEmptyStrings().trimResults();
  Splitter tableSplitter = Splitter.on("|").omitEmptyStrings().trimResults();

  for (String token : tokenSplitter.split(list)) {

    if (!Strings.isNullOrEmpty(token)) {
      List<String> parts = partSplitter.splitToList(token);
      if (parts.size() > 2) {
        throw new IOException("Invalid token " + token);
      }

      Pattern databasePattern = Pattern.compile(parts.get(0).replace("*", ".*"));
      Set<Pattern> tablePatterns = Sets.newHashSet();
      if (parts.size() == 2) {
        String tables = parts.get(1);
        for (String table : tableSplitter.split(tables)) {
          if (table.equals("*")) {
            // special case, must use ALL_TABLES due to use of set.contains(ALL_TABLES) in multimapContains
            tablePatterns.add(ALL_TABLES);
          } else {
            tablePatterns.add(Pattern.compile(table.replace("*", ".*")));
          }
        }
      } else {
        tablePatterns.add(ALL_TABLES);
      }
      multimap.putAll(databasePattern, tablePatterns);
    }
  }
}
 
Example 13
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 14
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 15
private String getTestJar() {
  String property = System.getProperty("java.class.path");
  Splitter splitter = Splitter.on(':');
  for (String s : splitter.split(property)) {
    if (s.endsWith(".jar")) {
      return s;
    }
  }
  throw new RuntimeException("No jars found?");
}
 
Example 16
private String pretty(String s) {
  Splitter splitter = Splitter.on('.');
  StringBuilder builder = new StringBuilder();
  for (String split : splitter.split(s)) {
    if (builder.length() == 0 && split.equals(BLUR)) {
      // skip
    } else {
      if (builder.length() != 0) {
        builder.append(' ');
      }
      builder.append(split.substring(0, 1).toUpperCase()).append(split.substring(1));
    }
  }
  return builder.toString();
}
 
Example 17
private List<TriggerBean> parseProp(String triggers) {
    Splitter splitter = Splitter.on('\n').omitEmptyStrings().trimResults();
    String lastTriggerId = null;
    TriggerBean triggerBean = null;
    List<TriggerBean> triggerBeans = Lists.newArrayList();

    for (String line : splitter.split(triggers)) {
        if (line.startsWith("#")) continue;

        int keyPos = indexOfAny(line, ':', '=');
        if (keyPos < 0) continue;

        String key = trim(substring(line, 0, keyPos));
        if (isBlank(key)) continue;

        String value = trim(substring(line, keyPos + 1));
        if (isBlank(value)) continue;

        int lastDotPos = key.lastIndexOf('.');
        if (lastDotPos < 0) continue;

        String triggerId = key.substring(0, lastDotPos);
        String property = key.substring(lastDotPos + 1);

        if (!triggerId.equalsIgnoreCase(lastTriggerId)) {
            lastTriggerId = triggerId;

            triggerBean = new TriggerBean();
            triggerBeans.add(triggerBean);
        }

        if ("name".equalsIgnoreCase(property)) {
            triggerBean.setName(value);
        } else if ("scheduler".equalsIgnoreCase(property)) {
            triggerBean.setScheduler(value);
        } else if ("jobClass".equalsIgnoreCase(property)) {
            triggerBean.setJobClass(value);
        } else if ("triggerDataMap".equalsIgnoreCase(property)) {
            triggerBean.setTriggerDataMap(value);
        } else if ("startDelay".equalsIgnoreCase(property)) {
            triggerBean.setStartDelay(Long.parseLong(value));
        }
    }

    return triggerBeans;
}
 
Example 18
@VisibleForTesting
@Nullable
static String getNamedDependency(@NonNull String expression) {
    //if (value.startsWith("group: 'com.android.support', name: 'support-v4', version: '21.0.+'"))
    if (expression.indexOf(',') != -1 && expression.contains("version:")) {
        String artifact = null;
        String group = null;
        String version = null;
        Splitter splitter = Splitter.on(',').omitEmptyStrings().trimResults();
        for (String property : splitter.split(expression)) {
            int colon = property.indexOf(':');
            if (colon == -1) {
                return null;
            }
            char quote = '\'';
            int valueStart = property.indexOf(quote, colon + 1);
            if (valueStart == -1) {
                quote = '"';
                valueStart = property.indexOf(quote, colon + 1);
            }
            if (valueStart == -1) {
                // For example, "transitive: false"
                continue;
            }
            valueStart++;
            int valueEnd = property.indexOf(quote, valueStart);
            if (valueEnd == -1) {
                return null;
            }
            String value = property.substring(valueStart, valueEnd);
            if (property.startsWith("group:")) {
                group = value;
            } else if (property.startsWith("name:")) {
                artifact = value;
            } else if (property.startsWith("version:")) {
                version = value;
            }
        }

        if (artifact != null && group != null && version != null) {
            return group + ':' + artifact + ':' + version;
        }
    }

    return null;
}
 
Example 19
Source Project: cuba   File: AppComponents.java    License: Apache License 2.0 4 votes vote down vote up
private void load(AppComponent component) {
    Document doc = getDescriptorDoc(component);

    String dependsOnAttr = doc.getRootElement().attributeValue("dependsOn");
    if (!StringUtils.isEmpty(dependsOnAttr)) {
        for (String depCompId : splitCommaSeparatedValue(dependsOnAttr)) {
            AppComponent depComp = get(depCompId);
            if (depComp == null) {
                depComp = new AppComponent(depCompId);
                load(depComp);
                components.add(depComp);
            }
            component.addDependency(depComp);
        }
    }

    for (Element moduleEl : doc.getRootElement().elements("module")) {
        String blocksAttr = moduleEl.attributeValue("blocks");
        if (StringUtils.isEmpty(blocksAttr)) {
            continue;
        }
        List<String> blocks = splitCommaSeparatedValue(blocksAttr);
        if (blocks.contains("*") || blocks.contains(block)) {
            for (Element propertyEl : moduleEl.elements("property")) {
                String name = propertyEl.attributeValue("name");
                String value = propertyEl.attributeValue("value");
                String existingValue = component.getProperty(name);

                if (value.startsWith("\\+")) {
                    if (existingValue != null) {
                        log.debug("Overwrite value of property {} from {} to {}", name, existingValue, value);
                    }

                    component.setProperty(name, value.substring(1), false);
                    continue;
                }

                if (!value.startsWith("+")) {
                    if (existingValue != null) {
                        log.debug("Overwrite value of property {} from {} to {}", name, existingValue, value);
                    }

                    component.setProperty(name, value, false);
                    continue;
                }

                String cleanValue = value.substring(1);

                if (existingValue != null) {
                    StringBuilder newValue = new StringBuilder(existingValue);
                    Splitter splitter = Splitter.on(AppProperties.SEPARATOR_PATTERN).omitEmptyStrings();
                    List<String> existingParts = splitter.splitToList(existingValue);
                    for (String part : splitter.split(cleanValue)) {
                        if (!existingParts.contains(part)) {
                            newValue.append(" ").append(part);
                        }
                    }
                    component.setProperty(name, newValue.toString(), true);
                } else {
                    component.setProperty(name, cleanValue, true);
                }
            }
        }
    }
}
 
Example 20
@Test
public void testGenerate() throws Exception {
  DateTime now = DateTime.now(DateTimeZone.UTC).withTimeAtStartOfDay();
  createTlds("tld", "com");

  ImmutableSet<InetAddress> ips =
      ImmutableSet.of(InetAddress.getByName("127.0.0.1"), InetAddress.getByName("::1"));
  HostResource host1 =
      persistResource(newHostResource("ns.foo.tld").asBuilder().addInetAddresses(ips).build());
  HostResource host2 =
      persistResource(newHostResource("ns.bar.tld").asBuilder().addInetAddresses(ips).build());

  ImmutableSet<VKey<HostResource>> nameservers =
      ImmutableSet.of(host1.createVKey(), host2.createVKey());
  // This domain will have glue records, because it has a subordinate host which is its own
  // nameserver. None of the other domains should have glue records, because their nameservers are
  // subordinate to different domains.
  persistResource(newDomainBase("bar.tld").asBuilder()
      .addNameservers(nameservers)
      .addSubordinateHost("ns.bar.tld")
      .build());
  persistResource(newDomainBase("foo.tld").asBuilder()
      .addSubordinateHost("ns.foo.tld")
      .build());
  persistResource(newDomainBase("ns-and-ds.tld").asBuilder()
      .addNameservers(nameservers)
      .setDsData(ImmutableSet.of(DelegationSignerData.create(1, 2, 3, new byte[] {0, 1, 2})))
      .build());
  persistResource(newDomainBase("ns-only.tld").asBuilder()
      .addNameservers(nameservers)
      .build());
  persistResource(newDomainBase("ns-only-client-hold.tld").asBuilder()
      .addNameservers(nameservers)
      .setStatusValues(ImmutableSet.of(StatusValue.CLIENT_HOLD))
      .build());
  persistResource(newDomainBase("ns-only-pending-delete.tld").asBuilder()
      .addNameservers(nameservers)
      .setStatusValues(ImmutableSet.of(StatusValue.PENDING_DELETE))
      .build());
  persistResource(newDomainBase("ns-only-server-hold.tld").asBuilder()
      .addNameservers(nameservers)
      .setStatusValues(ImmutableSet.of(StatusValue.SERVER_HOLD))
      .build());
  // These should be ignored; contacts aren't in DNS, hosts need to be from the same tld and have
  // IP addresses, and domains need to be from the same TLD and have hosts (even in the case where
  // domains contain DS data).
  persistResource(newDomainBase("ds-only.tld").asBuilder()
      .setDsData(ImmutableSet.of(DelegationSignerData.create(1, 2, 3, new byte[] {0, 1, 2})))
      .build());
  persistActiveContact("ignored_contact");
  persistActiveHost("ignored.host.tld");  // No ips.
  persistActiveDomain("ignored_domain.tld");  // No hosts or DS data.
  persistResource(newHostResource("ignored.foo.com").asBuilder().addInetAddresses(ips).build());
  persistResource(newDomainBase("ignored.com")
      .asBuilder()
      .addNameservers(nameservers)
      .setDsData(ImmutableSet.of(DelegationSignerData.create(1, 2, 3, new byte[] {0, 1, 2})))
      .build());

  GenerateZoneFilesAction action = new GenerateZoneFilesAction();
  action.mrRunner = makeDefaultRunner();
  action.bucket = "zonefiles-bucket";
  action.gcsBufferSize = 123;
  action.datastoreRetention = standardDays(29);
  action.dnsDefaultATtl = Duration.standardSeconds(11);
  action.dnsDefaultNsTtl = Duration.standardSeconds(222);
  action.dnsDefaultDsTtl = Duration.standardSeconds(3333);
  action.clock = new FakeClock(now.plusMinutes(2));  // Move past the actions' 2 minute check.

  Map<String, Object> response =
      action.handleJsonRequest(
          ImmutableMap.<String, Object>of("tlds", ImmutableList.of("tld"), "exportTime", now));
  assertThat(response)
      .containsEntry("filenames", ImmutableList.of("gs://zonefiles-bucket/tld-" + now + ".zone"));
  assertThat(response).containsKey("mapreduceConsoleLink");
  assertThat(response.get("mapreduceConsoleLink").toString())
      .startsWith(
          "Mapreduce console: https://backend-dot-projectid.appspot.com"
              + "/_ah/pipeline/status.html?root=");

  executeTasksUntilEmpty("mapreduce");

  GcsFilename gcsFilename =
      new GcsFilename("zonefiles-bucket", String.format("tld-%s.zone", now));
  String generatedFile = new String(readGcsFile(gcsService, gcsFilename), UTF_8);
  // The generated file contains spaces and tabs, but the golden file contains only spaces, as
  // files with literal tabs irritate our build tools.
  Splitter splitter = Splitter.on('\n').omitEmptyStrings();
  Iterable<String> generatedFileLines = splitter.split(generatedFile.replaceAll("\t", " "));
  Iterable<String> goldenFileLines = splitter.split(loadFile(getClass(), "tld.zone"));
  // The first line needs to be the same as the golden file.
  assertThat(generatedFileLines.iterator().next()).isEqualTo(goldenFileLines.iterator().next());
  // The remaining lines can be in any order.
  assertThat(generatedFileLines).containsExactlyElementsIn(goldenFileLines);
}