Do you still write those special if statements for enums like myEnum.equals(myEnum.TEST) ? If yes, I might be able to share with you a small tip how to avoid it! It will be very beneficial for you because of your code might express itself without a word! Moreover, you might avoid those imports of your enum fields in every class where the comparison is going to be defined by if statement. It will say you a lot of time in case of changing name of enum field.
For instance, for the example defined below, only what you need to do in your code is -> if (operation.isCount()) {...} instead of if (operations.equals(Operation.COUNTA)) {...}
Check this post on Medium.com -> Use enum utility methods in Java ! Avoid equals at all costs!
import org.apache.commons.lang3.EnumUtils; public enum Operation { COUNTA, SUM,
NA; public static Operation parse(String input) { Operation operation = EnumUtils.getEnum(Operation.class, input); if (operation == null) { return NA; } return operation; } public boolean isCount() { return this == COUNTA; } public boolean isSum() { return this == SUM; } public boolean isCountOrSum() { return this == COUNTA || this == SUM; } }
Enum helper method - avoid equals
2018-04-13T21:14:00+01:00
GarciaPL
Java|Java 8|