Java Code Examples for org.apache.commons.lang3.math.NumberUtils#createInteger()

The following examples show how to use org.apache.commons.lang3.math.NumberUtils#createInteger() . 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: ConfigValueParser.java    From Plan with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Integer compose(String fromValue) {
    if (NumberUtils.isParsable(fromValue)) {
        return NumberUtils.createInteger(fromValue);
    }
    return null;
}
 
Example 2
Source File: ReplaceFilter.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
  if (var == null) {
    return null;
  }
  if (args.length < 2) {
    throw new TemplateSyntaxException(
      interpreter,
      getName(),
      "requires 2 arguments (substring to replace, replacement string) or 3 arguments (substring to replace, replacement string, number of occurrences to replace)"
    );
  }

  String s = (String) var;
  String toReplace = args[0];
  String replaceWith = args[1];
  Integer count = null;

  if (args.length > 2) {
    count = NumberUtils.createInteger(args[2]);
  }

  if (count == null) {
    return StringUtils.replace(s, toReplace, replaceWith);
  } else {
    return StringUtils.replace(s, toReplace, replaceWith, count);
  }
}
 
Example 3
Source File: NumberUtilities.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
/**
 * Given an integer string, it checks if it's a valid integer (based on apaches NumberUtils.createInteger)
 *
 * @param integerStr the integer string to check
 * @return true if it's valid, otherwise false
 */
public static boolean isValidInt(@Nullable final String integerStr) {
    if (StringUtils.isBlank(integerStr)) {
        return false;
    }
    final String stripedInteger = StringUtils.strip(integerStr);
    try {
        NumberUtils.createInteger(stripedInteger);
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}
 
Example 4
Source File: NumberUtilities.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
/**
 * Given an integer string if it's a valid int (see isValidInt) it converts it into an integer otherwise it throws an exception
 *
 * @param integerStr the integer to convert
 * @return the integer value of the integerStr
 * @throws IllegalArgumentException if the passed integer string is not a valid integer
 */
public static int toInteger(@Nullable final String integerStr) {
    if (!isValidInt(integerStr)) {
        throw new IllegalArgumentException(integerStr + ExceptionValues.EXCEPTION_DELIMITER + ExceptionValues.INVALID_INTEGER_VALUE);
    }
    final String stripedInteger = StringUtils.strip(integerStr);
    return NumberUtils.createInteger(stripedInteger);
}
 
Example 5
Source File: HelpCommand.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
    if (!testPermission(sender)) return true;

    String command;
    int pageNumber;
    int pageHeight;
    int pageWidth;

    if (args.length == 0) {
        command = "";
        pageNumber = 1;
    } else if (NumberUtils.isDigits(args[args.length - 1])) {
        command = StringUtils.join(ArrayUtils.subarray(args, 0, args.length - 1), " ");
        try {
            pageNumber = NumberUtils.createInteger(args[args.length - 1]);
        } catch (NumberFormatException exception) {
            pageNumber = 1;
        }
        if (pageNumber <= 0) {
            pageNumber = 1;
        }
    } else {
        command = StringUtils.join(args, " ");
        pageNumber = 1;
    }

    if (sender instanceof ConsoleCommandSender) {
        pageHeight = ChatPaginator.UNBOUNDED_PAGE_HEIGHT;
        pageWidth = ChatPaginator.UNBOUNDED_PAGE_WIDTH;
    } else {
        pageHeight = ChatPaginator.CLOSED_CHAT_PAGE_HEIGHT - 1;
        pageWidth = ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH;
    }

    HelpMap helpMap = Bukkit.getServer().getHelpMap();
    HelpTopic topic = helpMap.getHelpTopic(command);

    if (topic == null) {
        topic = helpMap.getHelpTopic("/" + command);
    }

    if (topic == null) {
        topic = findPossibleMatches(command);
    }

    if (topic == null || !topic.canSee(sender)) {
        sender.sendMessage(ChatColor.RED + "No help for " + command);
        return true;
    }

    ChatPaginator.ChatPage page = ChatPaginator.paginate(topic.getFullText(sender), pageNumber, pageWidth, pageHeight);

    StringBuilder header = new StringBuilder();
    header.append(ChatColor.YELLOW);
    header.append("--------- ");
    header.append(ChatColor.WHITE);
    header.append("Help: ");
    header.append(topic.getName());
    header.append(" ");
    if (page.getTotalPages() > 1) {
        header.append("(");
        header.append(page.getPageNumber());
        header.append("/");
        header.append(page.getTotalPages());
        header.append(") ");
    }
    header.append(ChatColor.YELLOW);
    for (int i = header.length(); i < ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH; i++) {
        header.append("-");
    }
    sender.sendMessage(header.toString());

    sender.sendMessage(page.getLines());

    return true;
}