com.google.common.base.CharMatcher Java Examples
The following examples show how to use
com.google.common.base.CharMatcher.
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: TextPoolGenerator.java From dremio-oss with Apache License 2.0 | 6 votes |
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; } }
Example #2
Source File: CharMatcherExample.java From levelup-java-examples with Apache License 2.0 | 6 votes |
@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); }
Example #3
Source File: IterablesExample.java From levelup-java-examples with Apache License 2.0 | 6 votes |
/** * 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); }
Example #4
Source File: SvnProctorStoreFactory.java From proctor with Apache License 2.0 | 6 votes |
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; }
Example #5
Source File: StatementGenerator.java From j2objc with Apache License 2.0 | 6 votes |
@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; }
Example #6
Source File: ApplicationSubmissionContextPBImpl.java From hadoop with Apache License 2.0 | 6 votes |
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); } } }
Example #7
Source File: PaymentPointer.java From quilt with Apache License 2.0 | 6 votes |
@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; }
Example #8
Source File: ClassPath.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
/** * 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); }
Example #9
Source File: StringMapFunctions.java From tablesaw with Apache License 2.0 | 6 votes |
/** * 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; }
Example #10
Source File: QuoteFilter.java From tac-kbp-eal with MIT License | 6 votes |
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); } }
Example #11
Source File: Attestation.java From android-testdpc with Apache License 2.0 | 6 votes |
@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(); }
Example #12
Source File: CountWordOccurrencesInFile.java From levelup-java-examples with Apache License 2.0 | 6 votes |
/** * 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()); }
Example #13
Source File: BaseEncoding.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
@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(); } }; }
Example #14
Source File: HardwareAddress.java From dhcp4j with Apache License 2.0 | 6 votes |
/** * 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); }
Example #15
Source File: PythonRunTestsStep.java From buck with Apache License 2.0 | 6 votes |
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()) + "$"; }
Example #16
Source File: QuestionLemmaDedowncaserDenormalizer.java From bioasq with Apache License 2.0 | 5 votes |
@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())); } }
Example #17
Source File: BaseEncoding.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
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(); }
Example #18
Source File: GuavaStringUnitTest.java From tutorials with MIT License | 5 votes |
@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); }
Example #19
Source File: ObjectIdFunctions.java From presto with Apache License 2.0 | 5 votes |
@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()); }
Example #20
Source File: OkHttpUtil.java From presto with Apache License 2.0 | 5 votes |
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()); }
Example #21
Source File: GuavaStringUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void whenTrimString_thenTrimmed() { final String input = "---hello,,,"; String result = CharMatcher.is('-').trimLeadingFrom(input); assertEquals("hello,,,", result); result = CharMatcher.is(',').trimTrailingFrom(input); assertEquals("---hello", result); result = CharMatcher.anyOf("-,").trimFrom(input); assertEquals("hello", result); }
Example #22
Source File: SourceBuilder.java From j2cl with Apache License 2.0 | 5 votes |
public void append(String source) { checkState(!finished); String indentedSource = source.replace(LINE_SEPARATOR, LINE_SEPARATOR + Strings.repeat(INDENT, currentIndentation)); sb.append(indentedSource); currentLine += CharMatcher.is(LINE_SEPARATOR_CHAR).countIn(indentedSource); currentColumn = sb.length() - sb.lastIndexOf(LINE_SEPARATOR) - 1; }
Example #23
Source File: BaseEncoding.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
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(); }
Example #24
Source File: ConfigFileJobCreationTest.java From helios with Apache License 2.0 | 5 votes |
@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()); }
Example #25
Source File: BuildMessageMemTableEventListener.java From qmq with Apache License 2.0 | 5 votes |
@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(); }
Example #26
Source File: CodePropertyEditor.java From onedev with MIT License | 5 votes |
@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; }
Example #27
Source File: DownloadStepdefs.java From james-project with Apache License 2.0 | 5 votes |
@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 + "\""); } }
Example #28
Source File: CharMatcherUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void whenCountingDigitsInString_ShouldReturnActualCountOfDigits() throws Exception { String inputPhoneNumber = "8 123 456 123"; int result = CharMatcher.digit().countIn(inputPhoneNumber); assertEquals(10, result); }
Example #29
Source File: Convert.java From binnavi with Apache License 2.0 | 5 votes |
/** * Tests whether a given string is a valid hexadecimal string. * * @param string The string to check. * * @return True, if the string is a valid hexadecimal string. False, otherwise. */ public static boolean isHexString(final String string) { Preconditions.checkNotNull(string, "Error: String argument can't be null"); final CharMatcher cm = CharMatcher.inRange('0', '9').or(CharMatcher.inRange('a', 'z')) .or(CharMatcher.inRange('A', 'F')); for (int i = 0; i < string.length(); i++) { if (!cm.apply(string.charAt(i))) { return false; } } return string.length() != 0; }
Example #30
Source File: LTrimRTrimUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void givenString_whenCallingGuavaCharMatcher_thenReturnsTrue() { // Use StringUtils containsIgnoreCase to avoid case insensitive issues String ltrim = CharMatcher.whitespace().trimLeadingFrom(src);; String rtrim = CharMatcher.whitespace().trimTrailingFrom(src); // Compare the Strings obtained and the expected Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim)); Assert.assertTrue(rtrimResult.equalsIgnoreCase(rtrim)); }