diff options
| author | iwakeh <iwakeh@torproject.org> | 2018-02-14 21:45:21 +0000 |
|---|---|---|
| committer | iwakeh <iwakeh@torproject.org> | 2018-02-14 21:45:24 +0000 |
| commit | df11f4de038c31f33c3f6e231854887995271201 (patch) | |
| tree | 3214641f5cfd9410a5f006cba763db4a5feca88a | |
| parent | 1b5b7b4342205b3b4f49e7ba4ee42d634f1eae8f (diff) | |
Shorten code.
It is less effort to attempt the removal w/o knowing the set contains the string
to be removed.
Make use of the toArray method in Collection.
| -rw-r--r-- | src/main/java/org/torproject/onionoo/docs/NodeStatus.java | 34 |
1 files changed, 9 insertions, 25 deletions
diff --git a/src/main/java/org/torproject/onionoo/docs/NodeStatus.java b/src/main/java/org/torproject/onionoo/docs/NodeStatus.java index 299779f..dfa0026 100644 --- a/src/main/java/org/torproject/onionoo/docs/NodeStatus.java +++ b/src/main/java/org/torproject/onionoo/docs/NodeStatus.java @@ -19,6 +19,7 @@ import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; +import java.util.stream.Collectors; public class NodeStatus extends Document { @@ -56,45 +57,28 @@ public class NodeStatus extends Document { @SuppressWarnings("checkstyle:javadocmethod") public void setDeclaredFamily(SortedSet<String> declaredFamily) { - if (declaredFamily.contains(this.fingerprint)) { - SortedSet<String> declaredFamilyWithoutSelf = - new TreeSet<>(declaredFamily); - declaredFamilyWithoutSelf.remove(this.fingerprint); - this.declaredFamily = collectionToStringArray(declaredFamilyWithoutSelf); - } else { - this.declaredFamily = collectionToStringArray(declaredFamily); - } + SortedSet<String> declaredFamilyWithoutSelf = new TreeSet<>(declaredFamily); + declaredFamilyWithoutSelf.remove(this.fingerprint); + this.declaredFamily = collectionToStringArray(declaredFamilyWithoutSelf); } @SuppressWarnings("checkstyle:javadocmethod") public SortedSet<String> getDeclaredFamily() { SortedSet<String> declaredFamilyWithoutSelf = stringArrayToSortedSet(this.declaredFamily); - if (declaredFamilyWithoutSelf.contains(this.fingerprint)) { - declaredFamilyWithoutSelf.remove(this.fingerprint); - } + declaredFamilyWithoutSelf.remove(this.fingerprint); return declaredFamilyWithoutSelf; } private static String[] collectionToStringArray( Collection<String> collection) { - String[] stringArray = null; - if (collection != null && !collection.isEmpty()) { - stringArray = new String[collection.size()]; - int index = 0; - for (String string : collection) { - stringArray[index++] = string; - } - } - return stringArray; + return (null == collection || collection.isEmpty()) ? null + : collection.toArray(new String[collection.size()]); } private SortedSet<String> stringArrayToSortedSet(String[] stringArray) { - SortedSet<String> sortedSet = new TreeSet<>(); - if (stringArray != null) { - sortedSet.addAll(Arrays.asList(stringArray)); - } - return sortedSet; + return stringArray == null ? new TreeSet<>() : Arrays.stream(stringArray) + .collect(Collectors.toCollection(TreeSet::new)); } /* From network status entries: */ |
