看高手代碼--從小case學大道理

今天看sun的HttpMessages.java文件,雖然文件很小,但是對我的啓發很大。
前面定義了大量的HTTP常量:

Java代碼 複製代碼
  1. ...   
  2. ...   
  3. private static final String STATUS_305 = "Use Proxy";   
  4. private static final String STATUS_307 = "Temporary Redirect";   
  5. private static final String STATUS_400 = "Bad Request";   
  6. private static final String STATUS_401 = "Unauthorized";   
  7. private static final String STATUS_402 = "Payment Required";   
  8. private static final String STATUS_403 = "Forbidden";   
  9. private static final String STATUS_404 = "Not Found";   
  10. private static final String STATUS_405 = "Method Not Allowed";   
  11. private static final String STATUS_406 = "Not Acceptable";   
  12. private static final String STATUS_407 = "Proxy Authentication Required";   
  13. private static final String STATUS_408 = "Request Timeout";   
  14. ...   
  15. ...  
    ...
    ...
    private static final String STATUS_305 = "Use Proxy";
    private static final String STATUS_307 = "Temporary Redirect";
    private static final String STATUS_400 = "Bad Request";
    private static final String STATUS_401 = "Unauthorized";
    private static final String STATUS_402 = "Payment Required";
    private static final String STATUS_403 = "Forbidden";
    private static final String STATUS_404 = "Not Found";
    private static final String STATUS_405 = "Method Not Allowed";
    private static final String STATUS_406 = "Not Acceptable";
    private static final String STATUS_407 = "Proxy Authentication Required";
    private static final String STATUS_408 = "Request Timeout";
    ...
    ...


也可以趁機從這裏查看HTTP返回值的意義^_^。然後定義了一個java.util.concurrent.ConcurrentHashMap的變量httpStatusCodeMappings,再往裏面添加剛纔定義的常量(這個操作放在了一個靜態塊裏),如:

Java代碼 複製代碼
  1. static {   
  2.         httpStatusCodeMappings.put("sc.100", STATUS_100);   
  3.         httpStatusCodeMappings.put("sc.101", STATUS_101);   
  4.         httpStatusCodeMappings.put("sc.200", STATUS_200);   
  5.         httpStatusCodeMappings.put("sc.201", STATUS_201);   
  6.         httpStatusCodeMappings.put("sc.202", STATUS_202);   
  7.         httpStatusCodeMappings.put("sc.203", STATUS_203);   
  8.   ...   
  9.   ...  
static {
        httpStatusCodeMappings.put("sc.100", STATUS_100);
        httpStatusCodeMappings.put("sc.101", STATUS_101);
        httpStatusCodeMappings.put("sc.200", STATUS_200);
        httpStatusCodeMappings.put("sc.201", STATUS_201);
        httpStatusCodeMappings.put("sc.202", STATUS_202);
        httpStatusCodeMappings.put("sc.203", STATUS_203);
  ...
  ...


最重要的就是這個方法了,先看代碼

Java代碼 複製代碼
  1. public static String getMessage(int status) {   
  2.   
  3.         // Return the status message for the most frequently used status   
  4.         // codes directly, without any lookup   
  5.         switch (status) {   
  6.             case 200return STATUS_200;   
  7.             case 302return STATUS_302;   
  8.             case 400return STATUS_400;   
  9.             case 404return STATUS_404;   
  10.     }   
  11.   
  12.         return httpStatusCodeMappings.get("sc."+ status);   
  13.     }  
public static String getMessage(int status) {

        // Return the status message for the most frequently used status
        // codes directly, without any lookup
        switch (status) {
            case 200: return STATUS_200;
            case 302: return STATUS_302;
            case 400: return STATUS_400;
            case 404: return STATUS_404;
	}

        return httpStatusCodeMappings.get("sc."+ status);
    }


這段代碼的用途很簡單,就是要返回狀態碼對應的Message,而這些這些消息都已經存放在httpStatusCodeMappings變量裏了,爲了提高訪問常用的幾個返回碼的Message,它直接用了一個靜態塊,而不去lookup那個map了,這就是高手!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章