Skip to content

Commit

Permalink
Use hashCode() and equals() for elements in map
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-ronge committed Apr 9, 2021
1 parent c161662 commit c001166
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Optional;
import java.util.function.Function;

import org.apache.commons.lang3.tuple.Triple;
import org.kitodo.dataeditor.ruleset.xml.RestrictivePermit;
import org.kitodo.dataeditor.ruleset.xml.Ruleset;
import org.kitodo.dataeditor.ruleset.xml.Unspecified;
Expand All @@ -31,19 +30,6 @@
* restrictions.
*/
public class Rule {
/**
* Generates a triplet of rule with triple as a key. This is due to the
* problem because the rule is basically the key is three fields and applies
* everything.
*
* @param restrictivePermit
* restrictive permit for which a hash key is to be formed
* @return key is triple
*/
private static Triple<String, String, String> formAKeyForARuleInATemporaryMap(RestrictivePermit restrictivePermit) {
return Triple.of(restrictivePermit.getDivision().orElse(null), restrictivePermit.getKey().orElse(null), restrictivePermit.getValue().orElse(null));
}

/**
* Maybe a rule, but maybe not.
*/
Expand Down Expand Up @@ -247,16 +233,15 @@ private RestrictivePermit merge(RestrictivePermit one, RestrictivePermit another
: Unspecified.UNRESTRICTED);

// for sub-rules, apply recursively
HashMap<Triple<String, String, String>, RestrictivePermit> anotherPermits = new LinkedHashMap<>();
HashMap<RestrictivePermit, RestrictivePermit> anotherPermits = new LinkedHashMap<>();
for (RestrictivePermit anotherPermit : another.getPermits()) {
anotherPermits.put(formAKeyForARuleInATemporaryMap(anotherPermit), anotherPermit);
anotherPermits.put(anotherPermit, anotherPermit);
}
List<RestrictivePermit> mergedPermits = new LinkedList<>();
for (RestrictivePermit onePermit : one.getPermits()) {
Triple<String, String, String> key = formAKeyForARuleInATemporaryMap(onePermit);
if (anotherPermits.containsKey(key)) {
mergedPermits.add(merge(onePermit, anotherPermits.get(key)));
anotherPermits.remove(key);
if (anotherPermits.containsKey(onePermit)) {
mergedPermits.add(merge(onePermit, anotherPermits.get(onePermit)));
anotherPermits.remove(onePermit);
} else {
mergedPermits.add(onePermit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,47 @@ public void setUnspecified(Unspecified unspecified) {
public void setValue(Optional<String> value) {
this.value = value.orElse(null);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((division == null) ? 0 : division.hashCode());
result = prime * result + ((key == null) ? 0 : key.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof RestrictivePermit)) {
return false;
}
RestrictivePermit other = (RestrictivePermit) obj;
if (division == null) {
if (other.division != null) {
return false;
}
} else if (!division.equals(other.division)) {
return false;
}
if (key == null) {
if (other.key != null) {
return false;
}
} else if (!key.equals(other.key)) {
return false;
}
if (value == null) {
if (other.value != null) {
return false;
}
} else if (!value.equals(other.value)) {
return false;
}
return true;
}
}

0 comments on commit c001166

Please sign in to comment.