0

I'm using enumeration to define all my constant variables.

Enum

public enum ApplicationStatus {
        Initiated(1), PendingVerification(2), NotEligible(3), Approved(4), SoftDenial(
                5), Deferred(6), Saved(7), Cancelled(8), Approved_NoSSN(9), PendingVerification_NoSSN(
                10), Approved_Archived(11);

        private final int value;

        private ApplicationStatus(int value) {
            this.value = value;
        }

        public int getStatusMasterId() {
            return value;
        }
    };

And I need to compare two integer values. What is the efficient way to compare the below integers?

Comparing Integers:-

public void applicationStatus(){
    String applicationStatus = null;
    String statusMasterId = "2";
    if(ApplicationStatus.PendingVerification.ordinal() == Integer.parseInt(statusMasterId)){
        applicationStatus = "Pending Verification";
    }
}

Suggestions Welcome!

Aishu
  • 1,310
  • 6
  • 28
  • 52

2 Answers2

1

You have two options two compare enums.

  1. Use built in compareTo method of java enum. See https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html#compareTo(E)
  2. Create your own compare method within enum. Like:

    public enum ApplicationStatus {
        Initiated(1), PendingVerification(2), NotEligible(3), Approved(4), 
        SoftDenial(5), Deferred(6), Saved(7), Cancelled(8), 
        Approved_NoSSN(9),PendingVerification_NoSSN(10), 
        Approved_Archeived(11);
    
        private final int value;
    
        private ApplicationStatus(int value) {
            this.value = value;
        }
    
        public int getStatusMasterId() {
            return value;
        }
        public boolean compare(int value) {
            return this.value==value;
        }
    };
    
Sam
  • 128
  • 1
  • 5
Jay Smith
  • 2,331
  • 3
  • 16
  • 27
  • 1
    Very good information. I think you have been skipping the part about one of the numbers coming in a string. Maybe the asker can adjust for that himself/herself. – Ole V.V. Apr 11 '17 at 06:40
1

I have a couple of suggestions. Feel free to discard if they are not right for you.

Since you say you need the ordinal number of the enum, you may go the other way:

    if (ApplicationStatus.values()[Integer.parseInt(statusMasterId)] == ApplicationStatus.PendingVerification) {
        // do your stuff
    }

Be aware that as in the question Integer.parseInt(statusMasterId) may throw a NumberFormatException.

Another suggestion, the compare method from Jay Smith’s answer may be taken a step further to accept a string argument:

public enum ApplicationStatus {
    Initiated(1), PendingVerification(2), NotEligible(3), Approved(4), 
    SoftDenial(5), Deferred(6), Saved(7), Cancelled(8), 
    Approved_NoSSN(9),PendingVerification_NoSSN(10), 
    Approved_Archeived(11);

    private final int value;
    // redundant representation of ordinal() as a string for comparison to other strings
    private final String ordinalAsString;

    private ApplicationStatus(int value) {
        this.value = value;
        ordinalAsString = String.valueOf(ordinal());
    }

    public int getStatusMasterId() {
        return value;
    }

    public boolean compare(String stringValue) {
        return this.ordinalAsString.equals(stringValue);
    }
}

There is no risk of a NumberFormatException, but you may consider the risk there is even worse: This will not recognize for example "+2" or "02" as representing the number 2. Whether you want to store the string value permanently in the enum object is questionable; if you are serious about wanting the comparison to be efficient, you do want to. Jay Smith’s code is using the value, I have changed it to using ordinal().

Here’s the test!

    String statusMasterId = "2";
    if (ApplicationStatus.values()[Integer.parseInt(statusMasterId)] == ApplicationStatus.NotEligible) {
        System.out.println("Not eligible");
    }
    if (ApplicationStatus.NotEligible.compare(statusMasterId)) {
        System.out.println("Still not eligible using the compare() method");
    }

This prints

Not eligible
Still not eligible using the compare() method

This is because ordinals start from 0, so NotEligible, with a value of 3, has ordinal 2, it’s not the same thing.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I'm calling like this, but it is returning false - if(ApplicationStatus.Initiated.compare(statusMasterId)){ applicationStatus = "Initiated"; System.out.println(applicationStatus); } – Aishu Apr 11 '17 at 07:05
  • Have u tested the compare() code... It is returning false – Aishu Apr 11 '17 at 07:10
  • V.V - This works fine...But I'm trying to use the compare() in my if condition. That is returning false..And that is my concern – Aishu Apr 11 '17 at 07:17
  • Thanks.It is working fine. I did a slight modification in this line. ordinalAsString = String.valueOf(ordinal() + 1); – Aishu Apr 11 '17 at 07:24
  • 1
    Very good. In that case I think you will want to name it `valueAsString` and assign `String.valueOf(value)` to it instead. – Ole V.V. Apr 11 '17 at 07:26