Skip to content

Commit

Permalink
Ensure both equals and hashCode are always present
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacchella committed Jul 5, 2016
1 parent e6be34a commit a3bf3b8
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 16 deletions.
30 changes: 23 additions & 7 deletions src/jrds/GraphNode.java
Expand Up @@ -48,13 +48,6 @@ public GraphNode(Probe<?, ?> theStore, GraphDesc gd) {
protected GraphNode() {
}

/**
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return getQualifiedName().hashCode();
}

/**
* @return Returns the theStore.
*/
Expand Down Expand Up @@ -217,4 +210,27 @@ public void setBeans(Map<String, String> beans) {
this.beans = beans;
}

/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return getQualifiedName().hashCode();
}

/**
* Two nodes are equals if qualified names matches
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj == null || getClass() != obj.getClass()) {
return false;
} else {
return getQualifiedName().equals(obj);
}
}

}
19 changes: 19 additions & 0 deletions src/jrds/Probe.java
Expand Up @@ -418,10 +418,29 @@ public String getQualifiedName() {
return getHost().getName() + "/" + getName();
}

/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return getQualifiedName().hashCode();
}

/**
* Two probes are equals if qualified names matches
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj == null || getClass() != obj.getClass()) {
return false;
} else {
return getQualifiedName().equals(obj);
}
}

public Set<String> getTags() {
return getHost().getTags();
}
Expand Down
38 changes: 34 additions & 4 deletions src/jrds/starter/ConnectionInfo.java
Expand Up @@ -81,14 +81,44 @@ public String toString() {
return type.getCanonicalName() + (name == null ? "" : ("/" + name));
}

/*
* (non-Javadoc)
*
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return name == null ? type.hashCode() : name.hashCode();
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}

/**
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj == null || getClass() != obj.getClass()) {
return false;
} else {
ConnectionInfo other = (ConnectionInfo) obj;
if (name == null) {
if(other.name != null)
return false;
} else if (!name.equals(other.name)) {
return false;
}
if (type == null) {
if (other.type != null) {
return false;
}
} else if (!type.equals(other.type)) {
return false;
}
}
return true;
}

}
24 changes: 19 additions & 5 deletions src/jrds/starter/HostStarter.java
Expand Up @@ -92,21 +92,35 @@ public boolean isHidden() {
return host.isHidden();
}

/*
* (non-Javadoc)
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + super.hashCode();
result = prime * result + (getParent() == null ? 0 : getParent().hashCode());
return result;
}

/**
* Two hosts are equals if they are equals and their parents too
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if(!(obj instanceof HostStarter))
if (!(obj instanceof HostStarter)) {
return false;
}
HostStarter other = (HostStarter) obj;
boolean parentEquals;
if(getParent() != null)
if (getParent() != null) {
parentEquals = getParent().equals(other.getParent());
else
} else {
parentEquals = other.getParent() == null;
}
return host.equals(other.getHost()) && parentEquals;
}

Expand Down

0 comments on commit a3bf3b8

Please sign in to comment.