[Java] Express Enum type without using Enum (enumeration) type

Introduction

In this article, I will express the code that behaves the same as the Enum (enumeration) type without using the Enum type. Java's Enum (enumeration) type is a type that keeps multiple constants together. A set of fixed constants such as blood type, gender, moon name, constellation, and various codes can be coded based on object-oriented thinking. In addition, you can use useful functions by default. This time, I will explain using the Trump mark (spades, hearts, diamonds, clubs) as an example.

When using Enum type

Suit.java


public enum Suit {
    SPADE("♠"), HEART("♡"), DIAMOND("♢"), CLUB("♣");

    private String symbol;
    
    private Suit(String symbol){
        this.symbol = symbol;
    }
    
    public String getSymbol(){
        return symbol;
    }
}

When not using Enum type

Suit.java


public class Suit implements Comparable<Suit>{
    public static final Suit SPADE = new Suit("SPADE", "♠");
    public static final Suit HEART = new Suit("HEART", "♡");
    public static final Suit DIAMOND = new Suit("DIAMOND", "♢");
    public static final Suit CLUB = new Suit("CLUB", "♣");
    
    private static final List<Suit> VALUES =
            Collections.unmodifiableList(
                    Arrays.asList(SPADE, HEART, DIAMOND, CLUB)
            );
    private final String name;
    private final String symbol;
    
    private Suit(String name, String symbol){
        this.name = name;
        this.symbol = symbol;
    }
    
    public String getSymbol(){
        return symbol;
    }
    
    @Override
    public int compareTo(Suit t){
        return Integer.valueOf(this.ordinal()).compareTo(t.ordinal());
    }
    
    public Class<? extends Suit> getDeclaringClass(){
        return this.getClass();
    }
    
    public String name(){
        return name;
    }
    
    public int ordinal(){
        return VALUES.indexOf(this);
    }
    
    @Override
    public String toString(){
        return name();
    }
    
    public static Suit valueOf(String name){
        for(Suit value : VALUES){
            if(value.name.equals(name)) return value;
        }
        throw new IllegalArgumentException();
    }
    
    public static List<Suit> values() {
        return VALUES;
    }
}

Commentary

Member variable VALUES

The List part is written like this.

private static final List<Suit> VALUES =
            Collections.unmodifiableList(
                    Arrays.asList(SPADE, HEART, DIAMOND, CLUB)
            );

In this part, the member variable VALUES is assigned an invariant list with four marks (Suit) as elements. I will explain each function.

  1. The Collections.unmodifiableList function returns an invariant list. Immutable lists, unlike variable lists like ArrayList, cannot change their values.
  2. The Arrays.asList function returns a List with arguments as elements. By using this function, you can assign the initial value to List. Although not used this time, the size (number of elements) of the return value List cannot be increased or decreased. This List is a fixed list.

Constructor arguments

When using Enum type and

private Suit(String symbol)

When not in use

private Suit(String name, String symbol)

The arguments of the constructor are different. The reason is that there is no way to get the variable names of the member variables used in the toString function and the valueOf function (values are SPADE, HEART, DIAMOND, CLUB of each character string) using the function. Therefore, it is necessary to keep it in the class via an argument.

Reference material

[New Refactoring-Safely Improve Existing Code- (OBJECT TECHNOLOGY SERIES)](Fixed link: http://amzn.asia/c5WxOUh)

Recommended Posts

[Java] Express Enum type without using Enum (enumeration) type
Enum (enumeration type)
[Java] Enumeration type
[Java] About enum type
Formatting an enum using formatter-maven-plugin (Java)
How to use Java enum type
[Java] enum
Map without using an array in java
Notes on operators using Java ~ String type ~
Java type conversion
[In-house study session] Java basics-execution without using IDE- (2017/07/06)
[Swift] Type type ~ Enumeration type ~
[JAVA] Stream type
Java Optional type
[Java] Convert DB code to code value using enum
Java double type
[Java beginner] println method without collection type specification
Sorting using java comparator
Java Enum utilization example
[Java] Data type ①-Basic type
Scraping practice using Java ②
[Swift] Shared enumeration type
Enum reverse map Java
[Java, Kotlin] Type Variance
Java class type field
Type determination in Java
Java study # 1 (typical type)
Scraping practice using Java ①
Express failure in Java
[Java] Date type conversion
I want to build Java Applet without using an IDE
Check the status of Java application without using monitoring tool
[Java8] Sort int type array in descending order using stream