com.enofex.taikai.java.HashCodeAndEquals Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of taikai Show documentation
Show all versions of taikai Show documentation
Taikai is a wrapper around the awesome ArchUnit and provides a set of common rules for different technologies.
package com.enofex.taikai.java;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
final class HashCodeAndEquals {
private HashCodeAndEquals() {
}
static ArchCondition implementHashCodeAndEquals() {
return new ArchCondition<>("implement both equals() and hashCode()") {
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
boolean hasEquals = hasEquals(javaClass);
boolean hasHashCode = hasHashCode(javaClass);
if (hasEquals && !hasHashCode) {
events.add(SimpleConditionEvent.violated(javaClass,
"Class %s implements equals() but not hashCode()".formatted(
javaClass.getName())));
} else if (!hasEquals && hasHashCode) {
events.add(SimpleConditionEvent.violated(javaClass,
"Class %s implements hashCode() but not equals()".formatted(
javaClass.getName())));
}
}
private static boolean hasHashCode(JavaClass javaClass) {
return javaClass.getMethods().stream()
.anyMatch(method -> "hashCode".equals(method.getName()) &&
method.getRawParameterTypes().isEmpty());
}
private static boolean hasEquals(JavaClass javaClass) {
return javaClass.getMethods().stream()
.anyMatch(method -> "equals".equals(method.getName()) &&
method.getRawParameterTypes().size() == 1 &&
method.getRawParameterTypes().getFirst().getName().equals(Object.class.getName()));
}
};
}
}