Skip to content

Commit

Permalink
GROOVY-10463: Add equals() and hashCode() methods to ImportNode
Browse files Browse the repository at this point in the history
  • Loading branch information
tmoore authored and paulk-asert committed Jan 21, 2022
1 parent da55697 commit 3bbb165
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/main/java/org/codehaus/groovy/ast/ImportNode.java
Expand Up @@ -31,6 +31,7 @@ public class ImportNode extends AnnotatedNode {
private final String packageName;
private final boolean isStar;
private final boolean isStatic;
private int hashCode;

/**
* An import of a single type, i.e. {@code import pack.Type} or {@code import pack.Type as Alias}
Expand Down Expand Up @@ -147,6 +148,53 @@ public void setType(final ClassNode type) {
this.type = type;
}

@Override
public boolean equals(Object o) {

This comment has been minimized.

Copy link
@eric-milles

eric-milles Jan 21, 2022

Member

Could you just compare the toString() value? That would include alias and star and whatnot.

if (!(o instanceof ImportNode))
return false;
ImportNode imp = (ImportNode) o;
if ((type == null) != (imp.type == null))
return false;
if (type != null && !type.equals(imp.type))
return false;
if ((alias == null) != (imp.alias == null))
return false;
if (alias != null && !alias.equals(imp.alias))
return false;
if ((fieldName == null) != (imp.fieldName == null))
return false;
if (fieldName != null && !fieldName.equals(imp.fieldName))
return false;
if ((packageName == null) != (imp.packageName == null))
return false;
if (packageName != null && !packageName.equals(imp.packageName))
return false;
if (isStar != imp.isStar)
return false;
if (isStatic != imp.isStatic)
return false;
return true;
}

@Override
public int hashCode() {
int result = hashCode;

This comment has been minimized.

Copy link
@eric-milles

eric-milles Jan 21, 2022

Member

Like equals, could you just return toString().hashCode()? Does this value need to be cached?

if (result == 0) {
if (type != null)
result = 31 * result + type.hashCode();
if (alias != null)
result = 31 * result + alias.hashCode();
if (fieldName != null)
result = 31 * result + fieldName.hashCode();
if (packageName != null)
result = 31 * result + packageName.hashCode();
result = 31 * result +Boolean.hashCode(isStar);
result = 31 * result +Boolean.hashCode(isStatic);
hashCode = result;
}
return result;
}

@Override
public void visit(final GroovyCodeVisitor visitor) {
}
Expand Down

0 comments on commit 3bbb165

Please sign in to comment.