vendredi 18 septembre 2015

Map two related enums?

I have two enums which are related:

Enum1:

public enum HttpStatusCode {

    ACCEPTED(202),
    OK(200),
    CREATED(201),
    BAD_REQUEST(400),
    NOT_FOUND(404),
    METHOD_NOT_ALLOWED(405),
    REQUEST_TIMEOUT (408),
    FORBIDDEN(403),
    CONFLICT(409),
    INTERNAL_SERVER_ERROR(500),
    NOT_IMPLEMENTED(501);

    private int httpStatusCode;

    private HttpStatusCode(int name) {
        this.httpStatusCode = name;
    }

    public int getHttpStatusCode() {
        return httpStatusCode;
    }
}

Enum2:

public enum ProtocolStatusCode {

    ACCEPTED(1000),
    OK(2000),
    CREATED(2001),
    BAD_REQUEST(4000),
    NOT_FOUND(4004),
    OPERATION_NOT_ALLOWED(4005),
    REQUEST_TIMEOUT(4008),
    SUBSCRIPTION_CREATOR_HAS_NO_PRIVILEGE(4101),

private int protocolStatusCode;

    private ProtocolStatusCode(int protocolStatusCode) {
        this.protocolStatusCode = protocolStatusCode;
    }

    public int getStatusCode() {
        return protocolStatusCode;
    }
}

These two enums values are related in a mapping, for example

Protocol status code 2000 (OK) has mapping with 200 (OK).

So in my code I will get the ProtocolStatusCode (2000) and corresponding to that I will need HttpStatusCode (200).

I was thinking of maintaing the ProtocolStatusCode enum as

ACCEPTED(1000, 202),
    OK(2000, 200)

So like this when I get 2000, I will reverse lookup the enum to get OK and then call a getter to get the second value (200) related to 2000.

Any better approach ??



from Newest questions tagged java - Stack Overflow http://ift.tt/1Qm8oly
via IFTTT

Aucun commentaire:

Enregistrer un commentaire