Remove @author tags
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / IdentityChangeDetector.java
1 /*
2  * Sone - IdentityChangeDetector.java - Copyright © 2013–2016 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 public class IdentityChangeDetector {
43
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();
49
50         public IdentityChangeDetector(Collection<? extends Identity> oldIdentities) {
51                 this.oldIdentities = convertToMap(oldIdentities);
52         }
53
54         public void onNewIdentity(IdentityProcessor onNewIdentity) {
55                 this.onNewIdentity = fromNullable(onNewIdentity);
56         }
57
58         public void onRemovedIdentity(IdentityProcessor onRemovedIdentity) {
59                 this.onRemovedIdentity = fromNullable(onRemovedIdentity);
60         }
61
62         public void onChangedIdentity(IdentityProcessor onChangedIdentity) {
63                 this.onChangedIdentity = fromNullable(onChangedIdentity);
64         }
65
66         public void onUnchangedIdentity(IdentityProcessor onUnchangedIdentity) {
67                 this.onUnchangedIdentity = fromNullable(onUnchangedIdentity);
68         }
69
70         public void detectChanges(final 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))));
75         }
76
77         private void notifyForRemovedIdentities(Iterable<Identity> identities) {
78                 notify(onRemovedIdentity, identities);
79         }
80
81         private void notifyForNewIdentities(FluentIterable<? extends Identity> newIdentities) {
82                 notify(onNewIdentity, newIdentities);
83         }
84
85         private void notifyForChangedIdentities(FluentIterable<? extends Identity> identities) {
86                 notify(onChangedIdentity, identities);
87         }
88
89         private void notifyForUnchangedIdentities(FluentIterable<? extends Identity> identities) {
90                 notify(onUnchangedIdentity, identities);
91         }
92
93         private void notify(Optional<IdentityProcessor> identityProcessor, Iterable<? extends Identity> identities) {
94                 if (!identityProcessor.isPresent()) {
95                         return;
96                 }
97                 for (Identity identity : identities) {
98                         identityProcessor.get().processIdentity(identity);
99                 }
100         }
101
102         private static Predicate<Identity> hasChanged(final Map<String, Identity> oldIdentities) {
103                 return new Predicate<Identity>() {
104                         @Override
105                         public boolean apply(Identity identity) {
106                                 return (identity != null) && identityHasChanged(oldIdentities.get(identity.getId()), identity);
107                         }
108                 };
109         }
110
111         private static boolean identityHasChanged(Identity oldIdentity, Identity newIdentity) {
112                 return identityHasNewContexts(oldIdentity, newIdentity)
113                                 || identityHasRemovedContexts(oldIdentity, newIdentity)
114                                 || identityHasNewProperties(oldIdentity, newIdentity)
115                                 || identityHasRemovedProperties(oldIdentity, newIdentity)
116                                 || identityHasChangedProperties(oldIdentity, newIdentity);
117         }
118
119         private static boolean identityHasNewContexts(Identity oldIdentity, Identity newIdentity) {
120                 return from(TO_CONTEXTS.apply(newIdentity)).anyMatch(notAContextOf(oldIdentity));
121         }
122
123         private static boolean identityHasRemovedContexts(Identity oldIdentity, Identity newIdentity) {
124                 return from(TO_CONTEXTS.apply(oldIdentity)).anyMatch(notAContextOf(newIdentity));
125         }
126
127         private static boolean identityHasNewProperties(Identity oldIdentity, Identity newIdentity) {
128                 return from(TO_PROPERTIES.apply(newIdentity).entrySet()).anyMatch(notAPropertyOf(oldIdentity));
129         }
130
131         private static boolean identityHasRemovedProperties(Identity oldIdentity, Identity newIdentity) {
132                 return from(TO_PROPERTIES.apply(oldIdentity).entrySet()).anyMatch(notAPropertyOf(newIdentity));
133         }
134
135         private static boolean identityHasChangedProperties(Identity oldIdentity, Identity newIdentity) {
136                 return from(TO_PROPERTIES.apply(oldIdentity).entrySet()).anyMatch(hasADifferentValueThanIn(newIdentity));
137         }
138
139         private static Predicate<Identity> containedIn(final Map<String, Identity> identities) {
140                 return new Predicate<Identity>() {
141                         @Override
142                         public boolean apply(Identity identity) {
143                                 return (identity != null) && identities.containsKey(identity.getId());
144                         }
145                 };
146         }
147
148         private static Predicate<String> notAContextOf(final Identity identity) {
149                 return new Predicate<String>() {
150                         @Override
151                         public boolean apply(String context) {
152                                 return (identity != null) && !identity.getContexts().contains(context);
153                         }
154                 };
155         }
156
157         private static Predicate<Identity> notContainedIn(final Collection<? extends Identity> newIdentities) {
158                 return new Predicate<Identity>() {
159                         @Override
160                         public boolean apply(Identity identity) {
161                                 return (identity != null) && !newIdentities.contains(identity);
162                         }
163                 };
164         }
165
166         private static Predicate<Entry<String, String>> notAPropertyOf(final Identity identity) {
167                 return new Predicate<Entry<String, String>>() {
168                         @Override
169                         public boolean apply(Entry<String, String> property) {
170                                 return (property != null) && !identity.getProperties().containsKey(property.getKey());
171                         }
172                 };
173         }
174
175         private static Predicate<Entry<String, String>> hasADifferentValueThanIn(final Identity newIdentity) {
176                 return new Predicate<Entry<String, String>>() {
177                         @Override
178                         public boolean apply(Entry<String, String> property) {
179                                 return (property != null) && !newIdentity.getProperty(property.getKey()).equals(property.getValue());
180                         }
181                 };
182         }
183
184         private static Map<String, Identity> convertToMap(Collection<? extends Identity> identities) {
185                 ImmutableMap.Builder<String, Identity> mapBuilder = ImmutableMap.builder();
186                 for (Identity identity : identities) {
187                         mapBuilder.put(identity.getId(), identity);
188                 }
189                 return mapBuilder.build();
190         }
191
192         public interface IdentityProcessor {
193
194                 void processIdentity(Identity identity);
195
196         }
197
198 }