Define abstract methods in Java enum and write each behavior

Overview

This entry deals with defining abstract methods in Java enums and writing their behavior. (The one written in Effective Java 3rd edition is almost the same. Make a note of what you tried.)

Java enum

Java enums allow you to define methods as well as values.

Pattern that defines only the value

    enum Operator {
        PLUS, SUBTRACT, MULTIPLY, DIVIDE, NONE
    }

On the other hand, you can define a method for each value and write it as shown in the figure below.

    enum Operator {
        PLUS {
            @Override
            BigDecimal apply(@NonNull BigDecimal lhs, @NonNull BigDecimal rhs) {
                return lhs.add(rhs);
            }
        },
        SUBTRACT{
            @Override
            BigDecimal apply(@NonNull BigDecimal lhs, @NonNull BigDecimal rhs) {
                return lhs.subtract(rhs);
            }
        },
        MULTIPLY {
            @Override
            BigDecimal apply(@NonNull BigDecimal lhs, @NonNull BigDecimal rhs) {
                return lhs.multiply(rhs);
            }
        },
        DIVIDE {
            @Override
            BigDecimal apply(@NonNull BigDecimal lhs, @NonNull BigDecimal rhs) {
                return lhs.divide(rhs, BigDecimal.ROUND_UNNECESSARY);//;
            }
        },
        NONE {
            // allow null for rhs
            @Override
            BigDecimal apply(@NonNull BigDecimal lhs, BigDecimal rhs) {
                return lhs;
            }
        };

        abstract BigDecimal apply(BigDecimal lhs, BigDecimal rhs);
    }

In "abstract BigDecimal apply (BigDecimal lhs, BigDecimal rhs);" in enum, the method to be defined for all values is specified, and the method is overridden when each value is declared.

(Supplement) In the above source, "@NonNull" is one of annotations provided by lombok that implements Null check. is.

Example of use

If you define an enum in the above form, for example, the following method


    public synchronized BigDecimal pushEvalButton() {
        var v = new BigDecimal(sb.toString());
        switch(currentOperator) {
            case PLUS: {
                v = stack.add(getCurrentValue());
                break;
            }
            case SUBTRACT: {
                  v = stack.subtract(getCurrentValue());
                  break;
            }
            case MULTIPLY: {
                  v = stack.multiply(getCurrentValue());
                  break;
            }
            case DIVIDE: {
                  v = stack.divide(getCurrentValue(), BigDecimal.ROUND_UNNECESSARY);//
                  break;
            }
            case NONE: {
                return v;
            }
            default: {
                throw new RuntimeException("Not defined.");
            }
        }
        currentOperator = Operator.NONE;
        replaceBuffer(v.toPlainString());
        clearStack();
        return v;
    }

You can write clearly as follows.


    public synchronized BigDecimal pushEvalButton() {
        var v = new BigDecimal(sb.toString());

        if(Operator.NONE == currentOperator) {
            return v;
        }
        v = currentOperator.apply(stack, getCurrentValue());
        currentOperator = Operator.NONE;
        replaceBuffer(v.toPlainString());
        clearStack();
        return v;
    }

Since the content has moved to the enum method, it is natural that the code is reduced from the original method, but I felt that the following are merits. --I was able to expel the default in the switch --By using enum methods in other places, the processing is put together in one place.

Summary

In this entry, I explained how to define abstract methods in Java enum and write their behaviors with an example.

See [This commit] on GitHub (https://github.com/hrkt/commandline-calculator/commit/47e7b05075c2bb632b9bdabdf044ab3b5cefdeee) for an example of the difference above.

Recommended Posts

Define abstract methods in Java enum and write each behavior
Check static and public behavior in Java methods
Java abstract methods and classes
[Java] for Each and sorted in Lambda
JAVA learning history abstract classes and methods
Write a class in Kotlin and call it in Java
Reverse Enum constants from strings and values in Java
Use of Abstract Class and Interface properly in Java
[Java] Generics classes and generics methods
Write flyway callbacks in Java
Enum Strategy pattern in Java
Java methods and method overloads
Think about the differences between functions and methods (in Java)
Reproduce Java enum in C #
About abstract classes in java
Differences in default behavior of memory and CPU recognition on kubernetes (GKE) for each Java version
Create a high-performance enum with fields and methods like Java with JavaScript
Sample to read and write LibreOffice Calc fods file in Java 2021
About Java static and non-static methods
Encoding and Decoding example in Java
Studying Java 8 (StaticIF and Default methods)
Understanding equals and hashCode in Java
Java generics (defines classes and methods)
Hello world in Java and Gradle
Sample code to serialize and deserialize Java Enum enums and JSON in Jackson
A note on the differences between interfaces and abstract classes in Java
[Java] How to output and write files!
Java programming (classes and instances, main methods)
Arrylist and linked list difference in java
Program PDF headers and footers in Java
Learn Flyweight patterns and ConcurrentHashMap in Java
Java Direction in C ++ Design and Evolution
Java to C and C to Java in Android Studio
Reading and writing gzip files in Java
Difference between int and Integer in Java
Discrimination of Enums in Java 7 and above