-1

My app is sending request to APIs from some other parties. Every single parties have different return codes that I need to handle. The result is I need to handle thousands of different return code from those parties.

I am thinking about making a specific class that will hold constant to handle all of them. It will be like:

public static final Map<String, String>  RETURN_CODE_MAP = new HashMap<String, String>(){
    {
        // these mapping will be thousands of line
        put("BANK_A_000","SUCCESS");
        put("BANK_A_001","FAILED");
        put("BANK_A_002","UNKNOWN");
        put("BANK_B_SU","SUCCESS");
        put("BANK_B_FA","FAILED");
        put("BANK_B_UN","UNKNOWN");
        put("BANK_C_00077","SUCCESS");
        put("BANK_C_00088","FAILED");
        put("BANK_C_00099","UNKNOWN");
        put("E-COMMERCE_A_000","SUCCESS");
        put("E-COMMERCE_A_001","FAILED");
        put("E-COMMERCE_A_002","UNKNOWN");
        put("E-COMMERCE_B_000SU","SUCCESS");
        put("E-COMMERCE_B_000FA","FAILED");
        put("E-COMMERCE_B_000UN","UNKNOWN");
        put("E-COMMERCE_C_00077","SUCCESS");
        put("E-COMMERCE_C_00088","FAILED");
        put("E-COMMERCE_C_00099","UNKNOWN");
    }
};

The list of the return code will be thousands of them. Are there going to be a performance issue? Can you guys tell me the right way to handle this kind of case? Thank you all

  • 1
    I would store those values in a properties file, and read it at startup to load the Map. Alternatively, use a database (if they change a lot). This is a nightmare to maintain if you hardcode it in constants. – Elliott Frisch Apr 29 '20 at 01:58
  • 1
    On a side note, you should never use double brace initializers. If you put 100 values using DBI, then there will be 100 anonymous classes created. It's a lot of overheard for fancy syntax. – Harshal Parekh Apr 29 '20 at 02:02
  • That doesn't sound correct, @HarshalParekh . Do you have a source for that statement? – markspace Apr 29 '20 at 02:07
  • @markspace, https://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization – Harshal Parekh Apr 29 '20 at 02:08
  • I'm not able to reproduce your claim, and I don't think that link says what you think it says. In my testing, a single anonymous class handles the initialization, not "hundreds." (I'm not arguing for DBI, it looks clumsy, but we shouldn't tell mistruths about it either.) @HarshalParekh – markspace Apr 29 '20 at 02:18
  • @markspace, _And yes, there were 1000 .class files generated by compiling the Test1 double brace initialization test program._ This is from the link. – Harshal Parekh Apr 29 '20 at 02:22
  • 2
    Again, you misread it. The person is saying they had 1000s of such initializers throughout the code base. That's different from what you said. You said "if you put 100 values using DBI" and that's not right. It's one anonymous class per initializer, not per line or per method call. – markspace Apr 29 '20 at 02:24

1 Answers1

3

I would suggest using an Enum per party. It allows you to store additional information easier than a map and if used right saves you some performance that could get wasted by string comparison (for example map.get(x).equals("SUCCESS")).

This could look like this:

ResponseStatus.java

public enum ResponseStatus {
    SUCCESS,
    FAILED,
    UNKNOWN;
}

BankA.java

import static my.pkg.ResponseStatus;


public enum BankA {

    C_000("000", SUCCESS),
    C_001("001", FAILED),
    C_002("002", UNKNOWN),
    // And so on ...

    private final ResponseStatus status;
    private final int hashCode;

    private BankA(String code, ResponseStatus status) {
        this.status = status;
        this.hashCode = code.hashCode();
    }

    public ResponseStatus getStatus() {
        return this.status;
    }

    public static BankA byStaus(String status) {
        BankA[] values = values();
        int hash = status.hashCode();
        for (int n = 0; n < values.length; n++) {
            BankA value = values[n];
            if (value.hashCode == hash) return value;
        }

        return null; // No entry found by that code
    }

}

Note, that in byStatus I'm comparing the hash codes instead of the strings themselves which is faster than comparing hundreds of strings.

In the end your final check could look like this:

BankA status = BankA.byStatus(response);

if (status != null && status.getStatus() == ResponseStatus.SUCCESS) {
    // Do something
}
MysteriousPerson
  • 670
  • 7
  • 21