summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/unitConverter/unit/NameSymbol.java92
1 files changed, 69 insertions, 23 deletions
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
@@ -34,6 +34,28 @@ 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<String> otherNames) {
+ final Optional<String> primaryName;
+
+ if (name == null && !otherNames.isEmpty()) {
+ // get primary name and remove it from savedNames
+ Iterator<String> 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.
*
* @param name name to use
@@ -169,28 +191,6 @@ public final class NameSymbol {
}
/**
- * 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<String> otherNames) {
- final Optional<String> primaryName;
-
- if (name == null && !otherNames.isEmpty()) {
- // get primary name and remove it from savedNames
- Iterator<String> 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.
* <p>
@@ -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<String> getPrimaryName() {
return this.primaryName;
}
-
+
/**
* @return symbol
* @since 2019-10-21
@@ -268,4 +296,22 @@ public final class NameSymbol {
public final Optional<String> 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