映射两个相关的枚举?

时间:2015-09-18 09:18:53

标签: java enums

我有两个相关的枚举:

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)

private int protocolStatusCode;

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

    public int getStatusCode() {
        return protocolStatusCode;
    }
}

这两个枚举值在映射中相关,例如

协议状态代码2000(OK)已映射为200(OK)。

因此,在我的代码中,我将获得ProtocolStatusCode(2000)并且对应于我将需要HttpStatusCode(200)。

我在考虑维护ProtocolStatusCode枚举为

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

因此,当我得到2000时,我会反向查找枚举以获得OK,然后调用getter来获取与2000相关的第二个值(200)。

有更好的方法吗?

3 个答案:

答案 0 :(得分:3)

你可以在第二个枚举中添加一个字段,这是第一个枚举的类型。然后,您不必搜索第一个枚举数组以找到正确的对象。类似的东西:

enum A{
    A_FOO(1),
    A_BAR(2);
    private  int code;

    private A(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }
}

enum B{
    B_FOO(1000,A_FOO),
    B_BAR(2000,A_BAR);

    private int code;
    private A status;

    private B(int code, A status) {
        this.code = code;
        this.status = status;
    }

    public int getCode() {
        return code;
    }

    public A getStatus() {
        return status;
    }
}

因此,如果B enum中的代码是唯一的,您可以实现类似getBbyCode()的方法,然后您也有B.A

答案 1 :(得分:1)

取决于您是想单向还是双向。

一个简单的机制是在枚举中保存等价物并在构造函数中指定它。

public enum HttpStatusCode {

    ACCEPTED(202, ProtocolStatusCode.ACCEPTED),
    OK(200, ProtocolStatusCode.OK),
    CREATED(201,ProtocolStatusCode.CREATED);

    private int httpStatusCode;
    private final ProtocolStatusCode protocolEquivalent;

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

    public int getHttpStatusCode() {
        return httpStatusCode;
    }
}

或者 - 您可以构建两个地图:

static final Map<HttpStatusCode, ProtocolStatusCode> protocolStatus = new EnumMap(HttpStatusCode.class);
static final Map<ProtocolStatusCode, HttpStatusCode> httpStatus = new EnumMap(ProtocolStatusCode.class);

private static void equivalent(HttpStatusCode http, ProtocolStatusCode protocol) {
    protocolStatus.put(http, protocol);
    httpStatus.put(protocol, http);
}

static {
    equivalent(HttpStatusCode.ACCEPTED, ProtocolStatusCode.ACCEPTED);
    equivalent(HttpStatusCode.OK, ProtocolStatusCode.OK);
    // ...
}

答案 2 :(得分:0)

您的计划逻辑符合您的要求。

  

协议状态代码2000(OK)已映射为200(OK)。

这意味着在面向对象编程中需要一个has-a关系。

public enum ProtocolStatusCode {


    OK(2000, HttpStatusCode.OK);//so goes others


    private int protocolStatusCode;
    private HttpStatusCode httpStatusCode;

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

    public HttpStatusCode getHttpStatusCode(){//new
        return this.httpStatusCode;
    }

    public int getHttpStatusCodeVal(){//new method for lookup
        return this.getHttpStatusCode().getHttpStatusCode();
    }

    public int getStatusCode() {
        return protocolStatusCode;
    }


}

代码可能有拼写错误,因为我在网上输入原始内容。 :)