Skip to content

Commit

Permalink
Give TemplateDescriptions proper equals() and hashCode() (#312)
Browse files Browse the repository at this point in the history
* Give TemplateDescriptions proper equals() and hashCode()

These types are frequently used in Sets. Accordingly, they'll be hashed, and so should have proper hashCode() and equals() implementations.

The implementations I chose were a best-effort guess as to the identity of the models. ClassInfo was particularly challenging as it has a mutable field and I found it challenging to figure out the difference between the various types of names.

* fixup! Give TemplateDescriptions proper equals() and hashCode()

* fixup! Give TemplateDescriptions proper equals() and hashCode()
  • Loading branch information
MariusVolkhart committed Jan 10, 2024
1 parent 6cb0fa7 commit 7ad2e12
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
* Mock implementation to help with testing extensions.
Expand Down Expand Up @@ -82,4 +83,21 @@ public List<ParamDescription> params() {
public List<String> imports() {
return imports;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

MockTemplateDescription that = (MockTemplateDescription) o;

if (!Objects.equals(name, that.name)) return false;
if (!Objects.equals(packageName, that.packageName)) return false;
return Objects.equals(className, that.className);
}

@Override
public int hashCode() {
return Objects.hash(name, packageName, className);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

/**
* An instance of this class will be given to an extension for each jte template.
* <p>
* Implementations must properly implement {@link Object#equals(Object)} and {@link Object#hashCode()}.
*/
public interface TemplateDescription {
String name();
Expand Down
15 changes: 15 additions & 0 deletions jte-runtime/src/main/java/gg/jte/runtime/ClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@ public ClassInfo(String name, String parentPackage) {
}
fullName = packageName + "." + className;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ClassInfo classInfo = (ClassInfo) o;

return fullName.equals(classInfo.fullName);
}

@Override
public int hashCode() {
return fullName.hashCode();
}
}
25 changes: 25 additions & 0 deletions jte-runtime/src/test/java/gg/jte/runtime/ClassInfoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gg.jte.runtime;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ClassInfoTest {

@Test
void equality() {
var a1 = new ClassInfo("Message", "my.app");
var a2 = new ClassInfo("Message", "my.app");
var b = new ClassInfo("OtherMessage", "my.app");
var c = new ClassInfo("Message", "my.app.package");

assertThat(a1).isEqualTo(a2);
assertThat(a1).hasSameHashCodeAs(a2);

assertThat(a1).isNotEqualTo(b);
assertThat(a1).doesNotHaveSameHashCodeAs(b);

assertThat(a1).isNotEqualTo(c);
assertThat(a1).doesNotHaveSameHashCodeAs(c);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ExtensionTemplateDescription implements TemplateDescription {
private final ClassDefinition classDefinition;
Expand Down Expand Up @@ -42,5 +43,19 @@ public List<String> imports() {
return classDefinition.getImports();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ExtensionTemplateDescription that = (ExtensionTemplateDescription) o;

if (!classDefinition.equals(that.classDefinition)) return false;
return classInfo.equals(that.classInfo);
}

@Override
public int hashCode() {
return Objects.hash(classDefinition, classInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package gg.jte.compiler.extensionsupport;

import gg.jte.compiler.ClassDefinition;
import gg.jte.runtime.ClassInfo;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ExtensionTemplateDescriptionTest {

@Test
void equality() {
var a1 = new ExtensionTemplateDescription(
new ClassDefinition("Stub", "kt"),
new ClassInfo("Message", "my.app")
);
var a2 = new ExtensionTemplateDescription(
new ClassDefinition("Stub", "kt"),
new ClassInfo("Message", "my.app")
);
var b = new ExtensionTemplateDescription(
new ClassDefinition("Stub", "kt"),
new ClassInfo("OtherMessage", "my.app")
);
var c = new ExtensionTemplateDescription(
new ClassDefinition("OtherStub", "kt"),
new ClassInfo("Message", "my.app")
);
var d = new ExtensionTemplateDescription(
null,
new ClassInfo("Message", "my.app")
);
var e = new ExtensionTemplateDescription(
new ClassDefinition("OtherStub", "kt"),
null
);

assertThat(a1).isEqualTo(a2);
assertThat(a1).hasSameHashCodeAs(a2);

assertThat(a1).isNotEqualTo(b);
assertThat(a1).doesNotHaveSameHashCodeAs(b);

assertThat(a1).isNotEqualTo(c);
assertThat(a1).doesNotHaveSameHashCodeAs(c);

assertThat(a1).isNotEqualTo(d);
assertThat(a1).doesNotHaveSameHashCodeAs(d);

assertThat(a1).isNotEqualTo(e);
assertThat(a1).doesNotHaveSameHashCodeAs(e);
}
}

0 comments on commit 7ad2e12

Please sign in to comment.