List of usage examples for com.google.common.collect Sets complementOf
public static <E extends Enum<E>> EnumSet<E> complementOf(Collection<E> collection, Class<E> type)
From source file:se.toxbee.sleepfighter.persist.PersistenceManager.java
/** * "Joins" in challenge set into list of alarms. * * @param alarms list of alarms./*ww w. ja v a2 s. c o m*/ * @param challengeSetLookup the lookup challenge set id -> index in list of alarms. */ private void fetchJoinChallenge(List<Alarm> alarms, Map<Integer, Integer> challengeSetLookup) { OrmHelper helper = this.getHelper(); // 1) Read all sets and set them. List<ChallengeConfigSet> challengeSetList = this.queryInIds(helper.getChallengeConfigSetDao(), ChallengeConfigSet.ID_COLUMN, challengeSetLookup); for (ChallengeConfigSet challengeSet : challengeSetList) { // Bind challenge config set to alarm. int alarmIndex = challengeSetLookup.get(challengeSet.getId()); Alarm alarm = alarms.get(alarmIndex); challengeSet.setMessageBus(alarm.getMessageBus()); alarm.setChallenges(challengeSet); } // 2) Read all challenge config:s and set to each set. Map<Integer, ChallengeConfig> challengeConfigLookup = Maps.newHashMap(); PersistenceExceptionDao<ChallengeConfig, Integer> configDao = helper.getChallengeConfigDao(); List<ChallengeConfig> challengeConfigList = this.queryInIds(configDao, ChallengeConfig.SET_FOREIGN_COLUMN, challengeSetLookup); for (ChallengeConfig challengeConfig : challengeConfigList) { // Bind challenge config to set. int alarmIndex = challengeSetLookup.get(challengeConfig.getSetId()); alarms.get(alarmIndex).getChallengeSet().putChallenge(challengeConfig); // Add to challenge config lookup. challengeConfigLookup.put(challengeConfig.getId(), challengeConfig); } // 3) Sanity fix. Find any missing ChallengeType:s and add them. for (ChallengeConfigSet challengeSet : challengeSetList) { Set<ChallengeType> missingTypes = Sets.complementOf(challengeSet.getDefinedTypes(), ChallengeType.class); for (ChallengeType type : missingTypes) { ChallengeConfig config = new ChallengeConfig(type, false); config.setFetchedSetId(challengeSet.getId()); configDao.create(config); challengeSet.putChallenge(config); } } // 3) Finally read all parameters and set to each config. List<ChallengeParam> challengeParamList = this.queryInIds(helper.getChallengeParamDao(), ChallengeParam.CHALLENGE_ID_COLUMN, challengeConfigLookup); for (ChallengeParam param : challengeParamList) { challengeConfigLookup.get(param.getId()).setFetched(param); } }