Skip to content

Commit

Permalink
SONAR-10313 remove useless RuleActivationContext#rulesByKey
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb committed Feb 8, 2018
1 parent af9cd8b commit 1496d3f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
Expand Up @@ -113,7 +113,7 @@ private List<ActiveRuleChange> removeParent(DbSession dbSession, QProfileDto pro
changes.addAll(ruleActivator.deactivate(dbSession, context, activeRule.getRuleId(), true));

} else if (ActiveRuleDto.OVERRIDES.equals(activeRule.getInheritance())) {
context.reset(activeRule.getRuleKey());
context.reset(activeRule.getRuleId());
activeRule.setInheritance(null);
activeRule.setUpdatedAt(system2.now());
db.activeRuleDao().update(dbSession, activeRule);
Expand Down
Expand Up @@ -65,7 +65,6 @@ class RuleActivationContext {
private final List<QProfileDto> builtInAliases = new ArrayList<>();

// the rules
private final Map<RuleKey, RuleWrapper> rulesByKey;
private final Map<Integer, RuleWrapper> rulesById;
private final Map<ActiveRuleKey, ActiveRuleWrapper> activeRulesByKey;

Expand All @@ -85,12 +84,10 @@ private RuleActivationContext(Builder builder) {
this.date = builder.date;

// rules
this.rulesByKey = Maps.newHashMapWithExpectedSize(builder.rules.size());
this.rulesById = Maps.newHashMapWithExpectedSize(builder.rules.size());
ListMultimap<Integer, RuleParamDto> paramsByRuleId = builder.ruleParams.stream().collect(index(RuleParamDto::getRuleId));
for (RuleDefinitionDto rule : builder.rules) {
RuleWrapper wrapper = new RuleWrapper(rule, paramsByRuleId.get(rule.getId()));
rulesByKey.put(rule.getKey(), wrapper);
rulesById.put(rule.getId(), wrapper);
}

Expand Down Expand Up @@ -169,19 +166,9 @@ Collection<QProfileDto> getChildProfiles() {
.collect(Collectors.toList());
}

/**
* Resets cursor to base profile and selects the rule with specified key.
*/
void reset(RuleKey ruleKey) {
this.cascading = false;
doSwitch(this.baseProfile, this.baseRulesProfile, ruleKey);
}

public void reset(Integer ruleId) {
public void reset(int ruleId) {
this.cascading = false;
RuleWrapper ruleWrapper = rulesById.get(ruleId);
checkRequest(ruleWrapper != null, "Rule not found: %s", ruleId);
doSwitch(this.baseProfile, this.baseRulesProfile, ruleWrapper.get().getKey());
doSwitch(this.baseProfile, this.baseRulesProfile, ruleId);
}

/**
Expand All @@ -190,20 +177,21 @@ public void reset(Integer ruleId) {
void switchToChild(QProfileDto to) {
checkState(!to.isBuiltIn());
requireNonNull(this.currentRule, "can not switch profile if rule is not set");
RuleKey ruleKey = this.currentRule.get().getKey();
RuleDefinitionDto rule = this.currentRule.get();

QProfileDto qp = requireNonNull(this.profilesByUuid.get(to.getKee()), () -> "No profile with uuid " + to.getKee());
RulesProfileDto rulesProfile = RulesProfileDto.from(qp);

this.cascading = true;
doSwitch(qp, rulesProfile, ruleKey);
doSwitch(qp, rulesProfile, rule.getId());
}

private void doSwitch(@Nullable QProfileDto qp, RulesProfileDto rulesProfile, RuleKey ruleKey) {
this.currentRule = rulesByKey.get(ruleKey);
checkRequest(this.currentRule != null, "Rule not found: %s", ruleKey);
private void doSwitch(@Nullable QProfileDto qp, RulesProfileDto rulesProfile, int ruleId) {
this.currentRule = rulesById.get(ruleId);
checkRequest(this.currentRule != null, "Rule not found: %s", ruleId);
RuleKey ruleKey = currentRule.get().getKey();
checkRequest(rulesProfile.getLanguage().equals(currentRule.get().getLanguage()),
"%s rule %s cannot be activated on %s profile %s", currentRule.get().getLanguage(), currentRule.get().getKey(), rulesProfile.getLanguage(), rulesProfile.getName());
"%s rule %s cannot be activated on %s profile %s", currentRule.get().getLanguage(), ruleKey, rulesProfile.getLanguage(), rulesProfile.getName());
this.currentRulesProfile = rulesProfile;
this.currentProfile = qp;
this.currentActiveRule = this.activeRulesByKey.get(ActiveRuleKey.of(rulesProfile, ruleKey));
Expand Down

0 comments on commit 1496d3f

Please sign in to comment.