0

I can't find an answer to this question anywhere so I'm hoping someone can help me out. I'm expecting that what I am asking is not possible, but I wanted to confirm. First, an enum example...

public enum StatusCode {

    SUCCESS(0), GENERAL_ERROR(999), CONNECTION_TIMEOUT_ERROR(1337);

    private int statusCode;

    private StatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public int getStatusCode() {
        return statusCode;
    }
}

As you can see, I am using this enum to force specific status codes. My question is this: Is there a way that I can reference StatusCode.SUCCESS and have it return the int value associated with it? Rather than get into too much detail about what I would like to do, take this example:

public String getStatusMessage(int statusCode) {
    // Map<Integer, String> that contains status messages
    // return String for key statusCode
}

In this example, the syntax for calling this method is getStatusMessage(StatusCode.SUCCESS.getStatusCode()).

Is there a way to shorten this to getStatusMessage(StatusCode.SUCCESS)?

I think the second way looks much cleaner and would make my code much more readable. Any ideas? Any help is much appreciated. Thanks in advance!

4
  • 2
    Why can't you just use public String getStatusMessage(StatusCode status)? Commented Mar 7, 2012 at 16:00
  • Why don't you want to have a Map<StatusCode, String> instead? Commented Mar 7, 2012 at 16:00
  • Is there a reason you don't want to use public String getStatusMessage(StatusCode statusCode)? Commented Mar 7, 2012 at 16:00
  • I just meant that as an example. For instance, I may also want to use a setter that takes int statusCode. Same problem there, I will have to use StatusCode.SUCCESS.getStatusCode() Commented Mar 7, 2012 at 16:34

2 Answers 2

8

You mean like this?

public String getStatusMessage(StatusCode code) {
    int status = code.getStatusCode();
    String message = ...do stuff to get message :)
    return message;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm, I should have thought of this :) StatusCode was originally a set of static ints. When I converted it to an enum, I didn't consider changing some of the code around it. Thanks for the suggestion!
1

Luckily for you, EnumMap exists just for that situation.

    private static final Map<StatusCode, String> mapMessage = 
                         new EnumMap<>(StatusCode.class);
    mapMessage.put(SUCCESS, "Success.");
    ...

You don't even need the method getStatusMessage, just call map.getMessage(SUCCESS).

However maybe you would be better off adding a String message field within StatusMessage and calling the constructors like SUCCESS(0, "Success") and then adding a getter for the message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.