2 * Sone - IdentityChangeDetector.java - Copyright © 2013–2019 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.freenet.wot;
20 import static com.google.common.base.Optional.absent;
21 import static com.google.common.base.Optional.fromNullable;
22 import static com.google.common.base.Predicates.not;
23 import static com.google.common.collect.FluentIterable.from;
25 import java.util.Collection;
27 import java.util.Map.Entry;
29 import com.google.common.base.Optional;
30 import com.google.common.base.Predicate;
31 import com.google.common.collect.FluentIterable;
32 import com.google.common.collect.ImmutableMap;
35 * Detects changes between two lists of {@link Identity}s. The detector can find
36 * added and removed identities, and for identities that exist in both list
37 * their contexts and properties are checked for added, removed, or (in case of
38 * properties) changed values.
40 public class IdentityChangeDetector {
42 private final Map<String, Identity> oldIdentities;
43 private Optional<IdentityProcessor> onNewIdentity = absent();
44 private Optional<IdentityProcessor> onRemovedIdentity = absent();
45 private Optional<IdentityProcessor> onChangedIdentity = absent();
46 private Optional<IdentityProcessor> onUnchangedIdentity = absent();
48 public IdentityChangeDetector(Collection<? extends Identity> oldIdentities) {
49 this.oldIdentities = convertToMap(oldIdentities);
52 public void onNewIdentity(IdentityProcessor onNewIdentity) {
53 this.onNewIdentity = fromNullable(onNewIdentity);
56 public void onRemovedIdentity(IdentityProcessor onRemovedIdentity) {
57 this.onRemovedIdentity = fromNullable(onRemovedIdentity);
60 public void onChangedIdentity(IdentityProcessor onChangedIdentity) {
61 this.onChangedIdentity = fromNullable(onChangedIdentity);
64 public void onUnchangedIdentity(IdentityProcessor onUnchangedIdentity) {
65 this.onUnchangedIdentity = fromNullable(onUnchangedIdentity);
68 public void detectChanges(final Collection<? extends Identity> newIdentities) {
69 notifyForRemovedIdentities(from(oldIdentities.values()).filter(notContainedIn(newIdentities)));
70 notifyForNewIdentities(from(newIdentities).filter(notContainedIn(oldIdentities.values())));
71 notifyForChangedIdentities(from(newIdentities).filter(containedIn(oldIdentities)).filter(hasChanged(oldIdentities)));
72 notifyForUnchangedIdentities(from(newIdentities).filter(containedIn(oldIdentities)).filter(not(hasChanged(oldIdentities))));
75 private void notifyForRemovedIdentities(Iterable<Identity> identities) {
76 notify(onRemovedIdentity, identities);
79 private void notifyForNewIdentities(FluentIterable<? extends Identity> newIdentities) {
80 notify(onNewIdentity, newIdentities);
83 private void notifyForChangedIdentities(FluentIterable<? extends Identity> identities) {
84 notify(onChangedIdentity, identities);
87 private void notifyForUnchangedIdentities(FluentIterable<? extends Identity> identities) {
88 notify(onUnchangedIdentity, identities);
91 private void notify(Optional<IdentityProcessor> identityProcessor, Iterable<? extends Identity> identities) {
92 if (!identityProcessor.isPresent()) {
95 for (Identity identity : identities) {
96 identityProcessor.get().processIdentity(identity);
100 private static Predicate<Identity> hasChanged(final Map<String, Identity> oldIdentities) {
101 return new Predicate<Identity>() {
103 public boolean apply(Identity identity) {
104 return (identity != null) && identityHasChanged(oldIdentities.get(identity.getId()), identity);
109 private static boolean identityHasChanged(Identity oldIdentity, Identity newIdentity) {
110 return identityHasNewContexts(oldIdentity, newIdentity)
111 || identityHasRemovedContexts(oldIdentity, newIdentity)
112 || identityHasNewProperties(oldIdentity, newIdentity)
113 || identityHasRemovedProperties(oldIdentity, newIdentity)
114 || identityHasChangedProperties(oldIdentity, newIdentity);
117 private static boolean identityHasNewContexts(Identity oldIdentity, Identity newIdentity) {
118 return newIdentity.getContexts().stream().anyMatch(notAContextOf(oldIdentity)::apply);
121 private static boolean identityHasRemovedContexts(Identity oldIdentity, Identity newIdentity) {
122 return oldIdentity.getContexts().stream().anyMatch(notAContextOf(newIdentity)::apply);
125 private static boolean identityHasNewProperties(Identity oldIdentity, Identity newIdentity) {
126 return newIdentity.getProperties().entrySet().stream().anyMatch(notAPropertyOf(oldIdentity)::apply);
129 private static boolean identityHasRemovedProperties(Identity oldIdentity, Identity newIdentity) {
130 return oldIdentity.getProperties().entrySet().stream().anyMatch(notAPropertyOf(newIdentity)::apply);
133 private static boolean identityHasChangedProperties(Identity oldIdentity, Identity newIdentity) {
134 return oldIdentity.getProperties().entrySet().stream().anyMatch(hasADifferentValueThanIn(newIdentity)::apply);
137 private static Predicate<Identity> containedIn(final Map<String, Identity> identities) {
138 return new Predicate<Identity>() {
140 public boolean apply(Identity identity) {
141 return (identity != null) && identities.containsKey(identity.getId());
146 private static Predicate<String> notAContextOf(final Identity identity) {
147 return new Predicate<String>() {
149 public boolean apply(String context) {
150 return (identity != null) && !identity.getContexts().contains(context);
155 private static Predicate<Identity> notContainedIn(final Collection<? extends Identity> newIdentities) {
156 return new Predicate<Identity>() {
158 public boolean apply(Identity identity) {
159 return (identity != null) && !newIdentities.contains(identity);
164 private static Predicate<Entry<String, String>> notAPropertyOf(final Identity identity) {
165 return new Predicate<Entry<String, String>>() {
167 public boolean apply(Entry<String, String> property) {
168 return (property != null) && !identity.getProperties().containsKey(property.getKey());
173 private static Predicate<Entry<String, String>> hasADifferentValueThanIn(final Identity newIdentity) {
174 return new Predicate<Entry<String, String>>() {
176 public boolean apply(Entry<String, String> property) {
177 return (property != null) && !newIdentity.getProperty(property.getKey()).equals(property.getValue());
182 private static Map<String, Identity> convertToMap(Collection<? extends Identity> identities) {
183 ImmutableMap.Builder<String, Identity> mapBuilder = ImmutableMap.builder();
184 for (Identity identity : identities) {
185 mapBuilder.put(identity.getId(), identity);
187 return mapBuilder.build();
190 public interface IdentityProcessor {
192 void processIdentity(Identity identity);