{"id":1564,"date":"2015-10-12T11:25:10","date_gmt":"2015-10-12T03:25:10","guid":{"rendered":"https:\/\/cowmanchiang.me\/wp\/?p=1564"},"modified":"2023-10-31T15:43:12","modified_gmt":"2023-10-31T07:43:12","slug":"java-binary-numbers-in-enum-type","status":"publish","type":"post","link":"https:\/\/cowmanchiang.me\/wp\/?p=1564","title":{"rendered":"[Java] Binary numbers in enum type."},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">public enum Type {\n  \/** none *\/\n  NONE(0),\n  \/** ONE *\/\n  ONE(1),\n  \/** TWO *\/\n  TWO(2),\n  \/** FOUR *\/\n  FOUR(4),\n  \/** EIGHT *\/\n  EIGHT(8),\n  \/** SIXTEEN *\/\n  SIXTEEN(16);\n\n  private int key;\n  private static int[] binaryArray = { 0, 1, 2, 4, 8, 16 };\n\n  Type(int code) {\n    this.key = code;\n  }\n\n  public int getKey() {\n    return key;\n  }\n\n  public static void main(String[] args) {\n    System.out.println(findListByKey(60));\n  }\n\n  public static List&lt;Type&gt; findListByKey(int key) {\n    List&lt;Type&gt; matchList = new ArrayList&lt;&gt;();\n\n    List&lt;Integer&gt; binarySequence = getBinarySequence(key);\n\n    for (Integer code:binarySequence) {\n        matchList.add(findByKey(code));\n    }\n\n    return matchList;\n  }\n\n  public static Type findByKey(int key) {\n    Type match;\n    for (Type e : values()) {\n        if (e.getKey() == key) {\n            match = e;\n            return match;\n        }\n    }\n\n    return NONE;\n  }\n\n  private static List&lt;Integer&gt; getBinaryNumberSequence(int target){\n    List&lt;Integer&gt; result = new ArrayList&lt;&gt;();\n\n    boolean flag = true;\n    while (flag == true) {\n        int code = findCloseDigit(target);\n        target = target - code;\n        result.add(code);\n        if (target == 0) {\n            flag = false;\n        }\n    }\n\n    return result;\n  }\n\n  private static int findCloseDigit(int target) {\n    int result = 0;\n    for (int code:binaryArray) {\n        if (code &lt;= target) {\n            result = code;\n        } else {\n            break;\n        }\n    }\n    return result;\n  }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>public enum Type { \/** none *\/ NONE(0), \/** ONE *\/ ONE(1), \/** TWO *\/ TWO(2), \/** FOUR *\/ FOUR(4), \/** EIGHT *\/ EIGHT(8), \/** SIXTEEN *\/ SIXTEEN(16); private int key; private static int[] binaryArray = { 0, 1, 2, &hellip; <a href=\"https:\/\/cowmanchiang.me\/wp\/?p=1564\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-1564","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1564","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1564"}],"version-history":[{"count":1,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1564\/revisions"}],"predecessor-version":[{"id":1898,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1564\/revisions\/1898"}],"wp:attachment":[{"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cowmanchiang.me\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}