@Override public AttributeDiff reversed() { if (type == TYPE.MODIFIED) { return new GeometryAttributeDiff(this.diff.reversed()); } else { return new GeometryAttributeDiff(oldGeometry, newGeometry); } }
@Override public AttributeDiff reversed() { if (type == TYPE.MODIFIED) { return new GeometryAttributeDiff(this.diff.reversed()); } else { return new GeometryAttributeDiff(oldGeometry, newGeometry); } }
public static AttributeDiff attributeDiffFromText(Class<?> clazz, String s) { String[] tokens = s.split("\t"); AttributeDiff ad; if (Geometry.class.isAssignableFrom(clazz)) { ad = new GeometryAttributeDiff(s); } else { if (AttributeDiff.TYPE.REMOVED.name().startsWith(tokens[0])) { Preconditions.checkArgument(tokens.length == 2, "Wrong difference definition:", s); Object oldValue = TextValueSerializer.fromString(FieldType.forBinding(clazz), tokens[1]); ad = new GenericAttributeDiffImpl(oldValue, null); } else if (AttributeDiff.TYPE.ADDED.name().startsWith(tokens[0])) { Preconditions.checkArgument(tokens.length == 2, "Wrong difference definition:", s); Object newValue = TextValueSerializer.fromString(FieldType.forBinding(clazz), tokens[1]); ad = new GenericAttributeDiffImpl(null, newValue); } else if (AttributeDiff.TYPE.MODIFIED.name().startsWith(tokens[0])) { Preconditions.checkArgument(tokens.length == 3, "Wrong difference definition:", s); Object oldValue = TextValueSerializer.fromString(FieldType.forBinding(clazz), tokens[1]); Object newValue = TextValueSerializer.fromString(FieldType.forBinding(clazz), tokens[2]); ad = new GenericAttributeDiffImpl(oldValue, newValue); } else { throw new IllegalArgumentException("Wrong difference definition:" + s); } } return ad; }
public static AttributeDiff attributeDiffFromText(Class<?> clazz, String s) { String[] tokens = s.split("\t"); AttributeDiff ad; if (Geometry.class.isAssignableFrom(clazz)) { ad = new GeometryAttributeDiff(s); } else { if (AttributeDiff.TYPE.REMOVED.name().startsWith(tokens[0])) { Preconditions.checkArgument(tokens.length == 2, "Wrong difference definition:", s); Object oldValue = TextValueSerializer.fromString(FieldType.forBinding(clazz), tokens[1]); ad = new GenericAttributeDiffImpl(oldValue, null); } else if (AttributeDiff.TYPE.ADDED.name().startsWith(tokens[0])) { Preconditions.checkArgument(tokens.length == 2, "Wrong difference definition:", s); Object newValue = TextValueSerializer.fromString(FieldType.forBinding(clazz), tokens[1]); ad = new GenericAttributeDiffImpl(null, newValue); } else if (AttributeDiff.TYPE.MODIFIED.name().startsWith(tokens[0])) { Preconditions.checkArgument(tokens.length == 3, "Wrong difference definition:", s); Object oldValue = TextValueSerializer.fromString(FieldType.forBinding(clazz), tokens[1]); Object newValue = TextValueSerializer.fromString(FieldType.forBinding(clazz), tokens[2]); ad = new GenericAttributeDiffImpl(oldValue, newValue); } else { throw new IllegalArgumentException("Wrong difference definition:" + s); } } return ad; }
@Test public void testNoConflictIfSameDiff() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 35, 30 30),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom); assertFalse(diff.conflicts(diff2)); }
@Test public void testNoConflictIfSameDiff() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 35, 30 30),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom); assertFalse(diff.conflicts(diff2)); }
@Test public void testConflictEditedSamePoint() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 48 32, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); Geometry newGeom2 = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 41 33, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom2); assertTrue(diff.conflicts(diff2)); }
@Test public void testConflict() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader() .read("MULTILINESTRING ((40 40, 20 45),(20 35, 45 10, 20 35))"); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); Geometry newGeom2 = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 41 33, 25 25),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom2); assertTrue(diff.conflicts(diff2)); }
@Test public void testConflict() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader() .read("MULTILINESTRING ((40 40, 20 45),(20 35, 45 10, 20 35))"); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); Geometry newGeom2 = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 41 33, 25 25),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom2); assertTrue(diff.conflicts(diff2)); }
@Test public void testConflictEditedSamePoint() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 48 32, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); Geometry newGeom2 = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 41 33, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom2); assertTrue(diff.conflicts(diff2)); }
@Test public void testNoConflictIfSameDiff2() throws Exception { String wkt = "MULTIPOLYGON (((-75.0740073 38.6938098, -75.0739683 38.6935296, " + "-75.0745695 38.6934786, -75.0745824 38.6935715, -75.0741091 38.6936117, " + "-75.0741352 38.6937989, -75.0740073 38.6938098)))"; Geometry oldGeom = new WKTReader().read(wkt); Geometry newGeom = new WKTReader().read(wkt); assertTrue(oldGeom.equalsExact(newGeom)); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom); assertFalse(diff.conflicts(diff2)); } }
@Test public void testNoConflictIfSameDiff2() throws Exception { String wkt = "MULTIPOLYGON (((-75.0740073 38.6938098, -75.0739683 38.6935296, " + "-75.0745695 38.6934786, -75.0745824 38.6935715, -75.0741091 38.6936117, " + "-75.0741352 38.6937989, -75.0740073 38.6938098)))"; Geometry oldGeom = new WKTReader().read(wkt); Geometry newGeom = new WKTReader().read(wkt); assertTrue(oldGeom.equalsExact(newGeom)); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom); assertFalse(diff.conflicts(diff2)); } }
@Test public void testConflictPatcheable() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 35, 30 30),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); Geometry newGeom2 = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 31 6, 10 30, 20 35))"); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom2); // it's a conflict for the sake of merging branches assertTrue(diff.conflicts(diff2)); // yet can be merged for the sake of applying a patch assertTrue(diff2.canBeAppliedOn(newGeom)); Geometry merged = diff2.applyOn(newGeom); assertNotNull(merged); assertEquals( "MULTILINESTRING ((40 40, 20 45, 45 35, 30 30), (20 35, 45 10, 31 6, 10 30, 20 35))", merged.toText()); }
@Test public void testNoConflictAddingPoints() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 10 10, 20 45, 45 30, 30 30),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); Geometry newGeom2 = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 31 6, 10 30, 20 35))"); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom2); // the diff is conflicting assertTrue(diff.conflicts(diff2)); // yet it can be applied for patching assertTrue(diff2.canBeAppliedOn(newGeom)); Geometry merged = diff2.applyOn(newGeom); assertNotNull(merged); assertEquals( "MULTILINESTRING ((40 40, 10 10, 20 45, 45 30, 30 30), (20 35, 45 10, 31 6, 10 30, 20 35))", merged.toText()); }
@Test public void testConflictPatcheable() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 35, 30 30),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); Geometry newGeom2 = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 31 6, 10 30, 20 35))"); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom2); // it's a conflict for the sake of merging branches assertTrue(diff.conflicts(diff2)); // yet can be merged for the sake of applying a patch assertTrue(diff2.canBeAppliedOn(newGeom)); Geometry merged = diff2.applyOn(newGeom); assertNotNull(merged); assertEquals( "MULTILINESTRING ((40 40, 20 45, 45 35, 30 30), (20 35, 45 10, 31 6, 10 30, 20 35))", merged.toText()); }
@Test public void testNoConflictAddingPoints() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 10 10, 20 45, 45 30, 30 30),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); Geometry newGeom2 = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 31 6, 10 30, 20 35))"); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom2); // the diff is conflicting assertTrue(diff.conflicts(diff2)); // yet it can be applied for patching assertTrue(diff2.canBeAppliedOn(newGeom)); Geometry merged = diff2.applyOn(newGeom); assertNotNull(merged); assertEquals( "MULTILINESTRING ((40 40, 10 10, 20 45, 45 30, 30 30), (20 35, 45 10, 31 6, 10 30, 20 35))", merged.toText()); }
@Test public void testModifyFeatureAttributePatch() throws Exception { Patch patch = new Patch(); String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID()); Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap(); Object oldValue = points1.getProperty("sp").getValue(); GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(oldValue, "new"); Geometry oldGeometry = (Geometry) points1.getProperty("pp").getValue(); Geometry newGeometry = new WKTReader2().read("POINT (2 2)"); GeometryAttributeDiff geomDiff = new GeometryAttributeDiff(oldGeometry, newGeometry); map.put(pointsType.getDescriptor("sp"), diff); map.put(pointsType.getDescriptor("pp"), geomDiff); FeatureDiff feaureDiff = new FeatureDiff(path, map, RevFeatureTypeBuilder.build(pointsType), RevFeatureTypeBuilder.build(pointsType)); patch.addModifiedFeature(feaureDiff); testPatch(patch); }
@Test public void testConflictRemovingPoints() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader() .read("MULTILINESTRING ((40 40, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); Geometry newGeom2 = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 31 6, 10 30, 20 35))"); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom2); // the diff reports a conflict assertTrue(diff.conflicts(diff2)); // yet it can be applied for patching assertTrue(diff2.canBeAppliedOn(newGeom)); Geometry merged = diff2.applyOn(newGeom); assertNotNull(merged); Geometry expected = new WKTReader().read( "MULTILINESTRING ((40 40, 45 30, 40 40), (20 35, 45 10, 31 6, 10 30, 20 35))"); assertEquals(expected, merged); }
@Test public void testConflictRemovingPoints() throws Exception { Geometry oldGeom = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); Geometry newGeom = new WKTReader() .read("MULTILINESTRING ((40 40, 45 30, 40 40),(20 35, 45 10, 30 5, 10 30, 20 35))"); GeometryAttributeDiff diff = new GeometryAttributeDiff(oldGeom, newGeom); Geometry newGeom2 = new WKTReader().read( "MULTILINESTRING ((40 40, 20 45, 45 30, 40 40),(20 35, 45 10, 31 6, 10 30, 20 35))"); GeometryAttributeDiff diff2 = new GeometryAttributeDiff(oldGeom, newGeom2); // the diff reports a conflict assertTrue(diff.conflicts(diff2)); // yet it can be applied for patching assertTrue(diff2.canBeAppliedOn(newGeom)); Geometry merged = diff2.applyOn(newGeom); assertNotNull(merged); Geometry expected = new WKTReader().read( "MULTILINESTRING ((40 40, 45 30, 40 40), (20 35, 45 10, 31 6, 10 30, 20 35))"); assertEquals(expected, merged); }
@Test public void testModifyFeatureAttributePatch() throws Exception { Patch patch = new Patch(); String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID()); Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap(); Object oldValue = points1.getProperty("sp").getValue(); GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(oldValue, "new"); Geometry oldGeometry = (Geometry) points1.getProperty("pp").getValue(); Geometry newGeometry = new WKTReader2().read("POINT (2 2)"); GeometryAttributeDiff geomDiff = new GeometryAttributeDiff(oldGeometry, newGeometry); map.put(pointsType.getDescriptor("sp"), diff); map.put(pointsType.getDescriptor("pp"), geomDiff); FeatureDiff feaureDiff = new FeatureDiff(path, map, RevFeatureType.builder().type(pointsType).build(), RevFeatureType.builder().type(pointsType).build()); patch.addModifiedFeature(feaureDiff); testPatch(patch); }