Jody Garnett ha scritto:
Hi Andrea:

That approach works for me; however I am a bit worried that the check
will be fragile.

Could the method be phrased as:
FeatureTypes.equals( type1, type2, keys )

And only the user map keys mentioned would be involved in the
comparison? This would allow you to exactly represent the check you
want to perform.

This would defeat the purpose I've written the patch for.

The retyper is generic, does not know what it's retyping:
http://svn.osgeo.org/geotools/branches/2.5.x/modules/library/main/src/main/java/org/geotools/data/ReTypeFeatureReader.java

(you can get a patch that transform is, along with FeatureTypes,
attached to this message).

Cheers
Andrea

--
Andrea Aime
OpenGeo - http://opengeo.org
Expert service straight from the developers.
Index: main/src/main/java/org/geotools/feature/FeatureTypes.java
===================================================================
--- main/src/main/java/org/geotools/feature/FeatureTypes.java	(revisione 33293)
+++ main/src/main/java/org/geotools/feature/FeatureTypes.java	(copia locale)
@@ -23,6 +23,7 @@
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import org.geotools.factory.FactoryRegistryException;
@@ -444,30 +445,49 @@
 
     /** Exact equality based on typeNames, namespace, attributes and ancestors */
     public static boolean equals( SimpleFeatureType typeA, SimpleFeatureType typeB ) {
+        return equals(typeA, typeB, false);
+    }
+    
+    /** Exact equality based on typeNames, namespace, attributes and ancestors */
+    public static boolean equals( SimpleFeatureType typeA, SimpleFeatureType typeB, boolean compareUserMaps) {
         if (typeA == typeB)
             return true;
 
         if (typeA == null || typeB == null) {
             return false;
         }
+        
+        if(compareUserMaps) {
+            if(!equals(typeA.getUserData(), typeB.getUserData()))
+                return false;
+        }
+        
         return equalsId(typeA, typeB)
-                && equals(typeA.getAttributeDescriptors(), typeB.getAttributeDescriptors()) &&
+                && equals(typeA.getAttributeDescriptors(), typeB.getAttributeDescriptors(), compareUserMaps) &&
                 equalsAncestors( typeA, typeB );
     }
-
-    public static boolean equals( List attributesA, List attributesB ) {
+    
+    public static boolean equals( List attributesA, List attributesB, boolean compareUserMaps) {
         return equals(
             (AttributeDescriptor[]) attributesA.toArray(new AttributeDescriptor[attributesA.size()]),
-            (AttributeDescriptor[]) attributesB.toArray(new AttributeDescriptor[attributesB.size()])
-        );
+            (AttributeDescriptor[]) attributesB.toArray(new AttributeDescriptor[attributesB.size()]), 
+            compareUserMaps);
     }
 
+    public static boolean equals( List attributesA, List attributesB) {
+        return equals(attributesA, attributesB, false);
+    }
+    
     public static boolean equals( AttributeDescriptor attributesA[], AttributeDescriptor attributesB[] ) {
+        return equals(attributesA, attributesB, false);
+    }
+
+    public static boolean equals( AttributeDescriptor attributesA[], AttributeDescriptor attributesB[], boolean compareUserMaps ) {
         if (attributesA.length != attributesB.length)
             return false;
 
         for( int i = 0, length = attributesA.length; i < length; i++ ) {
-            if (!equals(attributesA[i], attributesB[i]))
+            if (!equals(attributesA[i], attributesB[i], compareUserMaps))
                 return false;
         }
         return true;
@@ -482,7 +502,7 @@
      * @param typeB
      */
     public static boolean equalsAncestors( SimpleFeatureType typeA, SimpleFeatureType typeB ) {
-        return ancestors( typeA ).equals( typeB );
+        return ancestors( typeA ).equals( ancestors(typeB) );
     }
 
     public static Set ancestors( SimpleFeatureType featureType ) {
@@ -491,10 +511,54 @@
         }
         return new HashSet(getAncestors(featureType));
     }
+    
+    public static boolean equals( AttributeDescriptor a, AttributeDescriptor b) {
+        return equals(a, b, false);
+    }
 
-    public static boolean equals( AttributeDescriptor a, AttributeDescriptor b ) {
-        return a == b || (a != null && a.equals(b));
+    public static boolean equals( AttributeDescriptor a, AttributeDescriptor b, boolean compareUserMaps) {
+        if(a == b)
+            return true;
+        
+        if(a == null)
+            return true;
+        
+        if(!a.equals(b))
+            return false;
+        
+        if(compareUserMaps) {
+            if(!equals(a.getUserData(), b.getUserData()))
+                return false;
+            if(!equals(a.getType().getUserData(), b.getType().getUserData()))
+                return false;
+        }
+        
+        return true;
+            
     }
+    
+    /**
+     * Tolerant map comparison. Two maps are considered to be equal if they express the
+     * same content. So for example two null maps are equal, but also a null and an 
+     * empty one are
+     */
+    static boolean equals(Map a, Map b) {
+        if(a == b)
+            return true;
+        
+        // null == null handled above
+        if(a == null || b == null)
+            return false;
+        
+        if(a != null && a.size() == 0 && b == null)
+            return true;
+        
+        if(b != null && b.size() == 0 && a == null)
+            return true;
+        
+        return a.equals(b);
+    }
+    
     /** Quick check of namespace and typename */
     public static boolean equalsId( SimpleFeatureType typeA, SimpleFeatureType typeB ) {
         if (typeA == typeB)
@@ -513,6 +577,9 @@
 
         String namespaceA = typeA.getName().getNamespaceURI();
         String namespaceB = typeB.getName().getNamespaceURI();
+        if(namespaceA == null && namespaceB == null)
+            return true;
+        
         if (namespaceA == null && namespaceB != null)
             return false;
         else if (!namespaceA.equals(namespaceB))
Index: main/src/main/java/org/geotools/data/ReTypeFeatureReader.java
===================================================================
--- main/src/main/java/org/geotools/data/ReTypeFeatureReader.java	(revisione 33293)
+++ main/src/main/java/org/geotools/data/ReTypeFeatureReader.java	(copia locale)
@@ -18,6 +18,8 @@
 
 import java.io.IOException;
 import java.util.NoSuchElementException;
+
+import org.geotools.feature.FeatureTypes;
 import org.geotools.feature.simple.SimpleFeatureBuilder;
 import org.geotools.resources.Classes;
 import org.opengis.feature.IllegalAttributeException;
@@ -118,7 +120,7 @@
      */
     protected AttributeDescriptor[] typeAttributes(SimpleFeatureType target,
         SimpleFeatureType origional) {
-        if (target.equals(origional)) {
+        if (FeatureTypes.equals(origional, target, true)) {
             throw new IllegalArgumentException(
                 "FeatureReader allready produces contents with the correct schema");
         }
------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Geotools-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to