Skip to content

Commit

Permalink
Introduce hash and equals for type heirarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
n1hility committed Sep 9, 2014
1 parent 4868723 commit fb66f22
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 16 deletions.
35 changes: 28 additions & 7 deletions src/main/java/org/jboss/jandex/ArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@
* @author Jason T. Greene
*/
public final class ArrayType extends Type {
private Type component;
private int dimensions;
private final Type component;
private final int dimensions;
private int hash;

ArrayType(Type component, int dimensions) {
super(null);
this.dimensions = dimensions;
this.component = component;
}

public ArrayType(DotName name) {
// FIXME parse;
super(name);
}

public Type component() {
return component;
}
Expand Down Expand Up @@ -70,4 +66,29 @@ public String toString() {
public Kind kind() {
return Kind.ARRAY;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (! (o instanceof ArrayType)) {
return false;
}
ArrayType arrayType = (ArrayType) o;

return dimensions == arrayType.dimensions && component.equals(arrayType.component);
}

@Override
public int hashCode() {
int hash = this.hash;
if (hash != 0) {
return hash;
}
hash = component.hashCode();
hash = 31 * hash + dimensions;
return this.hash = hash;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jboss/jandex/DotName.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public String toString(char delim) {

public int hashCode() {
int hash = this.hash;
if (hash > 0)
if (hash != 0)
return hash;

if (prefix != null) {
Expand Down
34 changes: 32 additions & 2 deletions src/main/java/org/jboss/jandex/ParameterizedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
*/
package org.jboss.jandex;

import java.util.Arrays;

/**
* @author Jason T. Greene
*/
public class ParameterizedType extends Type {
private Type[] parameters;
private Type owner;
private final Type[] parameters;
private final Type owner;
private int hash;

ParameterizedType(DotName name, Type[] parameters, Type owner) {
super(name);
Expand Down Expand Up @@ -64,4 +67,31 @@ public String toString() {

return builder.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!super.equals(o)) {
return false;
}

ParameterizedType other = (ParameterizedType) o;
return owner.equals(other.owner) && Arrays.equals(parameters, other.parameters);
}

@Override
public int hashCode() {
int hash = this.hash;
if (hash != 0) {
return hash;
}

hash = super.hashCode();
hash = 31 * hash + Arrays.hashCode(parameters);
hash = 31 * hash + owner.hashCode();
return this.hash = hash;
}
}
22 changes: 21 additions & 1 deletion src/main/java/org/jboss/jandex/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ public DotName name() {
public abstract Kind kind();

public String toString() {
return name().toString();
return name.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

Type type = (Type) o;

return name.equals(type.name);
}

@Override
public int hashCode() {
return name.hashCode();
}
}
38 changes: 35 additions & 3 deletions src/main/java/org/jboss/jandex/TypeVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
*/
package org.jboss.jandex;

import java.util.Arrays;

/**
* @author Jason T. Greene
*/
public class TypeVariable extends Type {
private String name;
private Type[] bounds;
public final class TypeVariable extends Type {
private final String name;
private final Type[] bounds;
private int hash;

TypeVariable(String name) {
this(name, EMPTY_ARRAY);
Expand Down Expand Up @@ -63,4 +66,33 @@ public String toString() {

return builder.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!super.equals(o)) {
return false;
}

TypeVariable that = (TypeVariable) o;

return name.equals(that.name) && Arrays.equals(bounds, that.bounds);

}

@Override
public int hashCode() {
int hash = this.hash;
if (hash != 0) {
return hash;
}

hash = super.hashCode();
hash = 31 * hash + name.hashCode();
hash = 31 * hash + Arrays.hashCode(bounds);
return this.hash = hash;
}
}
31 changes: 29 additions & 2 deletions src/main/java/org/jboss/jandex/WildcardType.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
public class WildcardType extends Type {
private static Type OBJECT = new ClassType(DotName.OBJECT_NAME);

private Type bound;
private boolean isExtends;
private final boolean isExtends;
private final Type bound;
private int hash;

WildcardType(Type bound, boolean isExtends) {
super(isExtends && bound != null ? bound.name() : DotName.OBJECT_NAME);
Expand Down Expand Up @@ -60,4 +61,30 @@ public String toString() {

return builder.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!super.equals(o)) {
return false;
}

WildcardType other = (WildcardType) o;
return isExtends == other.isExtends && bound.equals(other.bound);
}

@Override
public int hashCode() {
int hash = this.hash;
if (hash != 0) {
return hash;
}

hash = super.hashCode();
hash = 31 * hash + (isExtends ? 1 : 0);
hash = 31 * hash + bound.hashCode();
return this.hash = hash;
}
}

0 comments on commit fb66f22

Please # to comment.