Created
July 21, 2020 09:46
-
-
Save onero/54f61293d9775deb8ca3b71ecc4e1164 to your computer and use it in GitHub Desktop.
Equals Template for IntelliJ using instanceof, instead of getClass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #parse("equalsHelper.vm") | |
| public boolean equals(## | |
| #if ($settings.generateFinalParameters) | |
| final ## | |
| #end | |
| Object $paramName){ | |
| #addEqualsPrologue() | |
| #addClassInstance() | |
| return ## | |
| #set($i = 0) | |
| #foreach($field in $fields) | |
| #if ($i > 0) | |
| && | |
| #end | |
| #set($i = $i + 1) | |
| #if ($field.primitive) | |
| #if ($field.double || $field.float) | |
| #addDoubleFieldComparisonConditionDirect($field) ## | |
| #else | |
| #addPrimitiveFieldComparisonConditionDirect($field) ## | |
| #end | |
| #elseif ($field.enum) | |
| #addPrimitiveFieldComparisonConditionDirect($field) ## | |
| #elseif ($field.array) | |
| java.util.Arrays.equals($field.accessor, ${classInstanceName}.$field.accessor)## | |
| #elseif ($field.notNull) | |
| ${field.accessor}.equals(${classInstanceName}.$field.accessor)## | |
| #else | |
| java.util.Objects.equals($field.accessor, ${classInstanceName}.$field.accessor)## | |
| #end | |
| #end | |
| ; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am as of Java 21 using the following template, that copies/overwrites some of the stuff from the IntelliJ baked-in template:
Example output (null-check is superfluous because of the magic of
instanceof):