Skip to content

Commit

Permalink
Fix equalsAndHashCode generation (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobylynskyi committed Sep 24, 2019
1 parent 1bf1e35 commit 3e7f297
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/main/resources/templates/javaClassGraphqlType.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,21 @@ public class ${className} <#if implements?has_content>implements <#list implemen
if (this == obj) {
return true;
}
if (that == null || getClass() != obj.getClass()) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final ${className} that = (FormField) obj;
return <#list fields as field>Objects.equals(${field.name}, that.${field.name}) <#if field_has_next>
final ${className} that = (${className}) obj;
return <#list fields as field>Objects.equals(${field.name}, that.${field.name})<#if field_has_next>
&& </#if></#list>;
}

@Override
public int hashCode() {
return Objects.hash(
<#list fields as field>
${field.name}<#if field_has_next>, </#if>
</#list>;
<#if fields?has_content>
return Objects.hash(<#list fields as field>${field.name}<#if field_has_next>, </#if></#list>);
<#else>
return 0;
</#if>
}
</#if>
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ void generate_EqualsAndHashCode() throws Exception {
mappingConfig.setGenerateEqualsAndHashCode(true);
mappingConfig.setModelNameSuffix("TO");


generator.generate();

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
Expand All @@ -239,7 +238,10 @@ void generate_EqualsAndHashCode() throws Exception {
}
}


assertEquals(Utils.getFileContent("src/test/resources/expected-classes/EventPropertyTO_withEqualsAndHashCode.java.txt"),
Utils.getFileContent(Arrays.stream(files)
.filter(f -> f.getName().equals("EventPropertyTO.java"))
.map(File::getPath).findFirst().orElseThrow(FileNotFoundException::new)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.kobylynskyi.graphql.test1;

import java.util.*;

public class EventPropertyTO {

private Float floatVal;
private Boolean booleanVal;
private Integer intVal;
private String stringVal;
private Collection<EventPropertyTO> child;
private EventTO parent;

public EventPropertyTO() {
}

public EventPropertyTO(Float floatVal, Boolean booleanVal, Integer intVal, String stringVal, Collection<EventPropertyTO> child, EventTO parent) {
this.floatVal = floatVal;
this.booleanVal = booleanVal;
this.intVal = intVal;
this.stringVal = stringVal;
this.child = child;
this.parent = parent;
}

public Float getFloatVal() {
return floatVal;
}
public void setFloatVal(Float floatVal) {
this.floatVal = floatVal;
}

public Boolean getBooleanVal() {
return booleanVal;
}
public void setBooleanVal(Boolean booleanVal) {
this.booleanVal = booleanVal;
}

public Integer getIntVal() {
return intVal;
}
public void setIntVal(Integer intVal) {
this.intVal = intVal;
}

public String getStringVal() {
return stringVal;
}
public void setStringVal(String stringVal) {
this.stringVal = stringVal;
}

public Collection<EventPropertyTO> getChild() {
return child;
}
public void setChild(Collection<EventPropertyTO> child) {
this.child = child;
}

public EventTO getParent() {
return parent;
}
public void setParent(EventTO parent) {
this.parent = parent;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final EventPropertyTO that = (EventPropertyTO) obj;
return Objects.equals(floatVal, that.floatVal)
&& Objects.equals(booleanVal, that.booleanVal)
&& Objects.equals(intVal, that.intVal)
&& Objects.equals(stringVal, that.stringVal)
&& Objects.equals(child, that.child)
&& Objects.equals(parent, that.parent);
}

@Override
public int hashCode() {
return Objects.hash(floatVal, booleanVal, intVal, stringVal, child, parent);
}
}

0 comments on commit 3e7f297

Please sign in to comment.