How to error code representation using enum in java

How to error code representation using enum in java - Hello friend inabnomaniiyaha, In the article that you read this time with the title How to error code representation using enum in java, we have prepared this article well for you to read and take information in it. hopefully the contents of the post Artikel Code-java-example,what we write you can understand. Alright, happy reading.

Judul : How to error code representation using enum in java
link : How to error code representation using enum in java

Baca juga


How to error code representation using enum in java

How to error code representation using enum in java

Here is the solution using your Enum that I typically employ to deal with error codes as you have explained in your scenario.

import java.util.HashMap;

import java.util.Map;

public class EnumSample {


    public static enum LoginErrorCode {


        EMAIL_OR_PASSWORD_INCORRECT("101"), EMAIL_INCORRECT("102"), UNKNOWN_ERROR_CODE("---");


        private static Map<String, LoginErrorCode> codeToEnumMap;


        private final String code;


        LoginErrorCode(String code) {

            this.code = code;

        }


        public String getCode() {

            return code;

        }



        /**

         * Looks up enum based on code.  If code was not registered as enum, it returns UNKNOWN_ERROR_CODE

         * @param code

         * @return

         */

        public static LoginErrorCode fromCode(String code) {

            // Keep a hashmap of mapping between code and corresponding enum as a cache.  We need to initialize it only once

            if (codeToEnumMap == null) {

                codeToEnumMap = new HashMap<String, EnumSample.LoginErrorCode>();

                for (LoginErrorCode aEnum : LoginErrorCode.values()) {

                    codeToEnumMap.put(aEnum.getCode(), aEnum);

                }

            }


            LoginErrorCode enumForGivenCode = codeToEnumMap.get(code);

            if (enumForGivenCode == null) {

                enumForGivenCode = UNKNOWN_ERROR_CODE;

            }


            return enumForGivenCode;

        }

    }


    public static void main(String[] args) {


        System.out.println( LoginErrorCode.fromCode("101")); //Prints EMAIL_OR_PASSWORD_INCORRECT

        System.out.println( LoginErrorCode.fromCode("102")); //Prints EMAIL_INCORRECT

        System.out.println( LoginErrorCode.fromCode("999")); //Prints UNKWNOWN_ERROR_CODE

    }

}



That's the articleHow to error code representation using enum in java

That's it for the article How to error code representation using enum in java this time, hopefully can be useful for all of you. well, see you in another article post.

You are now reading the articleHow to error code representation using enum in java with link addresshttps://inabnonapudyawanabing.blogspot.com/2021/03/how-to-error-code-representation-using.html

0 Response to "How to error code representation using enum in java"

Post a Comment

Tips Tricks for Android Phone

Tips & Tricks for Android Phone is a free android app and Collection of Tips and Tricks related to using your android mobile device lik...