Author: michiel
Date: 2009-12-14 16:28:08 +0100 (Mon, 14 Dec 2009)
New Revision: 40242

Modified:
   mmbase/trunk/core/src/main/java/org/mmbase/datatypes/BasicDataType.java
   mmbase/trunk/core/src/main/java/org/mmbase/datatypes/DataType.java
Log:
added a version of 'cast' which also may refuse to do it,w hich is sometimes 
more reasonable

Modified: 
mmbase/trunk/core/src/main/java/org/mmbase/datatypes/BasicDataType.java
===================================================================
--- mmbase/trunk/core/src/main/java/org/mmbase/datatypes/BasicDataType.java     
2009-12-14 15:26:49 UTC (rev 40241)
+++ mmbase/trunk/core/src/main/java/org/mmbase/datatypes/BasicDataType.java     
2009-12-14 15:28:08 UTC (rev 40242)
@@ -262,7 +262,7 @@
     public void checkType(Object value) {
         if (!isCorrectType(value)) {
             // customize this?
-            throw new IllegalArgumentException("DataType of '" + value + "' 
for '" + getName() + "' must be of type " + classType + " (but is " + (value == 
null ? value : value.getClass()) + ")");
+            throw new IllegalArgumentException("DataType of '" + 
Casting.toString(value) + "' for '" + getName() + "' must be of type " + 
classType + " (but is " + (value == null ? value : value.getClass()) + ")");
         }
     }
 
@@ -306,6 +306,19 @@
      * Override {...@link #cast(Object, Cloud, Node, Field)}
      */
     public final C cast(Object value, final Node node, final Field field) {
+        try {
+            return castOrException(value, node, field);
+        } catch (CastException ce) {
+            log.service(ce.getMessage(), ce);
+            Cloud cloud = getCloud(getCloud(node, field));
+            return Casting.toType(classType, cloud, preCast(value, cloud, 
node, field));
+        }
+    }
+
+    /**
+     * @since MMBase-2.0
+     */
+    public final C castOrException(Object value, final Node node, final Field 
field) throws CastException {
         if (origin != null && (! 
origin.getClass().isAssignableFrom(getClass()))) {
             // if inherited from incompatible type, then first try to cast in 
the way of origin.
             // e.g. if origin is Date, but actual type is integer, then 
casting of 'today' works now.
@@ -313,12 +326,7 @@
         }
         if (value == null) return null;
         Cloud cloud = getCloud(getCloud(node, field));
-        try {
-            return cast(value, cloud, node, field);
-        } catch (CastException ce) {
-            log.service(ce.getMessage(), ce);
-            return Casting.toType(classType, cloud, preCast(value, cloud, 
node, field));
-        }
+        return cast(value, cloud, node, field);
     }
 
     /**

Modified: mmbase/trunk/core/src/main/java/org/mmbase/datatypes/DataType.java
===================================================================
--- mmbase/trunk/core/src/main/java/org/mmbase/datatypes/DataType.java  
2009-12-14 15:26:49 UTC (rev 40241)
+++ mmbase/trunk/core/src/main/java/org/mmbase/datatypes/DataType.java  
2009-12-14 15:28:08 UTC (rev 40242)
@@ -76,7 +76,7 @@
     static final int ENFORCE_ONCREATE = 1000;
 
     /**
-     * Return value for {...@link DataType.Restriction#getEnforceStrength}. 
This means that ta
+     * Return value for {...@link DataType.Restriction#getEnforceStrength}. 
This means that the
      * restriction on a value must be enforced only on validation of the 
value. This means that it
      * has no influence of the validity of the <em>node</em>.
      * @todo No difference made between always/change/create for this. We 
could also add
@@ -146,6 +146,22 @@
     public C cast(Object value, Node node, Field field);
 
     /**
+     * Tries to 'cast' an object for use with this parameter. E.g. if value is 
a String, but this
+     * parameter is of type Integer, then the string can be parsed to Integer. 
 This method may thow
+     * {...@link CastException} if such casting is not feasible. E.g. most 
strings cannot be reasonably
+     * casted to numbers. The method {...@link #cast} will return 
<code>-1</code>. This one will trow exception.
+     *
+     *
+     * @param value The value to be filled in a value with this DataType.
+     * @param node  Sometimes a node might be needed.
+     * @param field Sometimes a (or 'the') field might be needed.
+     * @return casted object of Class of this DataType
+     * @throws ClassException
+     * @since MMBase-2.0
+     */
+    public C castOrException(Object value, final Node node, final Field field) 
throws CastException;
+
+    /**
      * Before actually 'cast' an object to the right type, it may undergo some 
conversion by the
      * datatype, e.g. enumerations may get resolved (enumerations have the 
feature that they can
      * e.g. resolve java-constants to their values).

_______________________________________________
Cvs mailing list
Cvs@lists.mmbase.org
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to