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, 4, 8, 16 };
Type(int code) {
this.key = code;
}
public int getKey() {
return key;
}
public static void main(String[] args) {
System.out.println(findListByKey(60));
}
public static List<Type> findListByKey(int key) {
List<Type> matchList = new ArrayList<>();
List<Integer> binarySequence = getBinarySequence(key);
for (Integer code:binarySequence) {
matchList.add(findByKey(code));
}
return matchList;
}
public static Type findByKey(int key) {
Type match;
for (Type e : values()) {
if (e.getKey() == key) {
match = e;
return match;
}
}
return NONE;
}
private static List<Integer> getBinaryNumberSequence(int target){
List<Integer> result = new ArrayList<>();
boolean flag = true;
while (flag == true) {
int code = findCloseDigit(target);
target = target - code;
result.add(code);
if (target == 0) {
flag = false;
}
}
return result;
}
private static int findCloseDigit(int target) {
int result = 0;
for (int code:binaryArray) {
if (code <= target) {
result = code;
} else {
break;
}
}
return result;
}
}