Merge branch 'release/0.9-rc1'
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / IdentityChangeDetector.java
1 /*
2  * Sone - IdentityChangeDetector.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.freenet.wot;
19
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;
24 import static net.pterodactylus.sone.freenet.wot.Identity.TO_CONTEXTS;
25 import static net.pterodactylus.sone.freenet.wot.Identity.TO_PROPERTIES;
26
27 import java.util.Collection;
28 import java.util.Map;
29 import java.util.Map.Entry;
30
31 import com.google.common.base.Optional;
32 import com.google.common.base.Predicate;
33 import com.google.common.collect.FluentIterable;
34 import com.google.common.collect.ImmutableMap;
35
36 /**
37  * Detects changes between two lists of {@link Identity}s. The detector can find
38  * added and removed identities, and for identities that exist in both list
39  * their contexts and properties are checked for added, removed, or (in case of
40  * properties) changed values.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class IdentityChangeDetector {
45
46         private final Map<String, Identity> oldIdentities;
47         private Optional<IdentityProcessor> onNewIdentity = absent();
48         private Optional<IdentityProcessor> onRemovedIdentity = absent();
49         private Optional<IdentityProcessor> onChangedIdentity = absent();
50         private Optional<IdentityProcessor> onUnchangedIdentity = absent();
51
52         public IdentityChangeDetector(Collection<? extends Identity> oldIdentities) {
53                 this.oldIdentities = convertToMap(oldIdentities);
54         }
55
56         public void onNewIdentity(IdentityProcessor onNewIdentity) {
57                 this.onNewIdentity = fromNullable(onNewIdentity);
58         }
59
60         public void onRemovedIdentity(IdentityProcessor onRemovedIdentity) {
61                 this.onRemovedIdentity = fromNullable(onRemovedIdentity);
62         }
63
64         public void onChangedIdentity(IdentityProcessor onChangedIdentity) {
65                 this.onChangedIdentity = fromNullable(onChangedIdentity);
66         }
67
68         public void onUnchangedIdentity(IdentityProcessor onUnchangedIdentity) {
69                 this.onUnchangedIdentity = fromNullable(onUnchangedIdentity);
70         }
71
72         public void detectChanges(final Collection<? extends Identity> newIdentities) {
73                 notifyForRemovedIdentities(from(oldIdentities.values()).filter(notContainedIn(newIdentities)));
74                 notifyForNewIdentities(from(newIdentities).filter(notContainedIn(oldIdentities.values())));
75                 notifyForChangedIdentities(from(newIdentities).filter(containedIn(oldIdentities)).filter(hasChanged(oldIdentities)));
76                 notifyForUnchangedIdentities(from(newIdentities).filter(containedIn(oldIdentities)).filter(not(hasChanged(oldIdentities))));
77         }
78
79         private void notifyForRemovedIdentities(Iterable<Identity> identities) {
80                 notify(onRemovedIdentity, identities);
81         }
82
83         private void notifyForNewIdentities(FluentIterable<? extends Identity> newIdentities) {
84                 notify(onNewIdentity, newIdentities);
85         }
86
87         private void notifyForChangedIdentities(FluentIterable<? extends Identity> identities) {
88                 notify(onChangedIdentity, identities);
89         }
90
91         private void notifyForUnchangedIdentities(FluentIterable<? extends Identity> identities) {
92                 notify(onUnchangedIdentity, identities);
93         }
94
95         private void notify(Optional<IdentityProcessor> identityProcessor, Iterable<? extends Identity> identities) {
96                 if (!identityProcessor.isPresent()) {
97                         return;
98                 }
99                 for (Identity identity : identities) {
100                         identityProcessor.get().processIdentity(identity);
101                 }
102         }
103
104         private static Predicate<Identity> hasChanged(final Map<String, Identity> oldIdentities) {
105                 return new Predicate<Identity>() {
106                         @Override
107                         public boolean apply(Identity identity) {
108                                 return (identity == null) ? false : identityHasChanged(oldIdentities.get(identity.getId()), identity);
109                         }
110                 };
111         }
112
113         private static boolean identityHasChanged(Identity oldIdentity, Identity newIdentity) {
114                 return identityHasNewContexts(oldIdentity, newIdentity)
115                                 || identityHasRemovedContexts(oldIdentity, newIdentity)
116                                 || identityHasNewProperties(oldIdentity, newIdentity)
117                                 || identityHasRemovedProperties(oldIdentity, newIdentity)
118                                 || identityHasChangedProperties(oldIdentity, newIdentity);
119         }
120
121         private static boolean identityHasNewContexts(Identity oldIdentity, Identity newIdentity) {
122                 return from(TO_CONTEXTS.apply(newIdentity)).anyMatch(notAContextOf(oldIdentity));
123         }
124
125         private static boolean identityHasRemovedContexts(Identity oldIdentity, Identity newIdentity) {
126                 return from(TO_CONTEXTS.apply(oldIdentity)).anyMatch(notAContextOf(newIdentity));
127         }
128
129         private static boolean identityHasNewProperties(Identity oldIdentity, Identity newIdentity) {
130                 return from(TO_PROPERTIES.apply(newIdentity).entrySet()).anyMatch(notAPropertyOf(oldIdentity));
131         }
132
133         private static boolean identityHasRemovedProperties(Identity oldIdentity, Identity newIdentity) {
134                 return from(TO_PROPERTIES.apply(oldIdentity).entrySet()).anyMatch(notAPropertyOf(newIdentity));
135         }
136
137         private static boolean identityHasChangedProperties(Identity oldIdentity, Identity newIdentity) {
138                 return from(TO_PROPERTIES.apply(oldIdentity).entrySet()).anyMatch(hasADifferentValueThanIn(newIdentity));
139         }
140
141         private static Predicate<Identity> containedIn(final Map<String, Identity> identities) {
142                 return new Predicate<Identity>() {
143                         @Override
144                         public boolean apply(Identity identity) {
145                                 return identities.containsKey(identity.getId());
146                         }
147                 };
148         }
149
150         private static Predicate<String> notAContextOf(final Identity identity) {
151                 return new Predicate<String>() {
152                         @Override
153                         public boolean apply(String context) {
154                                 return (identity == null) ? false : !identity.getContexts().contains(context);
155                         }
156                 };
157         }
158
159         private static Predicate<Identity> notContainedIn(final Collection<? extends Identity> newIdentities) {
160                 return new Predicate<Identity>() {
161                         @Override
162                         public boolean apply(Identity identity) {
163                                 return (identity == null) ? false : !newIdentities.contains(identity);
164                         }
165                 };
166         }
167
168         private static Predicate<Entry<String, String>> notAPropertyOf(final Identity identity) {
169                 return new Predicate<Entry<String, String>>() {
170                         @Override
171                         public boolean apply(Entry<String, String> property) {
172                                 return (property == null) ? false : !identity.getProperties().containsKey(property.getKey());
173                         }
174                 };
175         }
176
177         private static Predicate<Entry<String, String>> hasADifferentValueThanIn(final Identity newIdentity) {
178                 return new Predicate<Entry<String, String>>() {
179                         @Override
180                         public boolean apply(Entry<String, String> property) {
181                                 return (property == null) ? false : !newIdentity.getProperty(property.getKey()).equals(property.getValue());
182                         }
183                 };
184         }
185
186         private static Map<String, Identity> convertToMap(Collection<? extends Identity> identities) {
187                 ImmutableMap.Builder<String, Identity> mapBuilder = ImmutableMap.builder();
188                 for (Identity identity : identities) {
189                         mapBuilder.put(identity.getId(), identity);
190                 }
191                 return mapBuilder.build();
192         }
193
194         public interface IdentityProcessor {
195
196                 void processIdentity(Identity identity);
197
198         }
199
200 }