Update of /var/cvs/src/org/mmbase/datatypes
In directory james.mmbase.org:/tmp/cvs-serv6366

Modified Files:
        BasicDataType.java NumberDataType.java 
Log Message:
MMB-1700, Failing test-cases


See also: http://cvs.mmbase.org/viewcvs/src/org/mmbase/datatypes
See also: http://www.mmbase.org/jira/browse/MMB-1700


Index: BasicDataType.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/datatypes/BasicDataType.java,v
retrieving revision 1.93
retrieving revision 1.94
diff -u -b -r1.93 -r1.94
--- BasicDataType.java  9 Aug 2008 09:22:59 -0000       1.93
+++ BasicDataType.java  12 Aug 2008 17:24:12 -0000      1.94
@@ -40,7 +40,7 @@
  * @author Pierre van Rooden
  * @author Michiel Meeuwissen
  * @since  MMBase-1.8
- * @version $Id: BasicDataType.java,v 1.93 2008/08/09 09:22:59 michiel Exp $
+ * @version $Id: BasicDataType.java,v 1.94 2008/08/12 17:24:12 michiel Exp $
  */
 
 public class BasicDataType<C> extends AbstractDescriptor implements 
DataType<C>, Cloneable, Comparable<DataType<C>>, Descriptor {
@@ -290,7 +290,7 @@
      * No need to override this. It is garantueed by javadoc that cast should 
work out of preCast
      * using Casting.toType. So that is what this final implementation is 
doing.
      *
-     * Override [EMAIL PROTECTED] #preCast(Object, Cloud, Node, Field)}
+     * Override [EMAIL PROTECTED] #cast(Object, Cloud, Node, Field)}
      */
     public final C cast(Object value, final Node node, final Field field) {
         if (origin != null && (! 
origin.getClass().isAssignableFrom(getClass()))) {
@@ -298,7 +298,7 @@
             // e.g. if origin is Date, but actual type is integer, then 
casting of 'today' works now.
             value = origin.cast(value, node, field);
         }
-        Cloud cloud = getCloud(node, field);
+        Cloud cloud = getCloud(getCloud(node, field));
         try {
             return cast(value, cloud, node, field);
         } catch (CastException ce) {
@@ -318,17 +318,23 @@
     }
 
     protected final Cloud getCloud(Node node, Field field) {
-        if (node != null) return node.getCloud();
-        if (field != null) return field.getNodeManager().getCloud();
+        if (node != null) {
+            log.trace("Using cloud of node");
+            return node.getCloud();
+        }
+        if (field != null) {
+            return field.getNodeManager().getCloud();
+        }
         return null;
     }
 
     /**
+     * Returns a cloud object if argument is <code>null</code>. Otherwise the 
argument.
      * @since MMBase-1.8.6
      */
     protected Cloud getCloud(Cloud cloud) {
         if (cloud == null) {
-            log.debug("No cloud found");
+            log.trace("No cloud found");
             cloud = org.mmbase.bridge.util.CloudThreadLocal.currentCloud();
         }
         if (cloud == null) {
@@ -345,7 +351,7 @@
      * cast to Number.
      */
     protected Object castToValidate(Object value, Node node, Field field) 
throws CastException {
-        return cast(value, getCloud(node, field), node, field);
+        return cast(value, getCloud(getCloud(node, field)), node, field);
     }
 
     /**


Index: NumberDataType.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/datatypes/NumberDataType.java,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -b -r1.29 -r1.30
--- NumberDataType.java 12 Aug 2008 16:11:36 -0000      1.29
+++ NumberDataType.java 12 Aug 2008 17:24:12 -0000      1.30
@@ -22,7 +22,7 @@
  * A DataType representing some kind of numeric value, like a floating point 
number or an integer number.
  *
  * @author Pierre van Rooden
- * @version $Id: NumberDataType.java,v 1.29 2008/08/12 16:11:36 michiel Exp $
+ * @version $Id: NumberDataType.java,v 1.30 2008/08/12 17:24:12 michiel Exp $
  * @since MMBase-1.8
  */
 abstract public class NumberDataType<E extends Number&Comparable<E>> extends 
ComparableDataType<E> {
@@ -38,9 +38,12 @@
 
 
     protected Number castString(Object preCast, Cloud cloud) throws 
CastException {
+        if (preCast == null) return null;
          if (preCast instanceof String) {
-             Locale l = cloud.getLocale();
+            Locale l = cloud != null ? cloud.getLocale() : Locale.getDefault();
              NumberFormat nf = NumberFormat.getNumberInstance(l);
+            nf.setGroupingUsed(false); // we never want to parse e.g. "1.2" to 
"12". It simply makes
+                                       // no sense, and hard to make backwards 
compatible
              ParsePosition p = new ParsePosition(0);
              String s = (String) preCast;
              Number number =  nf.parse(s, p);
@@ -49,10 +52,13 @@
              }
              if (p.getIndex() < s.length() || p.getErrorIndex() >= 0) {
                  log.debug("Not correct, falling back to toDouble");
-                 if (! StringDataType.DOUBLE_PATTERN.matcher((String) 
preCast).matches()) {
-                     throw new CastException("Not a number: " + preCast);
+                 if (! StringDataType.DOUBLE_PATTERN.matcher(s).matches()) {
+                     log.debug("Not a valid double");
+                     throw new CastException("Not a number: " + s);
+                 } else {
+                     log.debug("Casting to double " + s);
+                     return Casting.toDouble(s);
                  }
-                 return Casting.toDouble(preCast);
              }
              return number;
          }
@@ -65,9 +71,18 @@
     /**
      * @since MMBase-1.9
      */
+    @Override
     protected Object castToValidate(Object value, Node node, Field field) 
throws CastException {
         if (value == null) return null;
         Object preCast = preCast(value, node, field); // resolves enumerations
         return castString(preCast, getCloud(getCloud(node, field)));
     }
+
+    @Override
+    protected E cast(Object value, Cloud cloud, Node node, Field field) throws 
CastException {
+        Number preCast = castString(preCast(value, cloud, node, field), cloud);
+        if (preCast == null) return null;
+        E cast = Casting.toType(getTypeAsClass(), cloud, preCast);
+        return cast;
+    }
 }
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to