2 * Sone - IdentityChangeDetector.java - Copyright © 2013 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.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 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class IdentityChangeDetector {
44 private final Map<String, Identity> oldIdentities;
45 private Optional<IdentityProcessor> onNewIdentity = absent();
46 private Optional<IdentityProcessor> onRemovedIdentity = absent();
47 private Optional<IdentityProcessor> onChangedIdentity = absent();
48 private Optional<IdentityProcessor> onUnchangedIdentity = absent();
50 public IdentityChangeDetector(Collection<? extends Identity> oldIdentities) {
51 this.oldIdentities = convertToMap(oldIdentities);
54 public void onNewIdentity(IdentityProcessor onNewIdentity) {
55 this.onNewIdentity = fromNullable(onNewIdentity);
58 public void onRemovedIdentity(IdentityProcessor onRemovedIdentity) {
59 this.onRemovedIdentity = fromNullable(onRemovedIdentity);
62 public void onChangedIdentity(IdentityProcessor onChangedIdentity) {
63 this.onChangedIdentity = fromNullable(onChangedIdentity);
66 public void onUnchangedIdentity(IdentityProcessor onUnchangedIdentity) {
67 this.onUnchangedIdentity = fromNullable(onUnchangedIdentity);
70 public void detectChanges(Collection<? extends Identity> newIdentities) {
71 notifyForRemovedIdentities(from(oldIdentities.values()).filter(notContainedIn(newIdentities)));
72 notifyForNewIdentities(from(newIdentities).filter(notContainedIn(oldIdentities.values())));
73 notifyForChangedIdentities(from(newIdentities).filter(containedIn(oldIdentities)).filter(hasChanged(oldIdentities)));
74 notifyForUnchangedIdentities(from(newIdentities).filter(containedIn(oldIdentities)).filter(not(hasChanged(oldIdentities))));
77 private void notifyForRemovedIdentities(Iterable<Identity> identities) {
78 notify(onRemovedIdentity, identities);
81 private void notifyForNewIdentities(FluentIterable<? extends Identity> newIdentities) {
82 notify(onNewIdentity, newIdentities);
85 private void notifyForChangedIdentities(FluentIterable<? extends Identity> identities) {
86 notify(onChangedIdentity, identities);
89 private void notifyForUnchangedIdentities(FluentIterable<? extends Identity> identities) {
90 notify(onUnchangedIdentity, identities);
93 private void notify(Optional<IdentityProcessor> identityProcessor, Iterable<? extends Identity> identities) {
94 if (!identityProcessor.isPresent()) {
97 for (Identity identity : identities) {
98 identityProcessor.get().processIdentity(identity);
102 private static Predicate<Identity> hasChanged(Map<String, Identity> oldIdentities) {
103 return identity -> identityHasChanged(oldIdentities.get(identity.getId()), identity);
106 private static boolean identityHasChanged(Identity oldIdentity, Identity newIdentity) {
107 return identityHasNewContexts(oldIdentity, newIdentity)
108 || identityHasRemovedContexts(oldIdentity, newIdentity)
109 || identityHasNewProperties(oldIdentity, newIdentity)
110 || identityHasRemovedProperties(oldIdentity, newIdentity)
111 || identityHasChangedProperties(oldIdentity, newIdentity);
114 private static boolean identityHasNewContexts(Identity oldIdentity, Identity newIdentity) {
115 return from(newIdentity.getContexts()).anyMatch(notAContextOf(oldIdentity));
118 private static boolean identityHasRemovedContexts(Identity oldIdentity, Identity newIdentity) {
119 return from(oldIdentity.getContexts()).anyMatch(notAContextOf(newIdentity));
122 private static boolean identityHasNewProperties(Identity oldIdentity, Identity newIdentity) {
123 return from(newIdentity.getProperties().entrySet()).anyMatch(notAPropertyOf(oldIdentity));
126 private static boolean identityHasRemovedProperties(Identity oldIdentity, Identity newIdentity) {
127 return from(oldIdentity.getProperties().entrySet()).anyMatch(notAPropertyOf(newIdentity));
130 private static boolean identityHasChangedProperties(Identity oldIdentity, Identity newIdentity) {
131 return from(oldIdentity.getProperties().entrySet()).anyMatch(hasADifferentValueThanIn(newIdentity));
134 private static Predicate<Identity> containedIn(Map<String, Identity> identities) {
135 return identity -> identities.containsKey(identity.getId());
138 private static Predicate<String> notAContextOf(Identity identity) {
139 return context -> (identity != null) && !identity.getContexts().contains(context);
142 private static Predicate<Identity> notContainedIn(Collection<? extends Identity> newIdentities) {
143 return identity -> (identity != null) && !newIdentities.contains(identity);
146 private static Predicate<Entry<String, String>> notAPropertyOf(Identity identity) {
147 return property -> (property != null) && !identity.getProperties().containsKey(property.getKey());
150 private static Predicate<Entry<String, String>> hasADifferentValueThanIn(Identity newIdentity) {
152 (property != null) && !newIdentity.getProperty(property.getKey()).equals(property.getValue());
155 private static Map<String, Identity> convertToMap(Collection<? extends Identity> identities) {
156 ImmutableMap.Builder<String, Identity> mapBuilder = ImmutableMap.builder();
157 for (Identity identity : identities) {
158 mapBuilder.put(identity.getId(), identity);
160 return mapBuilder.build();
163 public interface IdentityProcessor {
165 void processIdentity(Identity identity);