让我们试试这样:
public enum BADGES {SUPPORTER, ALPHA, BETA_OWNER, BOOSTER, ONE_MONTH, THREE_MONTH, SIX_MONTH, ONE_YEAR, TWO_YEAR};
public HashMap<BADGES, Integer> badgesToIntegerMap = /* initialize map where integers are mapped to badges */
public HashMap<Integer, BADGES> integerToBadgesMap = /* initialize map where badges are mapped to their integers */
public List<BADGES> getUserBadges(int userStatus) {
List<BADGES> retVal = new ArrayList<BADGES>();
// Start from highest one, being TWO_YEAR at time of writing
int currentBadge = 1 << 8; //
while (currentBadge > 0) { // These two should be replaced by iterating the badgesToIntegerMap's values
if (userStatus && currentBadge == 1) {
retVal.add(integerToBadgesMap.get(currentBadge));
}
currentBadge = currentBadge >> 1;
}
return retVal;
}
或者你知道,只需将这些整数传递给每个徽章的构造函数,就可以避免一次查找。