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