From c6199890be59cd324c8a08c74920e4315a2346a4 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sun, 26 Jul 2020 12:24:26 -0500 Subject: Added equals, hashCode and isEmpty to NameSymbol --- src/org/unitConverter/unit/NameSymbol.java | 92 ++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 23 deletions(-) (limited to 'src/org') diff --git a/src/org/unitConverter/unit/NameSymbol.java b/src/org/unitConverter/unit/NameSymbol.java index 8d49c82..7fa5304 100644 --- a/src/org/unitConverter/unit/NameSymbol.java +++ b/src/org/unitConverter/unit/NameSymbol.java @@ -33,6 +33,28 @@ import java.util.Set; public final class NameSymbol { public static final NameSymbol EMPTY = new NameSymbol(Optional.empty(), Optional.empty(), new HashSet<>()); + /** + * Creates a {@code NameSymbol}, ensuring that if primaryName is null and + * otherNames is not empty, one name is moved from otherNames to primaryName + * + * Ensure that otherNames is a copy of the inputted argument. + */ + private static final NameSymbol create(final String name, final String symbol, final Set otherNames) { + final Optional primaryName; + + if (name == null && !otherNames.isEmpty()) { + // get primary name and remove it from savedNames + Iterator it = otherNames.iterator(); + assert it.hasNext(); + primaryName = Optional.of(it.next()); + otherNames.remove(primaryName.get()); + } else { + primaryName = Optional.ofNullable(name); + } + + return new NameSymbol(primaryName, Optional.ofNullable(symbol), otherNames); + } + /** * Gets a {@code NameSymbol} with a primary name, a symbol and no other names. * @@ -168,28 +190,6 @@ public final class NameSymbol { return NameSymbol.create(name, symbol, otherNames == null ? new HashSet<>() : new HashSet<>(otherNames)); } - /** - * Creates a {@code NameSymbol}, ensuring that if primaryName is null and - * otherNames is not empty, one name is moved from otherNames to primaryName - * - * Ensure that otherNames is a copy of the inputted argument. - */ - private static final NameSymbol create(final String name, final String symbol, final Set otherNames) { - final Optional primaryName; - - if (name == null && !otherNames.isEmpty()) { - // get primary name and remove it from savedNames - Iterator it = otherNames.iterator(); - assert it.hasNext(); - primaryName = Optional.of(it.next()); - otherNames.remove(primaryName.get()); - } else { - primaryName = Optional.ofNullable(name); - } - - return new NameSymbol(primaryName, Optional.ofNullable(symbol), otherNames); - } - /** * h * Gets a {@code NameSymbol} with a primary name, a symbol and additional * names. @@ -243,6 +243,34 @@ public final class NameSymbol { this.symbol = symbol; otherNames.remove(null); this.otherNames = Collections.unmodifiableSet(otherNames); + + if (this.primaryName.isEmpty()) + assert this.otherNames.isEmpty(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!(obj instanceof NameSymbol)) + return false; + NameSymbol other = (NameSymbol) obj; + if (otherNames == null) { + if (other.otherNames != null) + return false; + } else if (!otherNames.equals(other.otherNames)) + return false; + if (primaryName == null) { + if (other.primaryName != null) + return false; + } else if (!primaryName.equals(other.primaryName)) + return false; + if (symbol == null) { + if (other.symbol != null) + return false; + } else if (!symbol.equals(other.symbol)) + return false; + return true; } /** @@ -260,7 +288,7 @@ public final class NameSymbol { public final Optional getPrimaryName() { return this.primaryName; } - + /** * @return symbol * @since 2019-10-21 @@ -268,4 +296,22 @@ public final class NameSymbol { public final Optional getSymbol() { return this.symbol; } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((otherNames == null) ? 0 : otherNames.hashCode()); + result = prime * result + ((primaryName == null) ? 0 : primaryName.hashCode()); + result = prime * result + ((symbol == null) ? 0 : symbol.hashCode()); + return result; + } + + /** + * @return true iff this {@code NameSymbol} contains no names or symbols. + */ + public final boolean isEmpty() { + // if primaryName is empty, otherNames must also be empty + return this.primaryName.isEmpty() && this.symbol.isEmpty(); + } } \ No newline at end of file -- cgit v1.2.3