Skip to content

Commit

Permalink
Minor review changes
Browse files Browse the repository at this point in the history
Signed-off-by: David Schwilk <david.schwilk@bosch.io>
  • Loading branch information
DerSchwilk committed Oct 6, 2021
1 parent 08d551e commit 645b6f9
Showing 1 changed file with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.eclipse.ditto.policies.service.persistence.actors.announcements;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -63,9 +64,9 @@ private PolicyAnnouncementManager(final PolicyId policyId,
}

PolicyAnnouncementManager(final Function<Subject, Props> createChildProps) {
this.subjectExpiryActors = new HashMap<>();
this.activeSubjects = new HashMap<>();
this.activeSubjectIds = new HashMap<>();
subjectExpiryActors = new HashMap<>();
activeSubjects = new HashMap<>();
activeSubjectIds = new HashMap<>();
this.createChildProps = createChildProps;
}

Expand Down Expand Up @@ -104,7 +105,7 @@ private void onPolicyModified(final Policy policy) {
startChild(newSubject);
}
// copy current active subject IDs so that deleted subjects are immediately accounted for
final var counterMap = new HashMap<>(activeSubjectIds);
final Map<SubjectId, Integer> counterMap = new HashMap<>(activeSubjectIds);
for (final var deletedSubject : deletedSubjects) {
// precondition: child actors have started for new subjects so that modified subjects can be recognized
sendSubjectDeleted(deletedSubject, counterMap);
Expand Down Expand Up @@ -150,6 +151,7 @@ private void sendSubjectDeleted(final Subject subject, final Map<SubjectId, Inte
private void notifyChildOfSubjectDeletion(final Subject subject,
final ActorRef child,
final Map<SubjectId, Integer> counterMap) {

final var activeSubjectIdCount = counterMap.getOrDefault(subject.getId(), 0);
if (activeSubjectIdCount >= 2) {
// another actor took over the responsibility of child; terminate it.
Expand All @@ -162,7 +164,7 @@ private void notifyChildOfSubjectDeletion(final Subject subject,
decrementReferenceCount(subject, counterMap);
}

private Set<Subject> getSubjectsWithExpiryOrAnnouncements(final Policy policy) {
private static Set<Subject> getSubjectsWithExpiryOrAnnouncements(final Policy policy) {
if (policy.getLifecycle().filter(lifeCycle -> lifeCycle == PolicyLifecycle.ACTIVE).isPresent()) {
return StreamSupport.stream(policy.spliterator(), false)
.map(PolicyEntry::getSubjects)
Expand All @@ -188,7 +190,7 @@ private void removeActiveSubjectId(final Subject subject) {
decrementReferenceCount(subject, activeSubjectIds);
}

private void decrementReferenceCount(final Subject subject, final Map<SubjectId, Integer> counterMap) {
private static void decrementReferenceCount(final Subject subject, final Map<SubjectId, Integer> counterMap) {
counterMap.computeIfPresent(subject.getId(), (k, count) -> {
if (count <= 1) {
return null;
Expand All @@ -198,7 +200,9 @@ private void decrementReferenceCount(final Subject subject, final Map<SubjectId,
});
}

private static List<Subject> calculateDifference(final Set<Subject> minuend, final Set<Subject> subtrahend) {
private static List<Subject> calculateDifference(final Collection<Subject> minuend,
final Collection<Subject> subtrahend) {

return minuend.stream()
.filter(subject -> !subtrahend.contains(subject))
.collect(Collectors.toList());
Expand Down

0 comments on commit 645b6f9

Please sign in to comment.