Java Code Examples for org.apache.commons.lang3.StringUtils#reverse()

The following examples show how to use org.apache.commons.lang3.StringUtils#reverse() . 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: ReverseFunction.java    From jtwig-core with Apache License 2.0 6 votes vote down vote up
@Override
public Object execute(FunctionRequest request) {
    request.minimumNumberOfArguments(1).maximumNumberOfArguments(1);

    Object input = request.get(0);
    Converter.Result<WrappedCollection> collectionResult = request.getEnvironment()
            .getValueEnvironment().getCollectionConverter()
            .convert(input);

    if (collectionResult.isDefined()) {
        return reverse(collectionResult.get().values());
    } else if (input instanceof String) {
        return StringUtils.reverse((String) input);
    } else {
        return input;
    }
}
 
Example 2
Source File: PersistentCache.java    From ari-proxy with GNU Affero General Public License v3.0 5 votes vote down vote up
protected Future<HealthReport> provideHealthReport(final String key) {
	final String testValue = StringUtils.reverse(key);

	return persistenceStore.set(key, testValue).flatMap(v -> persistenceStore.get(key)).transformValue(tryOfValue ->
			tryOfValue
					.map(maybeValue -> maybeValue.map(v -> testValue.equals(v)
							? Try.success(HealthReport.ok())
							: Try.success(HealthReport.error(String.format("RedisCheck: %s does not match expected %s", v, testValue)))
					)
					.getOrElse(() -> Try.success(HealthReport.error("RedisCheck: empty result on get()"))))
					.getOrElseGet(t -> Try.success(HealthReport.error("RedisCheck: " + t.getMessage()))));
}
 
Example 3
Source File: OrderAssembler.java    From microservice_coffeeshop with Apache License 2.0 5 votes vote down vote up
private String createOrderId(Long userId, String placeholder) {
    userId = userId == null ? 0 : userId;
    String user = StringUtils.reverse(String.format("%02d", Long.valueOf(userId.longValue() % 8194)));
    String random = String.valueOf(Integer.toHexString(secureRandom.nextInt(1000000) + 1234567));
    String time = formatter.format(ZonedDateTime.now(CHINA));
    String timeStr = Long.toHexString(Long.parseLong(time));
    StringBuilder sb = new StringBuilder();
    sb.append(user).append(timeStr).append(random).append(placeholder);
    return sb.toString();
}
 
Example 4
Source File: NumericEditText.java    From fingen with Apache License 2.0 5 votes vote down vote up
/**
 * Add grouping separators to string
 *
 * @param original original string, may already contains incorrect grouping separators
 * @return string with correct grouping separators
 */
private String format(final String original) {
    final String[] parts = original.split("\\" + mDecimalSeparator, -1);
    String number = parts[0] // since we split with limit -1 there will always be at least 1 part
            .replaceAll(mNumberFilterRegex, "")
            .replaceFirst(LEADING_ZERO_FILTER_REGEX, "");

    // only add grouping separators for non custom decimal separator
    if (!hasCustomDecimalSeparator) {
        // add grouping separators, need to reverse back and forth since Java regex does not support
        // right to left matching
        number = StringUtils.reverse(
                StringUtils.reverse(number).replaceAll("(.{3})", "$1" + GROUPING_SEPARATOR));
        // remove leading grouping separator if any
        number = StringUtils.removeStart(number, String.valueOf(GROUPING_SEPARATOR));
    }

    // add fraction part if any
    if (parts.length > 1) {
        if (parts[1].length() > 4) {
            number += mDecimalSeparator + parts[1].substring(0, 3);
        } else {
            number += mDecimalSeparator + parts[1];
        }
    }

    return number;
}
 
Example 5
Source File: ExampleRecordPreProcessor.java    From liiklus with MIT License 5 votes vote down vote up
@Override
public CompletionStage<Envelope> preProcess(Envelope envelope) {
    String value = StandardCharsets.UTF_8.decode(envelope.getValue()).toString();

    String reversed = StringUtils.reverse(value);
    return CompletableFuture.completedFuture(
            envelope.withValue(ByteBuffer.wrap(reversed.getBytes()))
    );
}
 
Example 6
Source File: CasRegistryNumber.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected Chemical create(JCas jCas, Matcher matcher) {
  // Check checksum
  Integer checkDigit = Integer.valueOf(matcher.group(3));

  String part1 = matcher.group(1);
  String part2 = matcher.group(2);

  part1 = StringUtils.reverse(part1);

  Integer sum =
      Integer.valueOf(part2.substring(1, 2)) + (2 * Integer.valueOf(part2.substring(0, 1)));
  Integer pos = 0;
  while (pos < part1.length()) {
    Integer x = Integer.valueOf(part1.substring(pos, pos + 1));
    sum += (pos + 3) * x;

    pos++;
  }

  if (sum % 10 != checkDigit) {
    getMonitor().debug("Pattern matching CAS format found, but check digit is incorrect");
    return null;
  }

  Chemical c = new Chemical(jCas);
  c.setSubType("CAS");
  return c;
}
 
Example 7
Source File: NumericEditText.java    From CalculatorInputView with Apache License 2.0 5 votes vote down vote up
/**
 * Add grouping separators to string
 *
 * @param original original string, may already contains incorrect grouping separators
 * @return string with correct grouping separators
 */
private String format(final String original) {
    final String[] parts = original.split("\\" + mDecimalSeparator, -1);
    String number = parts[0] // since we split with limit -1 there will always be at least 1 part
            .replaceAll(mNumberFilterRegex, "")
            .replaceFirst(LEADING_ZERO_FILTER_REGEX, "");

    // only add grouping separators for non custom decimal separator
    if (!hasCustomDecimalSeparator) {
        // add grouping separators, need to reverse back and forth since Java regex does not support
        // right to left matching
        number = StringUtils.reverse(
                StringUtils.reverse(number).replaceAll("(.{3})", "$1" + GROUPING_SEPARATOR));
        // remove leading grouping separator if any
        number = StringUtils.removeStart(number, String.valueOf(GROUPING_SEPARATOR));
    }

    // add fraction part if any
    if (parts.length > 1) {
        if (parts[1].length() > 2) {
            number += mDecimalSeparator + parts[1].substring(parts[1].length() - 2, parts[1].length());
        } else {
            number += mDecimalSeparator + parts[1];
        }
    }

    return number;
}
 
Example 8
Source File: Reverse.java    From vscrawler with Apache License 2.0 4 votes vote down vote up
@Override
protected String handleSingleStr(String input) {
    return StringUtils.reverse(input);
}
 
Example 9
Source File: ReverseStringExamples.java    From tutorials with MIT License 4 votes vote down vote up
public static String reverseUsingApacheCommons(String input) {
    return StringUtils.reverse(input);
}
 
Example 10
Source File: StringUtilsUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenString_whenReversingCharacters_thenCorrect() {
    String originalString = "baeldung";
    String reversedString = StringUtils.reverse(originalString);
    assertEquals("gnudleab", reversedString);
}
 
Example 11
Source File: ReverseAString.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void reverse_a_string_with_apache_commons() {

	String reversedString = StringUtils.reverse(PHRASE);

	assertEquals(PHRASE_REVERSE, reversedString);
}
 
Example 12
Source File: StringFunctions.java    From incubator-pinot with Apache License 2.0 2 votes vote down vote up
/**
 * @see StringBuilder#reverse()
 * @param input
 * @return reversed input in from end to start
 */
@ScalarFunction
static String reverse(String input) {
  return StringUtils.reverse(input);
}