/
FeatureDiff.java
287 lines (251 loc) · 10.8 KB
/
FeatureDiff.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* Copyright (c) 2013-2016 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Victor Olaya (Boundless) - initial implementation
*/
package org.locationtech.geogig.plumbing.diff;
import java.util.BitSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.jdt.annotation.Nullable;
import org.locationtech.geogig.model.RevFeature;
import org.locationtech.geogig.model.RevFeatureType;
import org.opengis.feature.type.PropertyDescriptor;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.locationtech.jts.geom.Geometry;
/**
* Defines the differences between 2 versions of the a given feature
*
*/
public class FeatureDiff {
private Map<PropertyDescriptor, AttributeDiff> diffs;
private String path;
private RevFeatureType newFeatureType;
private RevFeatureType oldFeatureType;
private RevFeature newRevFeature;
private RevFeature oldRevFeature;
public FeatureDiff(String path, Map<PropertyDescriptor, AttributeDiff> diffs,
RevFeatureType oldFeatureType, RevFeatureType newFeatureType) {
this.path = path;
this.diffs = Maps.newHashMap(diffs);
this.newFeatureType = newFeatureType;
this.oldFeatureType = oldFeatureType;
}
/**
*
* @param path the full path to the feature, including its name
* @param newRevFeature the new version of the feature
* @param oldRevFeature the old version of the feature
* @param newRevFeatureType the new version of the feature type
* @param oldRevFeatureType the old version of the feature type
* @param all - true if all attributes should be added regardless of change
*/
public FeatureDiff(String path, @Nullable RevFeature newRevFeature,
@Nullable RevFeature oldRevFeature, @Nullable RevFeatureType newRevFeatureType,
@Nullable RevFeatureType oldRevFeatureType, boolean all) {
this.path = path;
this.newRevFeature = newRevFeature;
this.oldRevFeature = oldRevFeature;
this.newFeatureType = newRevFeatureType;
this.oldFeatureType = oldRevFeatureType;
diffs = new HashMap<PropertyDescriptor, AttributeDiff>();
if (newRevFeature == null) {
Preconditions.checkArgument(oldRevFeature != null, "A feature must be provided");
Preconditions.checkArgument(oldRevFeatureType != null,
"Old feature type must be provided.");
ImmutableList<PropertyDescriptor> oldAttributes = oldRevFeatureType.descriptors();
for (int i = 0; i < oldAttributes.size(); i++) {
Optional<Object> oldValue = oldRevFeature.get(i);
PropertyDescriptor descriptor = oldAttributes.get(i);
if (Geometry.class.isAssignableFrom(descriptor.getType().getBinding())) {
diffs.put(descriptor, new GeometryAttributeDiff((Geometry) oldValue.orNull(),
(Geometry) null));
} else {
diffs.put(oldAttributes.get(i),
new GenericAttributeDiffImpl(oldValue.orNull(), null));
}
}
} else if (oldRevFeature == null) {
Preconditions.checkArgument(newRevFeatureType != null,
"New feature type must be provided.");
ImmutableList<PropertyDescriptor> newAttributes = newRevFeatureType.descriptors();
for (int i = 0; i < newAttributes.size(); i++) {
Optional<Object> newValue = newRevFeature.get(i);
PropertyDescriptor descriptor = newAttributes.get(i);
if (Geometry.class.isAssignableFrom(descriptor.getType().getBinding())) {
diffs.put(descriptor, new GeometryAttributeDiff((Geometry) null,
(Geometry) newValue.orNull()));
} else {
diffs.put(newAttributes.get(i),
new GenericAttributeDiffImpl(null, newValue.orNull()));
}
}
} else {
ImmutableList<PropertyDescriptor> oldAttributes = oldRevFeatureType.descriptors();
ImmutableList<PropertyDescriptor> newAttributes = newRevFeatureType.descriptors();
BitSet updatedAttributes = new BitSet(newRevFeature.size());
for (int i = 0; i < oldAttributes.size(); i++) {
Optional<Object> oldValue = oldRevFeature.get(i);
int idx = newAttributes.indexOf(oldAttributes.get(i));
if (idx != -1) {
Optional<Object> newValue = newRevFeature.get(idx);
if (!oldValue.equals(newValue) || all) {
if (Geometry.class
.isAssignableFrom(oldAttributes.get(i).getType().getBinding())) {
diffs.put(oldAttributes.get(i), new GeometryAttributeDiff(
(Geometry) oldValue.orNull(), (Geometry) newValue.orNull()));
} else {
diffs.put(oldAttributes.get(i), new GenericAttributeDiffImpl(
oldValue.orNull(), newValue.orNull()));
}
}
updatedAttributes.set(idx);
} else {
if (Geometry.class
.isAssignableFrom(oldAttributes.get(i).getType().getBinding())) {
diffs.put(oldAttributes.get(i), new GeometryAttributeDiff(
(Geometry) oldValue.orNull(), (Geometry) null));
} else {
diffs.put(oldAttributes.get(i),
new GenericAttributeDiffImpl(oldValue.orNull(), null));
}
}
}
updatedAttributes.flip(0, newRevFeature.size());
for (int i = updatedAttributes.nextSetBit(0); i >= 0; i = updatedAttributes
.nextSetBit(i + 1)) {
PropertyDescriptor descriptor = newAttributes.get(i);
if (Geometry.class.isAssignableFrom(descriptor.getType().getBinding())) {
diffs.put(descriptor, new GeometryAttributeDiff((Geometry) null,
(Geometry) newRevFeature.get(i).orNull()));
} else {
diffs.put(descriptor,
new GenericAttributeDiffImpl(null, newRevFeature.get(i).orNull()));
}
}
}
}
public boolean hasDifferences() {
return diffs.size() != 0;
}
public Map<PropertyDescriptor, AttributeDiff> getDiffs() {
return ImmutableMap.copyOf(diffs);
}
/**
* Returns the full path to the feature, including its name
*
* @return
*/
public String getPath() {
return path;
}
/**
* The feature type of the new version of the feature
*
* @return
*/
public RevFeatureType getNewFeatureType() {
return newFeatureType;
}
/**
* The feature type of the old version of the feature
*
* @return
*/
public RevFeatureType getOldFeatureType() {
return oldFeatureType;
}
public RevFeature getNewFeature() {
return newRevFeature;
}
public RevFeature getOldFeature() {
return oldRevFeature;
}
/**
* Returns a human-readable representation of this class. To get a string that can be used to
* serialize this object, use the asText() method
*/
public String toString() {
StringBuilder sb = new StringBuilder();
Set<Entry<PropertyDescriptor, AttributeDiff>> entries = diffs.entrySet();
Iterator<Entry<PropertyDescriptor, AttributeDiff>> iter = entries.iterator();
while (iter.hasNext()) {
Entry<PropertyDescriptor, AttributeDiff> entry = iter.next();
PropertyDescriptor pd = entry.getKey();
AttributeDiff ad = entry.getValue();
sb.append(pd.getName() + "\t" + ad.toString() + "\n");
}
return sb.toString();
}
/**
* Returns a serialized text version of this object
*
* @return
*/
public String asText() {
StringBuilder sb = new StringBuilder();
Set<Entry<PropertyDescriptor, AttributeDiff>> entries = diffs.entrySet();
Iterator<Entry<PropertyDescriptor, AttributeDiff>> iter = entries.iterator();
while (iter.hasNext()) {
Entry<PropertyDescriptor, AttributeDiff> entry = iter.next();
PropertyDescriptor pd = entry.getKey();
AttributeDiff ad = entry.getValue();
sb.append(pd.getName().toString() + "\t" + ad.asText() + "\n");
}
return sb.toString();
}
/**
* Returns a reversed version of the feature difference
*
* @return
*/
public FeatureDiff reversed() {
Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
Set<Entry<PropertyDescriptor, AttributeDiff>> entries = diffs.entrySet();
for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = entries
.iterator(); iterator.hasNext();) {
Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
map.put(entry.getKey(), entry.getValue().reversed());
}
return new FeatureDiff(path, map, newFeatureType, oldFeatureType);
}
@Override
public boolean equals(Object o) {
// TODO: this is a temporary simple comparison. Should be more elaborate
if (!(o instanceof FeatureDiff)) {
return false;
}
FeatureDiff f = (FeatureDiff) o;
return f.asText().equals(asText());
// return f.diffs.equals(diffs) && f.path.equals(path);
}
/**
* Checks whether a FeatureDiff conflicts with this one
*
* @param featureDiff the featureDiff to check against this one
*/
public boolean conflicts(FeatureDiff featureDiff) {
Map<PropertyDescriptor, AttributeDiff> otherDiffs = featureDiff.diffs;
for (PropertyDescriptor pd : otherDiffs.keySet()) {
if (diffs.containsKey(pd)) {
AttributeDiff ad = diffs.get(pd);
AttributeDiff otherAd = otherDiffs.get(pd);
if (ad.conflicts(otherAd)) {
return true;
}
}
}
return false;
}
}