naming improvements

This commit is contained in:
Craig Raw 2020-05-21 11:01:20 +02:00
parent 06de1d7e14
commit 785040898b
3 changed files with 34 additions and 4 deletions

View file

@ -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()) {

View file

@ -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(" ");

View file

@ -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());
}
}