类com.google.common.base.CharMatcher源码实例Demo

下面列出了怎么用com.google.common.base.CharMatcher的API类实例代码及写法,或者点击链接到github查看源代码。

/**
 * Example was modified from the guava site to remove 
 * periods
 * 
 * @throws IOException
 */
@Test 
public void count_distinct_words_in_file_guava () throws IOException {

	File file = new File(sourceFileURI);
	
	Multiset<String> wordOccurrences = HashMultiset.create(
	  Splitter.on(CharMatcher.WHITESPACE)
	  	.trimResults(CharMatcher.is('.'))
	    .omitEmptyStrings()
	    .split(Files.asCharSource(file, Charsets.UTF_8).read()));

	
	logger.info(wordOccurrences);

	assertEquals(80, wordOccurrences.elementSet().size());
}
 
源代码2 项目: buck   文件: PythonRunTestsStep.java
private String getTestsToRunRegexFromListOutput(String listOutput) {
  ImmutableList.Builder<String> testsToRunPatternComponents = ImmutableList.builder();

  for (String strTestCase : CharMatcher.whitespace().trimFrom(listOutput).split("\n")) {
    String[] testCase = CharMatcher.whitespace().trimFrom(strTestCase).split("#", 2);
    if (testCase.length != 2) {
      throw new RuntimeException(
          String.format("Bad test case name from python runner: '%s'", strTestCase));
    }

    TestDescription testDescription = new TestDescription(testCase[0], testCase[1]);
    if (testSelectorList.isIncluded(testDescription)) {
      testsToRunPatternComponents.add(escapeForPythonRegex(strTestCase));
    }
  }

  return "^" + Joiner.on('|').join(testsToRunPatternComponents.build()) + "$";
}
 
源代码3 项目: codebuff   文件: ClassPath.java
/**
 * Returns the simple name of the underlying class as given in the source code.
 *
 * <p>Behaves identically to {@link Class#getSimpleName()} but does not require the class to be
 * loaded.
 */


public String getSimpleName() {
  int lastDollarSign = className.lastIndexOf('$');
  if (lastDollarSign != -1) {
    String innerClassName = className.substring(lastDollarSign + 1);
    // local and anonymous classes are prefixed with number (1,2,3...), anonymous classes are
    // entirely numeric whereas local classes have the user supplied name as a suffix
    return CharMatcher.digit().trimLeadingFrom(innerClassName);
  }
  String packageName = getPackageName();
  if (packageName.isEmpty()) {
    return className;
  }

  // Since this is a top level class, its simple name is always the part after package name.
  return className.substring(packageName.length() + 1);
}
 
源代码4 项目: levelup-java-examples   文件: IterablesExample.java
/**
 * Frequency of objects
 */
@Test
public void frequency_of_object_in_iterable () {
	
	String jingleChorus = "Oh, jingle bells, jingle bells "
			+ "Jingle all the way "
			+ "Oh, what fun it is to ride "
			+ "In a one horse open sleigh "
			+ "Jingle bells, jingle bells "
			+ "Jingle all the way "
			+ "Oh, what fun it is to ride "
			+ "In a one horse open sleigh";
	
	List<String> words = Splitter.on(CharMatcher.anyOf(" ."))
			.trimResults(CharMatcher.is('.'))
			.omitEmptyStrings()
			.splitToList(jingleChorus.toLowerCase());
	
	int numberOfOccurences = Iterables.frequency(words, "jingle");

	assertEquals(6, numberOfOccurences);
}
 
源代码5 项目: proctor   文件: SvnProctorStoreFactory.java
private File createTempDirectoryForPath(final String relativePath) {
    // replace "/" with "-" omit first "/" but omitEmptyStrings
    final String dirName = CharMatcher.is(File.separatorChar).trimAndCollapseFrom(relativePath, '-');
    final File parent = tempRoot != null ? tempRoot : implicitTempRoot;
    final File temp = new File(parent, dirName);
    if (temp.exists()) {
        if (!temp.isDirectory()) {
            throw new IllegalStateException(temp + " exists but is not a directory");
        }
    } else {
        if (!temp.mkdir()) {
            throw new IllegalStateException("Could not create directory : " + temp);
        }
    }
    return temp;
}
 
源代码6 项目: tablesaw   文件: StringMapFunctions.java
/**
 * Splits on Whitespace and returns the lexicographically sorted result.
 *
 * @return a {@link StringColumn}
 */
default StringColumn tokenizeAndSort() {
  StringColumn newColumn = StringColumn.create(name() + "[sorted]", this.size());

  for (int r = 0; r < size(); r++) {
    String value = getString(r);
    Splitter splitter = Splitter.on(CharMatcher.whitespace());
    splitter = splitter.trimResults();
    splitter = splitter.omitEmptyStrings();
    List<String> tokens = new ArrayList<>(splitter.splitToList(value));
    Collections.sort(tokens);
    value = String.join(" ", tokens);
    newColumn.set(r, value);
  }
  return newColumn;
}
 
源代码7 项目: tac-kbp-eal   文件: QuoteFilter.java
private QuoteFilter(Map<Symbol, ImmutableRangeSet<Integer>> docIdToBannedRegions) {
  this.docIdToBannedRegions = ImmutableMap.copyOf(docIdToBannedRegions);
  for (RangeSet<Integer> rs : docIdToBannedRegions.values()) {
    for (final Range<Integer> r : rs.asRanges()) {
      checkArgument(r.hasLowerBound());
      checkArgument(r.hasUpperBound());
      checkArgument(r.lowerEndpoint() >= 0);
    }
  }
  // these ensure we can serialize safely
  for (Symbol sym : docIdToBannedRegions.keySet()) {
    final String s = sym.toString();
    checkArgument(!s.isEmpty(), "Document IDs may not be empty");
    checkArgument(!CharMatcher.WHITESPACE.matchesAnyOf(s),
        "Document IDs may not contain whitespace: %s", s);
  }
}
 
源代码8 项目: codebuff   文件: BaseEncoding.java
@GwtIncompatible // Reader
static Reader ignoringReader(
  final Reader delegate, final CharMatcher toIgnore) {
  checkNotNull(delegate);
  checkNotNull(toIgnore);
  return new Reader() {
    @Override
    public int read() throws IOException {
      int readChar;
      do {
        readChar = delegate.read();
      } while (readChar != -1 && toIgnore.matches((char) readChar));
      return readChar;
    }

    @Override
    public int read(char[] cbuf, int off, int len) throws IOException {
      throw new UnsupportedOperationException();
    }

    @Override
    public void close() throws IOException {
      delegate.close();
    }
  };
}
 
源代码9 项目: dhcp4j   文件: HardwareAddress.java
/**
 * Parses a string representation of a hardware address.
 * Valid: 1/11:22:33:44:55:66 (toString())
 * Valid: Ethernet/11:22:33:44:55:66
 * Valid: 11:22:33:44:55:66 (defaults to Ethernet)
 *
 * @param text
 * @return HardwareAddress
 */
@Nonnull
public static HardwareAddress fromString(@Nonnull String text) {
    int idx = text.indexOf('/');
    HardwareAddressType hardwareAddressType = HardwareAddressType.Ethernet;
    if (idx != -1) {
        String hardwareAddressTypeText = text.substring(0, idx);
        try {
            int hardwareAddressTypeCode = Integer.parseInt(hardwareAddressTypeText);
            hardwareAddressType = HardwareAddressType.forCode(hardwareAddressTypeCode);
        } catch (NumberFormatException e) {
            // This will throw IllegalArgumentException, which is roughly what we want.
            hardwareAddressType = HardwareAddressType.valueOf(hardwareAddressTypeText);
        }
        text = text.substring(idx + 1);
    }

    CharMatcher separator = CharMatcher.BREAKING_WHITESPACE.or(CharMatcher.anyOf(":-"));
    Iterable<String> parts = Splitter.on(separator).omitEmptyStrings().trimResults().split(text);
    int i = 0;
    byte[] out = new byte[Iterables.size(parts)];
    for (String part : parts)
        out[i++] = (byte) Integer.parseInt(part, 16);
    return new HardwareAddress(hardwareAddressType.getCode(), (short) out.length, out);
}
 
源代码10 项目: android-testdpc   文件: Attestation.java
@Override
public String toString() {
    StringBuilder s = new StringBuilder();
    s.append("Attest version: " + attestationVersion);
    s.append("\nAttest security: " + securityLevelToString(attestationSecurityLevel));
    s.append("\nKM version: " + keymasterVersion);
    s.append("\nKM security: " + securityLevelToString(keymasterSecurityLevel));

    s.append("\nChallenge");
    String stringChallenge = new String(attestationChallenge);
    if (CharMatcher.ASCII.matchesAllOf(stringChallenge)) {
        s.append(": [" + stringChallenge + "]");
    } else {
        s.append(" (base64): [" + BaseEncoding.base64().encode(attestationChallenge) + "]");
    }
    if (uniqueId != null) {
        s.append("\nUnique ID (base64): [" + BaseEncoding.base64().encode(uniqueId) + "]");
    }

    s.append("\n-- SW enforced --");
    s.append(softwareEnforced);
    s.append("\n-- TEE enforced --");
    s.append(teeEnforced);

    return s.toString();
}
 
源代码11 项目: quilt   文件: PaymentPointer.java
@Check
AbstractPaymentPointer validate() {
  Preconditions.checkState(!host().isEmpty(), "PaymentPointers must specify a host");
  if (Strings.isNullOrEmpty(path())) {
    return ImmutablePaymentPointer.builder().from(this)
        .path(WELL_KNOWN)
        .build();
  }
  // Normalize acceptable input.
  if (path().equals("/")) {
    return ImmutablePaymentPointer.builder().from(this)
        .path(WELL_KNOWN)
        .build();
  }
  Preconditions.checkState(path().startsWith("/"), "path must start with a forward-slash");
  Preconditions.checkState(
      CharMatcher.ascii().matchesAllOf(toString()), "PaymentPointers may only contain ASCII characters");

  // Exclude the userinfo, port, query, and fragment in the URL.
  Preconditions.checkState(path().contains("://") == false, "PaymentPointers paths must not contain a scheme");

  return this;
}
 
源代码12 项目: j2objc   文件: StatementGenerator.java
@Override
public boolean visit(AssertStatement node) {
  buffer.append("JreAssert(");
  acceptMacroArgument(node.getExpression());
  buffer.append(", ");
  if (node.getMessage() != null) {
    acceptMacroArgument(node.getMessage());
  } else {
    int startPos = node.getStartPosition();
    String assertStatementString =
        unit.getSource().substring(startPos, startPos + node.getLength());
    assertStatementString = CharMatcher.whitespace().trimFrom(assertStatementString);
    // Generates the following string:
    // filename.java:456 condition failed: foobar != fish.
    String msg = TreeUtil.getSourceFileName(unit) + ":" + node.getLineNumber()
        + " condition failed: " + assertStatementString;
    buffer.append(LiteralGenerator.generateStringLiteral(msg));
  }
  buffer.append(");\n");
  return false;
}
 
源代码13 项目: levelup-java-examples   文件: CharMatcherExample.java
@Test
public void obtain_digits_from_telephone_number () {
	
	String telephoneNumber = CharMatcher
			.inRange('0','9')
			.retainFrom("123-456-7890");
	
	assertEquals("1234567890", telephoneNumber);
	
	// worried about performance
	CharMatcher digits = CharMatcher
			.inRange('0','9')
			.precomputed();
	
	String teleNumber = digits.retainFrom("123-456-7890");
	assertEquals("1234567890", teleNumber);
}
 
private void checkTags(Set<String> tags) {
  if (tags.size() > YarnConfiguration.APPLICATION_MAX_TAGS) {
    throw new IllegalArgumentException("Too many applicationTags, a maximum of only "
        + YarnConfiguration.APPLICATION_MAX_TAGS + " are allowed!");
  }
  for (String tag : tags) {
    if (tag.length() > YarnConfiguration.APPLICATION_MAX_TAG_LENGTH) {
      throw new IllegalArgumentException("Tag " + tag + " is too long, " +
          "maximum allowed length of a tag is " +
          YarnConfiguration.APPLICATION_MAX_TAG_LENGTH);
    }
    if (!CharMatcher.ASCII.matchesAllOf(tag)) {
      throw new IllegalArgumentException("A tag can only have ASCII " +
          "characters! Invalid tag - " + tag);
    }
  }
}
 
源代码15 项目: dremio-oss   文件: TextPoolGenerator.java
private ParsedDistribution(Distribution distribution) {
  parsedDistribution = new char[distribution.size()][];
  bonusText = new String[distribution.size()];
  for (int i = 0; i < distribution.size(); i++) {

    List<String> tokens = Splitter.on(CharMatcher.whitespace()).splitToList(distribution.getValue(i));

    parsedDistribution[i] = new char[tokens.size()];
    for (int j = 0; j < parsedDistribution[i].length; j++) {
      String token = tokens.get(j);
      parsedDistribution[i][j] = token.charAt(0);
      bonusText[i] = token.substring(1);
    }
  }

  randomTable = new int[distribution.getWeight(distribution.size() - 1)];
  int valueIndex = 0;
  for (int i = 0; i < randomTable.length; i++) {
    if (i >= distribution.getWeight(valueIndex)) {
      valueIndex++;
    }
    randomTable[i] = valueIndex;
  }

}
 
源代码16 项目: buck   文件: Escaper.java
/**
 * Escapes the special characters identified the {@link CharMatcher}, using single quotes.
 *
 * @param matcher identifies characters to be escaped
 * @param str string to quote
 * @return possibly quoted string
 */
public static String escape(Quoter quoter, CharMatcher matcher, String str) {
  if (matcher.matchesAnyOf(str) || str.isEmpty()) {
    return quoter.quote(str);
  } else {
    return str;
  }
}
 
源代码17 项目: tutorials   文件: GuavaStringUnitTest.java
@Test
public void whenCollapseFromString_thenCollapsed() {
    final String input = "       hel    lo      ";

    String result = CharMatcher.is(' ').collapseFrom(input, '-');
    assertEquals("-hel-lo-", result);

    result = CharMatcher.is(' ').trimAndCollapseFrom(input, '-');
    assertEquals("hel-lo", result);
}
 
@Override
public void process(JCas jcas) throws AnalysisEngineProcessException {
  List<Token> tokens = TypeUtil.getOrderedTokens(jcas);
  // use original ClearNLP lemmatizer in case missing
  tokens.stream().filter(token -> token.getLemmaForm() == null).forEach(token -> {
    DEPNode node = createNode(token);
    mpAnalyzer.analyze(node);
    token.setLemmaForm(node.getLemma());
  } );
  // try to de-downcase for proper nouns
  tokens.stream().filter(token -> equalsPosTag("NNP", token))
          .forEach(QuestionLemmaDedowncaserDenormalizer::setLemmaByText);
  tokens.stream().filter(token -> equalsPosTag("NNPS", token)).forEach(token -> {
    char[] tokenText = token.getCoveredText().toCharArray();
    char[] lemma = token.getLemmaForm().toCharArray();
    for (int i = 0; i < lemma.length; i++) {
      if (isUpperCase(tokenText[i]))
        lemma[i] = toUpperCase(lemma[i]);
    }
    token.setLemmaForm(new String(lemma));
  } );
  // de-normalization
  tokens.stream().filter(token -> equalsPosTag("CD", token))
          .forEach(QuestionLemmaDedowncaserDenormalizer::setLemmaByText);
  tokens.stream().filter(token -> CharMatcher.JAVA_DIGIT.matchesAnyOf(token.getCoveredText()))
          .forEach(QuestionLemmaDedowncaserDenormalizer::setLemmaByText);

  if (LOG.isTraceEnabled()) {
    tokens.forEach(token -> LOG.trace("{} {} {}", token.getCoveredText(), token.getLemmaForm(),
            token.getPartOfSpeech()));
  }
}
 
源代码19 项目: presto   文件: ObjectIdFunctions.java
@Description("Mongodb ObjectId from the given string")
@ScalarFunction
@SqlType("ObjectId")
public static Slice objectid(@SqlType(StandardTypes.VARCHAR) Slice value)
{
    return Slices.wrappedBuffer(new ObjectId(CharMatcher.is(' ').removeFrom(value.toStringUtf8())).toByteArray());
}
 
源代码20 项目: presto   文件: OkHttpUtil.java
public static Interceptor tokenAuth(String accessToken)
{
    requireNonNull(accessToken, "accessToken is null");
    checkArgument(CharMatcher.inRange((char) 33, (char) 126).matchesAllOf(accessToken));

    return chain -> chain.proceed(chain.request().newBuilder()
            .addHeader(AUTHORIZATION, "Bearer " + accessToken)
            .build());
}
 
源代码21 项目: tutorials   文件: GuavaStringUnitTest.java
@Test
public void whenRemoveCharsNotInCharset_thenRemoved() {
    final Charset charset = Charset.forName("cp437");
    final CharsetEncoder encoder = charset.newEncoder();

    final Predicate<Character> inRange = new Predicate<Character>() {
        @Override
        public boolean apply(final Character c) {
            return encoder.canEncode(c);
        }
    };

    final String result = CharMatcher.forPredicate(inRange).retainFrom("helloは");
    assertEquals("hello", result);
}
 
源代码22 项目: compliance   文件: Base.java
public Base(String urlRoot, String path, T responseBuilder, WireTracker wireTracker) {
    this.wireTracker = wireTracker;
    // neither urlRoot nor path should have leading or trailing spaces.
    this.urlRoot = urlRoot.trim();

    // the path does not begin or end with a slash
    String tsPath = path.trim();
    this.path = CharMatcher.is('/').trimFrom(tsPath);

    log.debug("set urlRoot = " + this.urlRoot + " path = " + this.path + " merged = " +
            makeUrl(this.urlRoot, this.path));

    this.responseBuilder = responseBuilder;
}
 
源代码23 项目: levelup-java-examples   文件: IterablesExample.java
/**
 * Get element at specified position
 */
@Test
public void get_iterable_element_by_index () {
	
	String baconIpsum = "Bacon ipsum dolor sit "
			+ "amet tri-tip rump shoulder "
			+ "kielbasa strip steak";
	
	Iterable<String> chars = Splitter.on(CharMatcher.WHITESPACE)
			.split(baconIpsum);
	
	String elementAtPos5 = Iterables.get(chars, 5);
	
	assertEquals("tri-tip", elementAtPos5);
}
 
源代码24 项目: codebuff   文件: MediaType.java
char consumeCharacter(CharMatcher matcher) {
  checkState(hasMore());
  char c = previewChar();
  checkState(matcher.matches(c));
  position++;
  return c;
}
 
源代码25 项目: bazel   文件: StringModule.java
@StarlarkMethod(
    name = "istitle",
    doc =
        "Returns True if the string is in title case and it contains at least one character. "
            + "This means that every uppercase character must follow an uncased one (e.g. "
            + "whitespace) and every lowercase character must follow a cased one (e.g. "
            + "uppercase or lowercase).",
    parameters = {@Param(name = "self", type = String.class, doc = "This string.")})
public Boolean isTitle(String self) throws EvalException {
  if (self.isEmpty()) {
    return false;
  }
  // From the Python documentation: "uppercase characters may only follow uncased characters
  // and lowercase characters only cased ones".
  char[] data = self.toCharArray();
  CharMatcher matcher = CharMatcher.any();
  char leftMostCased = ' ';
  for (int pos = data.length - 1; pos >= 0; --pos) {
    char current = data[pos];
    // 1. Check condition that was determined by the right neighbor.
    if (!matcher.matches(current)) {
      return false;
    }
    // 2. Determine condition for the left neighbor.
    if (LOWER.matches(current)) {
      matcher = CASED;
    } else if (UPPER.matches(current)) {
      matcher = CASED.negate();
    } else {
      matcher = CharMatcher.any();
    }
    // 3. Store character if it is cased.
    if (CASED.matches(current)) {
      leftMostCased = current;
    }
  }
  // The leftmost cased letter must be uppercase. If leftMostCased is not a cased letter here,
  // then the string doesn't have any cased letter, so UPPER.test will return false.
  return UPPER.matches(leftMostCased);
}
 
源代码26 项目: codebuff   文件: BaseEncoding.java
SeparatedBaseEncoding(BaseEncoding delegate, String separator, int afterEveryChars) {
  this.delegate = checkNotNull(delegate);
  this.separator = checkNotNull(separator);
  this.afterEveryChars = afterEveryChars;
  checkArgument(
      afterEveryChars > 0, "Cannot add a separator after every %s chars", afterEveryChars);
  this.separatorChars = CharMatcher.anyOf(separator).precomputed();
}
 
源代码27 项目: helios   文件: ConfigFileJobCreationTest.java
@Test
public void test() throws Exception {
  startDefaultMaster();

  final HeliosClient client = defaultClient();

  final String name = testJobName;
  final String version = "17";
  final String image = BUSYBOX;
  final Map<String, PortMapping> ports = ImmutableMap.of(
      "foo", PortMapping.of(4711),
      "bar", PortMapping.of(5000, externalPort),
      "p-tcp", PortMapping.of(1900, 2900, PortMapping.TCP),
      "p-udp", PortMapping.of(1900, 2900, PortMapping.UDP));
  final Map<ServiceEndpoint, ServicePorts> registration = ImmutableMap.of(
      ServiceEndpoint.of("foo-service", "tcp"), ServicePorts.of("foo"),
      ServiceEndpoint.of("bar-service", "http"), ServicePorts.of("bar"));
  final Map<String, String> env = ImmutableMap.of("BAD", "f00d");

  final Map<String, Object> configuration = ImmutableMap.of("id", name + ":" + version,
      "image", image,
      "ports", ports,
      "registration", registration,
      "env", env);

  final Path file = temporaryFolder.newFile().toPath();
  Files.write(file, Json.asBytes(configuration));

  final String output = cli("create", "-q", "-f", file.toAbsolutePath().toString());
  final JobId jobId = JobId.parse(CharMatcher.whitespace().trimFrom(output));

  final Map<JobId, Job> jobs = client.jobs().get();
  final Job job = jobs.get(jobId);

  assertEquals(name, job.getId().getName());
  assertEquals(version, job.getId().getVersion());
  assertEquals(ports, job.getPorts());
  assertEquals(env, job.getEnv());
  assertEquals(registration, job.getRegistration());
}
 
源代码28 项目: qmq   文件: BuildMessageMemTableEventListener.java
@Override
public void onEvent(final MessageLogRecord event) {
    if (CharMatcher.INVISIBLE.matchesAnyOf(event.getSubject())) {
        LOG.error("hit illegal subject during iterate message log, skip this message. subject: {}, wroteOffset: {}", event.getSubject(), event.getWroteOffset());
        return;
    }

    if (needRolling(event)) {
        final long nextTabletId = smt.getNextTabletId(tabletId);
        LOG.info("rolling new memtable, nextTabletId: {}, event: {}", nextTabletId, event);

        currentMemTable = (MessageMemTable) manager.rollingNewMemTable(nextTabletId, event.getWroteOffset());
        tabletId = nextTabletId;
    }

    if (currentMemTable == null) {
        throw new RuntimeException("lost first event of current log segment");
    }

    final long offset = event.getWroteOffset() + event.getWroteBytes();
    final Result<MessageMemTable.AddResultStatus, MessageMemTable.MessageIndex> result = currentMemTable.add(
            event.getSubject(),
            event.getSequence(),
            offset,
            event.getPayload());
    switch (result.getStatus()) {
        case SUCCESS:
            break;
        case OVERFLOW:
            throw new RuntimeException("memtable overflow");
        default:
            throw new RuntimeException("unknown status " + result.getStatus());
    }

    blockIfTooMuchActiveMemTable();
}
 
源代码29 项目: onedev   文件: CodePropertyEditor.java
@Override
protected List<String> convertInputToValue() throws ConversionException {
	List<String> convertedInput = new ArrayList<>();
	if (input.getConvertedInput() != null)
		convertedInput.addAll(Splitter.on("\n").trimResults(CharMatcher.is('\r')).splitToList(input.getConvertedInput()));
	return convertedInput;
}
 
源代码30 项目: james-project   文件: DownloadStepdefs.java
@Then("^the attachment is named \"([^\"]*)\"$")
public void assertContentDisposition(String name) {
    if (!CharMatcher.ascii().matchesAllOf(name)) {
        assertEncodedFilenameMatches(name);
    } else {
        assertThat(response.getFirstHeader("Content-Disposition").getValue()).isEqualTo("attachment; filename=\"" + name + "\"");
    }
}