Java Code Examples for com.google.common.primitives.Ints.tryParse()

The following are Jave code examples for showing how to use tryParse() of the com.google.common.primitives.Ints class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Elasticsearch   File: NodeEnvironment.java   Source Code and License Vote up 6 votes
private static Set<ShardId> findAllShardsForIndex(Path indexPath) throws IOException {
    Set<ShardId> shardIds = new HashSet<>();
    if (Files.isDirectory(indexPath)) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(indexPath)) {
            String currentIndex = indexPath.getFileName().toString();
            for (Path shardPath : stream) {
                if (Files.isDirectory(shardPath)) {
                    Integer shardId = Ints.tryParse(shardPath.getFileName().toString());
                    if (shardId != null) {
                        ShardId id = new ShardId(currentIndex, shardId);
                        shardIds.add(id);
                    }
                }
            }
        }
    }
    return shardIds;
}
 
Example 2
Project: Elasticsearch   File: MultiDataPathUpgrader.java   Source Code and License Vote up 6 votes
private static Set<ShardId> findAllShardsForIndex(Path indexPath) throws IOException {
    Set<ShardId> shardIds = new HashSet<>();
    if (Files.isDirectory(indexPath)) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(indexPath)) {
            String currentIndex = indexPath.getFileName().toString();
            for (Path shardPath : stream) {
                if (Files.isDirectory(shardPath)) {
                    Integer shardId = Ints.tryParse(shardPath.getFileName().toString());
                    if (shardId != null) {
                        ShardId id = new ShardId(currentIndex, shardId);
                        shardIds.add(id);
                    }
                }
            }
        }
    }
    return shardIds;
}
 
Example 3
Project: Reer   File: S3ConnectionProperties.java   Source Code and License Vote up 5 votes
private Optional<Integer> configureErrorRetryCount(String property) {
    Integer count = null;
    if (null != property) {
        count = Ints.tryParse(property);
        if (null == count || count < 0) {
            throw new IllegalArgumentException("System property [" + S3_MAX_ERROR_RETRY + "=" + property + "]  must be a valid positive Integer");

        }
    }
    return Optional.fromNullable(count);
}
 
Example 4
Project: lttng-scope   File: DebugOptionsDialog.java   Source Code and License Vote up 5 votes
@Override
public void save() {
    String text = getTextField().getText();
    Integer value = Ints.tryParse(text);
    if (value == null) {
        getOption().resetToDefault();
        load();
    } else {
        getOption().set(value);
    }
}
 
Example 5
Project: HCFCore   File: LivesGiveArgument.java   Source Code and License Vote up 5 votes
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (args.length < 3) {
        sender.sendMessage((Object)ChatColor.RED + "Usage: " + this.getUsage(label));
        return true;
    }
    Integer amount = Ints.tryParse((String)args[2]);
    if (amount == null) {
        sender.sendMessage((Object)ChatColor.RED + "'" + args[2] + "' is not a number.");
        return true;
    }
    if (amount <= 0) {
        sender.sendMessage((Object)ChatColor.RED + "The amount of lives must be positive.");
        return true;
    }
    OfflinePlayer target = Bukkit.getOfflinePlayer((String)args[1]);
    if (!target.hasPlayedBefore() && !target.isOnline()) {
        sender.sendMessage(String.format(BaseConstants.PLAYER_WITH_NAME_OR_UUID_NOT_FOUND, args[1]));
        return true;
    }
    Player onlineTarget = target.getPlayer();
    if (sender instanceof Player && !sender.hasPermission(PERMISSION)) {
        Player player = (Player)sender;
        int ownedLives = this.plugin.getDeathbanManager().getLives(player.getUniqueId());
        if (amount > ownedLives) {
            sender.sendMessage((Object)ChatColor.RED + "You tried to give " + target.getName() + ' ' + amount + " lives, but you only have " + ownedLives + '.');
            return true;
        }
        this.plugin.getDeathbanManager().takeLives(player.getUniqueId(), amount);
    }
    final int targetLives = this.plugin.getDeathbanManager().getLives(target.getUniqueId());
    this.plugin.getDeathbanManager().addLives(target.getUniqueId(), amount);
    sender.sendMessage((Object)ChatColor.YELLOW + "You have sent " + (Object)ChatColor.GOLD + target.getName() + (Object)ChatColor.YELLOW + ' ' + amount + ' ' + (amount > 1 ? "life" : "lives") + '.');
    sender.sendMessage(ChatColor.GREEN + "Remaining Lives: " + ChatColor.RED + targetLives +  ChatColor.RED + ' ' + ((targetLives == 1) ? "life" : "lives") + '.');
    if (onlineTarget != null) {
        onlineTarget.sendMessage((Object)ChatColor.GOLD + sender.getName() + (Object)ChatColor.YELLOW + " has sent you " + (Object)ChatColor.GOLD + amount + ' ' + (amount > 1 ? "life" : "lives") + '.');
    }
    return true;
}
 
Example 6
Project: appinventor-extensions   File: MockImageBase.java   Source Code and License Vote up 5 votes
/**
 * This resizes the picture according to
 * 1. height and width value of the div tag enclosing the img tag
 * 2. scaling mode. 0 - Scale proportionally, 1 - Scale to fit
 *    which correspond to the choices in ScalingChoicePropertyEditor
 *
 * This should be called whenever a property affecting the size is changed
 */
private void resizeImage() {
  if (image.getUrl().equals(getIconImage().getUrl())) {
    unclipImage();
    return;
  }

  String width = getElement().getStyle().getWidth();
  String height = getElement().getStyle().getHeight();

  // the situation right after refreshing the page
  if (width.isEmpty() || height.isEmpty()) {
    return;
  }

  int frameWidth = Ints.tryParse(width.substring(0, width.indexOf("px")));
  int frameHeight = Ints.tryParse(height.substring(0, height.indexOf("px")));

  if (scalingMode.equals("0")) {
    float ratio = Math.min(frameWidth / (float) getPreferredWidth(),
        frameHeight / (float) getPreferredHeight());
    int scaledWidth = Double.valueOf(getPreferredWidth() * ratio).intValue();
    int scaledHeight = Double.valueOf(getPreferredHeight() * ratio).intValue();
    image.setSize(scaledWidth + "px", scaledHeight + "px");

  } else if (scalingMode.equals("1")) {
    image.setSize("100%", "100%");

  } else {
    throw new IllegalStateException("Illegal scaling mode: " + scalingMode);
  }
}
 
Example 7
Project: ArchUnit   File: Formatters.java   Source Code and License Vote up 4 votes
private static boolean isAnonymousRest(String lastPart) {
    return Ints.tryParse(lastPart) != null;
}