1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-09 09:09:38 +02:00

Introduce EqualsUtil and HashCode.(Builder|Cache)

This commit is contained in:
Florian Schmaus 2019-06-12 14:51:17 +02:00
parent 92b02afbff
commit fa0c16d75c
8 changed files with 551 additions and 167 deletions

View file

@ -25,6 +25,8 @@ import java.util.List;
import java.util.Set;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.TypedCloneable;
import org.jivesoftware.smack.util.XmlStringBuilder;
@ -397,37 +399,22 @@ public class DiscoverInfo extends IQ implements TypedCloneable<DiscoverInfo> {
*/
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (obj.getClass() != getClass())
return false;
DiscoverInfo.Identity other = (DiscoverInfo.Identity) obj;
if (!this.key.equals(other.key))
return false;
String otherLang = other.lang == null ? "" : other.lang;
String thisLang = lang == null ? "" : lang;
if (!otherLang.equals(thisLang))
return false;
String otherName = other.name == null ? "" : other.name;
String thisName = name == null ? "" : other.name;
if (!thisName.equals(otherName))
return false;
return true;
return EqualsUtil.equals(this, obj, (e, o) -> {
e.append(key, o.key)
.append(lang, o.lang)
.append(name, o.name);
});
}
private final HashCode.Cache hashCodeCache = new HashCode.Cache();
@Override
public int hashCode() {
int result = 1;
result = 37 * result + key.hashCode();
result = 37 * result + (lang == null ? 0 : lang.hashCode());
result = 37 * result + (name == null ? 0 : name.hashCode());
return result;
return hashCodeCache.getHashCode(c ->
c.append(key)
.append(lang)
.append(name)
);
}
/**
@ -522,15 +509,9 @@ public class DiscoverInfo extends IQ implements TypedCloneable<DiscoverInfo> {
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (obj.getClass() != getClass())
return false;
DiscoverInfo.Feature other = (DiscoverInfo.Feature) obj;
return variable.equals(other.variable);
return EqualsUtil.equals(this, obj, (e, o) -> {
e.append(variable, o.variable);
});
}
@Override

View file

@ -23,6 +23,8 @@ import java.util.concurrent.Future;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleContent;
@ -112,22 +114,20 @@ public abstract class JingleSession implements JingleSessionHandler {
@Override
public int hashCode() {
int hashCode = 31 + getInitiator().hashCode();
hashCode = 31 * hashCode + getResponder().hashCode();
hashCode = 31 * hashCode + getSessionId().hashCode();
return hashCode;
return HashCode.builder()
.append(getInitiator())
.append(getResponder())
.append(getSessionId())
.build();
}
@Override
public boolean equals(Object other) {
if (!(other instanceof JingleSession)) {
return false;
}
JingleSession otherJingleSession = (JingleSession) other;
return getInitiator().equals(otherJingleSession.getInitiator())
&& getResponder().equals(otherJingleSession.getResponder())
&& sid.equals(otherJingleSession.sid);
return EqualsUtil.equals(this, other, (e, o) ->
e.append(getInitiator(), o.getInitiator())
.append(getResponder(), o.getResponder())
.append(sid, o.sid)
);
}
@Override

View file

@ -30,6 +30,8 @@ import javax.xml.namespace.QName;
import org.jivesoftware.smack.packet.FullyQualifiedElement;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.util.CollectionUtil;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.MultiMap;
import org.jivesoftware.smack.util.XmlStringBuilder;
@ -770,33 +772,20 @@ public final class FormField implements FullyQualifiedElement {
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (obj.getClass() != getClass())
return false;
Option other = (Option) obj;
if (!value.equals(other.value))
return false;
String thisLabel = label == null ? "" : label;
String otherLabel = other.label == null ? "" : other.label;
if (!thisLabel.equals(otherLabel))
return false;
return true;
return EqualsUtil.equals(this, obj, (e, o) -> {
e.append(value, o.value)
.append(label, o.label);
});
}
private final HashCode.Cache hashCodeCache = new HashCode.Cache();
@Override
public int hashCode() {
int result = 1;
result = 37 * result + value.hashCode();
result = 37 * result + (label == null ? 0 : label.hashCode());
return result;
return hashCodeCache.getHashCode(c ->
c.append(value)
.append(label)
);
}
}
@ -917,5 +906,15 @@ public final class FormField implements FullyQualifiedElement {
xml.escape(value);
return xml.closeElement(this);
}
@Override
public boolean equals(Object other) {
return EqualsUtil.equals(this, other, (e, o) -> e.append(this.value, o.value));
}
@Override
public int hashCode() {
return value.hashCode();
}
}
}