From acf2c7c50ef1bf7c3f03f35c71a81ebc3b0e98eb Mon Sep 17 00:00:00 2001 From: ghubstan <36207203+ghubstan@users.noreply.github.com> Date: Thu, 14 Jan 2021 10:27:47 -0300 Subject: [PATCH] Remove deprecated NegativeNumberOptions Not needed anymore, and all method opts are posix style. (The opts parsing lib used to treat negative numbers as opt labels.) --- .../java/bisq/cli/NegativeNumberOptions.java | 97 ------------------- 1 file changed, 97 deletions(-) delete mode 100644 cli/src/main/java/bisq/cli/NegativeNumberOptions.java diff --git a/cli/src/main/java/bisq/cli/NegativeNumberOptions.java b/cli/src/main/java/bisq/cli/NegativeNumberOptions.java deleted file mode 100644 index 6623f9ad15..0000000000 --- a/cli/src/main/java/bisq/cli/NegativeNumberOptions.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq is free software: you can redistribute it and/or modify it - * under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at - * your option) any later version. - * - * Bisq is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - * License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Bisq. If not, see . - */ - -package bisq.cli; - -import java.math.BigDecimal; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; - -import static java.util.Arrays.stream; -import static java.util.stream.IntStream.range; - -class NegativeNumberOptions { - - private final Map negativeNumberParams = new HashMap<>(); - - String[] removeNegativeNumberOptions(String[] args) { - // Cache any negative number params that will be rejected by the parser. - // This should be called before command line parsing. - int skipped = getIndexOfMethodInArgs(args); - for (int i = skipped; i < args.length; i++) { - if (isNegativeNumber.test(args[i])) { - String param = args[i]; - negativeNumberParams.put(i - skipped, new BigDecimal(param).toString()); - // Substitute a zero placeholder at the index containing the - // negative number positional option value. - args[i] = "0"; - } - } - return args; - } - - List restoreNegativeNumberOptions(List nonOptionArgs) { - // Put cached negative number params into a clone of the nonOptionArgs list. - // This should be called after command line parsing. - if (!negativeNumberParams.isEmpty()) { - List nonOptionArgsClone = new ArrayList<>(nonOptionArgs); - negativeNumberParams.forEach((k, v) -> { - int idx = k; - nonOptionArgsClone.set(idx, v); - }); - return Collections.unmodifiableList(nonOptionArgsClone); - } else { - // This should never happen. Instances of this class should not be created - // if there are no negative number options. - return nonOptionArgs; - } - } - - static boolean hasNegativeNumberOptions(String[] args) { - return stream(args).anyMatch(isNegativeNumber); - } - - private static final Predicate isNegativeNumber = (param) -> { - if (param.length() > 1 && param.startsWith("-")) { - try { - new BigDecimal(param); - return true; - } catch (NumberFormatException ignored) { - // empty - } - } - return false; - }; - - private int getIndexOfMethodInArgs(String[] args) { - // The first argument that does not start with '-' or '--' is the method name. - // Skip over the --password=xyz [--host=s --port=n] options. - int skipped = range(0, args.length) - .filter(i -> !args[i].startsWith("-")) - .findFirst() - .orElse(-1); - if (skipped >= 0) - return skipped; - else - throw new IllegalArgumentException("required --password option not found"); - } -}