Commit df11f4de authored by iwakeh's avatar iwakeh 🌴
Browse files

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.
parent 1b5b7b43
Loading
Loading
Loading
Loading
+9 −25
Original line number Original line Diff line number Diff line
@@ -19,6 +19,7 @@ import java.util.SortedMap;
import java.util.SortedSet;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.TreeSet;
import java.util.stream.Collectors;


public class NodeStatus extends Document {
public class NodeStatus extends Document {


@@ -56,45 +57,28 @@ public class NodeStatus extends Document {


  @SuppressWarnings("checkstyle:javadocmethod")
  @SuppressWarnings("checkstyle:javadocmethod")
  public void setDeclaredFamily(SortedSet<String> declaredFamily) {
  public void setDeclaredFamily(SortedSet<String> declaredFamily) {
    if (declaredFamily.contains(this.fingerprint)) {
    SortedSet<String> declaredFamilyWithoutSelf = new TreeSet<>(declaredFamily);
      SortedSet<String> declaredFamilyWithoutSelf =
          new TreeSet<>(declaredFamily);
    declaredFamilyWithoutSelf.remove(this.fingerprint);
    declaredFamilyWithoutSelf.remove(this.fingerprint);
    this.declaredFamily = collectionToStringArray(declaredFamilyWithoutSelf);
    this.declaredFamily = collectionToStringArray(declaredFamilyWithoutSelf);
    } else {
      this.declaredFamily = collectionToStringArray(declaredFamily);
    }
  }
  }


  @SuppressWarnings("checkstyle:javadocmethod")
  @SuppressWarnings("checkstyle:javadocmethod")
  public SortedSet<String> getDeclaredFamily() {
  public SortedSet<String> getDeclaredFamily() {
    SortedSet<String> declaredFamilyWithoutSelf =
    SortedSet<String> declaredFamilyWithoutSelf =
        stringArrayToSortedSet(this.declaredFamily);
        stringArrayToSortedSet(this.declaredFamily);
    if (declaredFamilyWithoutSelf.contains(this.fingerprint)) {
    declaredFamilyWithoutSelf.remove(this.fingerprint);
    declaredFamilyWithoutSelf.remove(this.fingerprint);
    }
    return declaredFamilyWithoutSelf;
    return declaredFamilyWithoutSelf;
  }
  }


  private static String[] collectionToStringArray(
  private static String[] collectionToStringArray(
      Collection<String> collection) {
      Collection<String> collection) {
    String[] stringArray = null;
    return (null == collection || collection.isEmpty()) ? null
    if (collection != null && !collection.isEmpty()) {
        : collection.toArray(new String[collection.size()]);
      stringArray = new String[collection.size()];
      int index = 0;
      for (String string : collection) {
        stringArray[index++] = string;
      }
    }
    return stringArray;
  }
  }


  private SortedSet<String> stringArrayToSortedSet(String[] stringArray) {
  private SortedSet<String> stringArrayToSortedSet(String[] stringArray) {
    SortedSet<String> sortedSet = new TreeSet<>();
    return stringArray == null ? new TreeSet<>() : Arrays.stream(stringArray)
    if (stringArray != null) {
        .collect(Collectors.toCollection(TreeSet::new));
      sortedSet.addAll(Arrays.asList(stringArray));
    }
    return sortedSet;
  }
  }


  /* From network status entries: */
  /* From network status entries: */