0

I have a variable like that in my Go stuff and trying to do it as same as in Java;

var (
   STATUS = map[int]string{
      200: "OK",
      201: "Created",
      202: "Accepted",
      304: "Not Modified",
      400: "Bad Request",
      401: "Unauthorized",
      403: "Forbidden",
      404: "Not Found",
      405: "Resource Not Allowed",
      406: "Not Acceptable",
      409: "Conflict",
      412: "Precondition Failed",
      415: "Bad Content Type",
      416: "Requested Range Not Satisfiable",
      417: "Expectation Failed",
      500: "Internal Server Error",
   }
)

I tried to use HashMap or other array stuff but couldn't cos it would be a property of Response class and must be defined at the beginning like;

package http;

class Response {
    // here define filling it e.g STATUS = new Array(200, "OK", ...) etc..
    ... STATUS ... 
}

Yes, I can fill it in constructor using HashMap but then I can't get e.g "OK" like this: String status = STATUS[200].

Kerem
  • 11,377
  • 5
  • 59
  • 58
  • Java has no way to initialize an associative array directly. You have to use a loop or something similar. http://stackoverflow.com/questions/24922011/add-multiple-string-variables-to-arraylist/24922034#24922034 – markspace Aug 18 '16 at 23:44
  • The best structure for this would be an `enum` mapping ints to strings. Here this is an example: http://stackoverflow.com/questions/10661775/java-custom-enum-value-to-enum – Oleg Sklyar Aug 18 '16 at 23:48
  • Well, `enum`s are OK, but often unwieldy. I think the best structure would be an external file so you could easily configure new (or deprecated) return codes without recompiling. That precludes `enum`s. – markspace Aug 18 '16 at 23:56

2 Answers2

2

An enum would suit best:

public enum Response {
  OK(200, "OK"), 
  Created(201, "Created"),
  NotFound(404, "Not found");

  private final int _code;
  private final String _message;

  Response(int code, String message) {
    _code = code;
    _message = message;
  }

  public int code() {
    return _code;
  }

  public String message() {
    return _message;
  }
}

Additional benefit of an enum is that you would operate with comprehensible constant names in code, such as Response.NotFound rather than numerical codes. If you need to really get a value by code, just add a static method to resolve the enum instance.

Oleg Sklyar
  • 9,834
  • 6
  • 39
  • 62
1

You can use static class initializer and use a HashMap, like u tried.

class Response
{
    public static final Map<Integer,String> STATUS;

    static{
        STATUS=new HashMap<>();
        STATUS.put(200,"OK");
        STATUS.put(201,"Created");
        // ...
    }
}

Example usage:

Response.STATUS.get(200); // will return "OK"



If you want, you can even make it unmodifiable with Collection.unmodifiableMap(). That is how I would do it. So the static class initializer from above would now look like this:
static{
    HashMap<Integer,String> statusValues=new HashMap<>();
    statusValues.put(200,"OK");
    statusValues.put(201,"Created");
    // ...

    STATUS=Collections.unmodifiableMap(statusValues);
}
GregorMohorko
  • 2,739
  • 2
  • 22
  • 33