From 785040898babf4b198985531f61af7ce48178b17 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 21 May 2020 11:01:20 +0200 Subject: [PATCH] naming improvements --- .../sparrowwallet/drongo/wallet/Wallet.java | 25 ++++++++++++++++--- .../drongo/wallet/WalletModel.java | 3 +++ .../drongo/wallet/WalletTest.java | 10 ++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java index ca7a6d7..24791c2 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java @@ -126,13 +126,18 @@ public class Wallet { return !keystores.stream().map(Keystore::getLabel).allMatch(new HashSet<>()::add); } - public int makeLabelsUnique(Keystore newKeystore) { + public void makeLabelsUnique(Keystore newKeystore) { + makeLabelsUnique(newKeystore, false); + } + + private int makeLabelsUnique(Keystore newKeystore, boolean duplicateFound) { int max = 0; for(Keystore keystore : getKeystores()) { if(newKeystore != keystore && keystore.getLabel().startsWith(newKeystore.getLabel())) { + duplicateFound = true; String remainder = keystore.getLabel().substring(newKeystore.getLabel().length()); if(remainder.length() == 0) { - max = makeLabelsUnique(keystore); + max = makeLabelsUnique(keystore, true); } else { try { int count = Integer.parseInt(remainder.trim()); @@ -144,8 +149,10 @@ public class Wallet { } } - max++; - newKeystore.setLabel(newKeystore.getLabel() + " " + max); + if(duplicateFound) { + max++; + newKeystore.setLabel(newKeystore.getLabel() + " " + max); + } return max; } @@ -171,6 +178,16 @@ public class Wallet { return false; } + public boolean containsSource(KeystoreSource keystoreSource) { + for(Keystore keystore : keystores) { + if(keystoreSource.equals(keystore.getSource())) { + return true; + } + } + + return false; + } + public boolean isEncrypted() { for(Keystore keystore : keystores) { if(keystore.isEncrypted()) { diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/WalletModel.java b/src/main/java/com/sparrowwallet/drongo/wallet/WalletModel.java index 593761a..6bcaec6 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/WalletModel.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/WalletModel.java @@ -30,6 +30,9 @@ public enum WalletModel { String[] words = line.split("_"); StringBuilder builder = new StringBuilder(); for(String word : words) { + if(word.equals("1")) { + word = "one"; + } builder.append(Character.toUpperCase(word.charAt(0))); builder.append(word.substring(1)); builder.append(" "); diff --git a/src/test/java/com/sparrowwallet/drongo/wallet/WalletTest.java b/src/test/java/com/sparrowwallet/drongo/wallet/WalletTest.java index ba8e028..fce3d5e 100644 --- a/src/test/java/com/sparrowwallet/drongo/wallet/WalletTest.java +++ b/src/test/java/com/sparrowwallet/drongo/wallet/WalletTest.java @@ -55,5 +55,15 @@ public class WalletTest { wallet.makeLabelsUnique(cckeystore); Assert.assertEquals("Coldcard 3", keystore3.getLabel()); Assert.assertEquals("Coldcard 4", cckeystore.getLabel()); + + Keystore eekeystore = new Keystore("Electrum"); + wallet.makeLabelsUnique(cckeystore); + Assert.assertEquals("Electrum", eekeystore.getLabel()); + wallet.getKeystores().add(eekeystore); + + Keystore eekeystore2 = new Keystore("Electrum"); + wallet.makeLabelsUnique(eekeystore2); + Assert.assertEquals("Electrum 1", eekeystore.getLabel()); + Assert.assertEquals("Electrum 2", eekeystore2.getLabel()); } }