instructions

This article only describes scenarios where policy enumerations can be used. Provide additional implementation ideas.

State of the circulation

In the development process, it is inevitable to encounter the flow between states, how to facilitate maintenance and can know the flow between states at a glance? Such as:As you can see from the figure above, we have state groups: the contents of the yellow boxes; Action group: Connect the contents on the line. You can wrap it with a state enumeration

package com.strategy.enums;

/** * Document status */
public enum StatusEnum {

    /** has been reported */
    HAD_CREATE(1."已新建"),

    /** has been reported */
    HAD_SUBMIT(2."Reported"),/** approved "*/
    HAD_AUDIT(3."Reviewed"),/** has been returned */
    HAD_BACK(4."Returned"),

    /** rejected */
    HAD_REJECT(5."Rejected."),

    /** Already generated settlement */
    HAD_USE(6."Generated settlement"),

    /** Closed */
    HAD_CLOSE(7."Closed");
    
    /** Enumeration value */
    private int index;

    /** Enumerate description */
    private String name;

    StatusEnum(int index, String name) {
        this.index = index;
        this.name = name;
    }

    public int getIndex(a) {
        return index;
    }

    public String getName(a) {
        returnname; }}Copy the code

How to transform this into a policy enumeration, plus operations (methods) and bindings (overrides).

package com.strategy.enums;

/** * Document status */
public enum StatusEnum {

    /** has been reported */
    HAD_CREATE(1."已新建") {
        @Override
        public StatusEnum create(a) {
            return HAD_CREATE;
        }
        @Override
        public StatusEnum save(a) {
            return HAD_CREATE;
        }
        @Override
        public StatusEnum submit(a) {
            return HAD_SUBMIT;
        }
        @Override
        public StatusEnum close(a) {
            returnHAD_CLOSE; }},/** has been reported */
    HAD_SUBMIT(2."Reported") {
        @Override
        public StatusEnum pass(a) {
            return HAD_AUDIT;
        }
        @Override
        public StatusEnum back(a) {
            return HAD_BACK;
        }
        @Override
        public StatusEnum reject(a) {
            returnHAD_REJECT; }},/** approved "*/
    HAD_AUDIT(3."Reviewed") {
        @Override
        public StatusEnum used(a) {
            returnHAD_USE; }},/** has been returned */
    HAD_BACK(4."Returned") {
        @Override
        public StatusEnum save(a) {
            return HAD_BACK;
        }
        @Override
        public StatusEnum submit(a) {
            return HAD_SUBMIT;
        }
        @Override
        public StatusEnum close(a) {
            returnHAD_CLOSE; }},/** rejected */
    HAD_REJECT(5."Rejected.") {},/** * already used */
    HAD_USE(6."In use") {},/** Closed */
    HAD_CLOSE(7."Closed") {};/** Enumeration value */
    private int index;

    /** Enumerate description */
    private String name;

    / * * * / creation
    public StatusEnum create(a) {
        throw new RuntimeException("Current state" + this.getName() + "Create operation not allowed");
    }

    Staging / * * * /
    public StatusEnum save(a) {
        throw new RuntimeException("Current state" + this.getName() + "The [temporary] operation is not allowed");
    }

    / * * to * /
    public StatusEnum submit(a) {
        throw new RuntimeException("Current state" + this.getName() + "The [report] operation is not allowed");
    }

    / * * by * /
    public StatusEnum pass(a) {
        throw new RuntimeException("Current state" + this.getName() + "Not allowed to perform [pass] operation");
    }

    / * * * / rejection
    public StatusEnum back(a) {
        throw new RuntimeException("Current state" + this.getName() + "The [reject] operation is not allowed to be performed");
    }

    / refuses to * * * /
    public StatusEnum reject(a) {
        throw new RuntimeException("Current state" + this.getName() + The [reject] operation is not allowed to be performed.);
    }

    /** 使用 */
    public StatusEnum used(a) {
        throw new RuntimeException("Current state" + this.getName() + "The [use] operation is not allowed");
    }

    / * * closed * /
    public StatusEnum close(a) {
        throw new RuntimeException("Current state" + this.getName() + "The [close] operation is not allowed");
    }

    /** the key method used */
    public static StatusEnum valueOf(Integer index) {
        if (index == null) {
            return null;
        }
        for (StatusEnum modeEnum : StatusEnum.values()) {
            if (index == modeEnum.getIndex()) {
                returnmodeEnum; }}return null;
    }
    
    StatusEnum(int index, String name) {
        this.index = index;
        this.name = name;
    }

    public int getIndex(a) {
        return index;
    }

    public String getName(a) {
        returnname; }}Copy the code

Above, the policy enumeration is implemented, and a class implements the complete state flow of the flowchart. The flowchart is ready, so how do you use it? It’s simple. Just one sentence. When we modify the state of the data, we first need to get the original state of the data from the library, already know what our current request action [button, method] is. So now that we have the two determinants, we can operate

package com.strategy.enums;

import java.util.ArrayList;
import java.util.List;

/** * Document status test class */
public class StatusEnumMainTest {

    public static void main(String[] args) {
        new StatusEnumMainTest().test();
    }

    private void test(a) {
        // Suppose we get the state of the changed data from the database
        List<StatusEnumTest> list = getInitList();
        // Create data to report (successful)
        StatusEnumTest statusEnumTest1 = getStatusEnumTest(1L, list);
        System.out.println("Data before modification:" + statusEnumTest1.toString());
        statusEnumTest1.setStatus(StatusEnum.valueOf(statusEnumTest1.getStatus()).submit().getIndex());
        System.out.println("Revised data:" + statusEnumTest1.toString());
        // Closed data for modification (failed)
        StatusEnumTest statusEnumTest7 = getStatusEnumTest(7L, list);
        System.out.println("Data before closing:" + statusEnumTest7.toString());
        statusEnumTest1.setStatus(StatusEnum.valueOf(statusEnumTest7.getStatus()).save().getIndex());
        System.out.println("Data after closing:" + statusEnumTest7.toString());
    }

    private StatusEnumTest getStatusEnumTest(Long id, List<StatusEnumTest> list) {
        for (StatusEnumTest data : list) {
            if (id.equals(data.getId())) {
                returndata; }}return null;
    }

    private List<StatusEnumTest> getInitList(a) {
        List<StatusEnumTest> list = new ArrayList<StatusEnumTest>();
        list.add(new StatusEnumTest(1L.1."This is new data."));
        list.add(new StatusEnumTest(2L.2."This is the reported data."));
        list.add(new StatusEnumTest(3L.3."This is the audited data."));
        list.add(new StatusEnumTest(4L.4."This is returned data."));
        list.add(new StatusEnumTest(5L.5."This is rejected data"));
        list.add(new StatusEnumTest(6L.6."This is the used data."));
        list.add(new StatusEnumTest(7L.7."This is closed data."));
        return list;
    }

    private class StatusEnumTest {
        private Long id;
        private Integer status;
        private String other;

        public StatusEnumTest(Long id, Integer status, String other) {
            this.id = id;
            this.status = status;
            this.other = other;
        }

        public Long getId(a) {
            return id;
        }

        public Integer getStatus(a) {
            return status;
        }

        public void setStatus(Integer status) {
            this.status = status;
        }

        public String getOther(a) {
            return other;
        }

        @Override
        public String toString(a) {
            return "StatusEnumTest{" + "id=" + id + ", status=" + status + ", other='" + other + '\' ' + '} '; }}}Copy the code

The result is as follows

StatusEnumTest{id=1, status=1, statusName= new, other=' new '} StatusEnumTest{id=1, status=2, statusName= reported, other=' new data '} StatusEnumTest {id = 7, status = 7, statusName = closed, other = 'this is a closed data'} Java lang. RuntimeException: Current state [closed] is not allowed to perform operations at the staging 】 【 strategy. Enums. StatusEnum. Save the at (StatusEnum. Java: 100) com.strategy.enums.StatusEnumMainTest.test(StatusEnumMainTest.java:26) at com.strategy.enums.StatusEnumMainTest.main(StatusEnumMainTest.java:12)Copy the code

This allows us to use code to control the flow of state. You can control the flow and get to the next state, killing two birds with one stone.