Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve equals and hashCode implementations using Objects #2095

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/main/java/org/jsoup/nodes/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -248,15 +249,12 @@ public boolean equals(@Nullable Object o) { // note parent not considered
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Attribute attribute = (Attribute) o;
if (key != null ? !key.equals(attribute.key) : attribute.key != null) return false;
return val != null ? val.equals(attribute.val) : attribute.val == null;
return Objects.equals(key, attribute.key) && Objects.equals(val, attribute.val);
}

@Override
public int hashCode() { // note parent not considered
int result = key != null ? key.hashCode() : 0;
result = 31 * result + (val != null ? val.hashCode() : 0);
return result;
return Objects.hash(key, val);
}

@Override
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/org/jsoup/nodes/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;

import static org.jsoup.internal.Normalizer.lowerCase;
Expand Down Expand Up @@ -496,14 +497,7 @@ public boolean equals(@Nullable Object o) {
for (int i = 0; i < size; i++) {
String key = keys[i];
int thatI = that.indexOfKey(key);
if (thatI == NotFound)
return false;
Object val = vals[i];
Object thatVal = that.vals[thatI];
if (val == null) {
if (thatVal != null)
return false;
} else if (!val.equals(thatVal))
if (thatI == NotFound || !Objects.equals(vals[i], that.vals[thatI]))
return false;
}
return true;
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/org/jsoup/nodes/Range.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jsoup.nodes;

import java.util.Objects;

import static org.jsoup.internal.SharedConstants.*;

/**
Expand Down Expand Up @@ -108,9 +110,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = start.hashCode();
result = 31 * result + end.hashCode();
return result;
return Objects.hash(start, end);
}

/**
Expand Down Expand Up @@ -198,10 +198,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = pos;
result = 31 * result + lineNumber;
result = 31 * result + columnNumber;
return result;
return Objects.hash(pos, lineNumber, columnNumber);
}
}

Expand Down Expand Up @@ -245,9 +242,7 @@ public Range valueRange() {
}

@Override public int hashCode() {
int result = nameRange.hashCode();
result = 31 * result + valueRange.hashCode();
return result;
return Objects.hash(nameRange, valueRange);
}
}
}
12 changes: 3 additions & 9 deletions src/main/java/org/jsoup/parser/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -232,15 +233,8 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = tagName.hashCode();
result = 31 * result + (isBlock ? 1 : 0);
result = 31 * result + (formatAsBlock ? 1 : 0);
result = 31 * result + (empty ? 1 : 0);
result = 31 * result + (selfClosing ? 1 : 0);
result = 31 * result + (preserveWhitespace ? 1 : 0);
result = 31 * result + (formList ? 1 : 0);
result = 31 * result + (formSubmit ? 1 : 0);
return result;
return Objects.hash(tagName, isBlock, formatAsBlock, empty, selfClosing, preserveWhitespace,
formList, formSubmit);
}

@Override
Expand Down
13 changes: 4 additions & 9 deletions src/main/java/org/jsoup/safety/Safelist.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Thank you to Ryan Grove (wonko.com) for the Ruby HTML cleaner http://github.com/
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.jsoup.internal.Normalizer.lowerCase;
Expand Down Expand Up @@ -646,21 +647,15 @@ abstract static class TypedValue {

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

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
if (obj == null || getClass() != obj.getClass()) return false;
TypedValue other = (TypedValue) obj;
if (value == null) {
return other.value == null;
} else return value.equals(other.value);
return Objects.equals(value, other.value);
}

@Override
Expand Down