changeset 1635:1b5204203e18

Minor refactoring, documentation. flys-artifacts/trunk@2814 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 23 Sep 2011 07:19:41 +0000
parents cc47828a1390
children c2edf0032cf8
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java
diffstat 2 files changed, 45 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Sep 23 04:21:16 2011 +0000
+++ b/flys-artifacts/ChangeLog	Fri Sep 23 07:19:41 2011 +0000
@@ -1,3 +1,12 @@
+2011-09-23  Felix Wolfsteller <felix.wolfsteller@intevation.de>
+
+ Refactoring, doc.
+
+	* src/main/java/de/intevation/flys/collections/AttributeWriter.java:
+	  (mergeFacets): Removed, replaced in parts by pickFacet.
+	  (pickFacet): New, return facet to be added to document.
+	  Documentation added.
+
 2011-09-23  Felix Wolfsteller <felix.wolfsteller@intevation.de>
 
  Cosmetics, docs.
--- a/flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java	Fri Sep 23 04:21:16 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java	Fri Sep 23 07:19:41 2011 +0000
@@ -122,64 +122,57 @@
         Document       doc,
         ElementCreator cr,
         Element        output,
-        List<Facet>    a, /* new facets */
-        List<Facet>    b) /* old facets */
+        List<Facet>    newFacets,
+        List<Facet>    oldFacets)
     {
-        int num = a.size();
+        int num = newFacets.size();
 
+        // Add all new Facets either in their old state or (if really
+        // new) as they are.
         for (int i = 0; i < num; i++) {
-            ManagedFacet fA = (ManagedFacet) a.get(i);
+            ManagedFacet facet = (ManagedFacet) newFacets.get(i);
 
-            if (!mergeFacets(doc, cr, output, fA, b)) {
-                Node n = fA.toXML(doc);
-
-                if (n != null) {
-                    output.appendChild(n);
-                }
+            ManagedFacet picked = pickFacet(facet, oldFacets);
+            Node         node   = picked.toXML(doc);
+            if (node != null) {
+                output.appendChild(node);
             }
         }
     }
 
 
     /**
-     * @param a    new facets
-     * @param list old facets
+     * Returns the facet to be added to Document.
+     * Return the new facet only if the "same" facet was not present before.
+     * Return the "old" facet otherwise (user-defined information sticks
+     * to it).
+     * @param facet     the new facet.
+     * @param oldFacets the old facets, new facet is compared against each of
+     *                  these.
+     * @return facet if genuinely new, matching old facet otherwise.
      */
-    protected boolean mergeFacets(
-        Document       doc,
-        ElementCreator cr,
-        Element        output,
-        ManagedFacet   a,    /* new facets */
-        List<Facet>    list) /* old facets */
-    {
-        String nameA = a.getName() + a.getIndex();
-
-        if (list == null) {
-            logger.debug("No old facets found.");
-            return false;
+    protected ManagedFacet pickFacet(ManagedFacet facet,
+         List<Facet> oldFacets) {
+        if (oldFacets == null) {
+            logger.debug("No old facets to compare a new to found.");
+            return facet;
         }
+       
+        String hash = facet.getName() + facet.getIndex() + facet.getArtifact();
 
-        for (Facet facet: list) {
-            String nameB = facet.getName() + facet.getIndex();
-
-            if (nameA.equals(nameB)) {
-                ManagedFacet b = (ManagedFacet) facet;
-
-                if (!b.getArtifact().equals(a.getArtifact())) {
-                    continue;
-                }
-
-                Node n = facet.toXML(doc);
-
-                if (n != null) {
-                    output.appendChild(n);
-                }
-
-                return true;
+        // Compare "new" facet with all old facets.
+        // Take oldFacet if that facet was already present (otherwise
+        // information is lost, the new one otherwise.
+        for (Facet oFacet: oldFacets) {
+            ManagedFacet oldFacet = (ManagedFacet) oFacet;
+            String oldHash = oldFacet.getName()
+                           + oldFacet.getIndex()
+                           + oldFacet.getArtifact();
+            if (hash.equals(oldHash)) {
+                return oldFacet;
             }
         }
-
-        return false;
+        return facet;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org