changeset 1:9c42f25cd944

use a Difference object to describe the diff
author Dirk Olmes <dirk@xanthippe.ping.de>
date Mon, 12 Sep 2011 12:03:28 +0200
parents 6f11757c4811
children ebff95a55276
files conflict-editor/src/main/java/de/codedo/conflicteditor/ConflictsView.java conflict-editor/src/main/java/de/codedo/conflicteditor/Difference.java conflict-editor/src/main/java/de/codedo/conflicteditor/DocumentMatcher.java conflict-editor/src/test/java/de/codedo/conflicteditor/Playground.java
diffstat 4 files changed, 50 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/ConflictsView.java	Mon Sep 12 11:47:48 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/ConflictsView.java	Mon Sep 12 12:03:28 2011 +0200
@@ -9,6 +9,9 @@
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.map.ObjectMapper;
 
+/**
+ * Creates a temporary view in the database to retrieve all documents that have conflicts.
+ */
 public class ConflictsView extends Object
 {
     // @formatter:off
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/Difference.java	Mon Sep 12 12:03:28 2011 +0200
@@ -0,0 +1,31 @@
+
+package de.codedo.conflicteditor;
+
+public class Difference extends Object
+{
+    public final String key;
+    public final Object currentValue;
+    public final Object otherValue;
+
+    @SuppressWarnings("hiding")
+    public Difference(String key, Object currentValue, Object otherValue)
+    {
+        super();
+        this.key = key;
+        this.currentValue = currentValue;
+        this.otherValue = otherValue;
+    }
+
+    @Override
+    public String toString()
+    {
+        StringBuilder buf = new StringBuilder(128);
+        buf.append(key);
+        buf.append(" : \"");
+        buf.append(currentValue);
+        buf.append("\" - \"");
+        buf.append(otherValue);
+        buf.append("\"");
+        return buf.toString();
+    }
+}
--- a/conflict-editor/src/main/java/de/codedo/conflicteditor/DocumentMatcher.java	Mon Sep 12 11:47:48 2011 +0200
+++ b/conflict-editor/src/main/java/de/codedo/conflicteditor/DocumentMatcher.java	Mon Sep 12 12:03:28 2011 +0200
@@ -1,10 +1,12 @@
 
 package de.codedo.conflicteditor;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 
 import org.codehaus.jackson.JsonNode;
@@ -27,18 +29,24 @@
         _other = other;
     }
 
-    public void compare()
+    public List<Difference> compare()
     {
+        List<Difference> differences = new ArrayList<Difference>();
+
         Set<String> originalFieldNames = getFieldNames(_original);
         for (String name : originalFieldNames)
         {
+            // TODO better handling of values - could be of non-string type really
             Object originalValue = _original.findValue(name).getValueAsText();
             Object otherValue = _other.findValue(name).getValueAsText();
             if (originalValue.equals(otherValue) == false)
             {
-                System.out.printf("%s : \"%s\" - \"%s\"\n", name, originalValue, otherValue);
+                Difference diff = new Difference(name, originalValue, otherValue);
+                differences.add(diff);
             }
         }
+
+        return differences;
     }
 
     private Set<String> getFieldNames(JsonNode node)
--- a/conflict-editor/src/test/java/de/codedo/conflicteditor/Playground.java	Mon Sep 12 11:47:48 2011 +0200
+++ b/conflict-editor/src/test/java/de/codedo/conflicteditor/Playground.java	Mon Sep 12 12:03:28 2011 +0200
@@ -6,6 +6,7 @@
 import java.io.PrintStream;
 import java.io.Reader;
 import java.net.URL;
+import java.util.List;
 
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.map.ObjectMapper;
@@ -46,7 +47,11 @@
 
     private static void compare(JsonNode currentDocument, JsonNode conflictDocument)
     {
-        new DocumentMatcher(currentDocument, conflictDocument).compare();
+        List<Difference> result = new DocumentMatcher(currentDocument, conflictDocument).compare();
+        for (Difference diff : result)
+        {
+            System.out.println(diff);
+        }
     }
 
     private static void prettyPrint(JsonNode node) throws IOException