You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by df...@apache.org on 2004/12/30 16:05:53 UTC
cvs commit: jakarta-commons-sandbox/contract/src/java/org/apache/commons/contract/constraints Evaluatable.java MapConstraints.java DateConstraints.java ArrayConstraints.java ListConstraints.java
dflorey 2004/12/30 07:05:53
Modified: contract/src/java/org/apache/commons/contract/constraints
MapConstraints.java DateConstraints.java
ArrayConstraints.java ListConstraints.java
Added: contract/src/java/org/apache/commons/contract/constraints
Evaluatable.java
Log:
Added support for dynamic values that need to be evaluated before casting
Revision Changes Path
1.3 +15 -1 jakarta-commons-sandbox/contract/src/java/org/apache/commons/contract/constraints/MapConstraints.java
Index: MapConstraints.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/contract/src/java/org/apache/commons/contract/constraints/MapConstraints.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- MapConstraints.java 18 Dec 2004 13:52:16 -0000 1.2
+++ MapConstraints.java 30 Dec 2004 15:05:53 -0000 1.3
@@ -56,7 +56,7 @@
}
}
- private Map castedMap(Map map, Context context) throws CastException {
+ protected Map castedMap(Map map, Context context) throws CastException {
Map castedMap = new HashMap(map);
for ( Iterator i = entryConstraints.iterator(); i.hasNext(); ) {
ParameterDescriptor parameterDescriptor = (ParameterDescriptor)i.next();
@@ -66,6 +66,13 @@
for ( Iterator j = castedMap.entrySet().iterator(); j.hasNext(); ) {
Map.Entry entry = (Map.Entry)j.next();
Object value = entry.getValue();
+ if ( value instanceof Evaluatable ) {
+ try {
+ value = ((Evaluatable)value).evaluate(context);
+ } catch (Exception e) {
+ throw new CastException(new LocalizedError("evaluatingAnyFailed"), e);
+ }
+ }
castedMap.put(entry.getKey(), entryDescriptor.cast(value, context));
}
} else {
@@ -78,6 +85,13 @@
if ( object == null && parameterDescriptor.getDefaultValue() == null || object.equals(parameterDescriptor.getDefaultValue())) {
castedMap.put(key, object);
} else {
+ if ( object instanceof Evaluatable ) {
+ try {
+ object = ((Evaluatable)object).evaluate(context);
+ } catch (Exception e) {
+ throw new CastException(new LocalizedError("evaluatingAnyFailed"), e);
+ }
+ }
castedMap.put(key, entryDescriptor.cast(object, context));
}
}
1.3 +16 -1 jakarta-commons-sandbox/contract/src/java/org/apache/commons/contract/constraints/DateConstraints.java
Index: DateConstraints.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/contract/src/java/org/apache/commons/contract/constraints/DateConstraints.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DateConstraints.java 18 Dec 2004 13:52:16 -0000 1.2
+++ DateConstraints.java 30 Dec 2004 15:05:53 -0000 1.3
@@ -2,6 +2,7 @@
import java.text.DateFormat;
import java.text.ParseException;
+import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -16,11 +17,17 @@
protected boolean constrained;
protected List allowedValues = new ArrayList();
protected Date earliest, latest;
+ protected String formatPattern;
public DateConstraints() {
this.constrained = false;
}
+ public DateConstraints(String formatPattern) {
+ this.constrained = false;
+ this.formatPattern = formatPattern;
+ }
+
public DateConstraints(Date earliest, Date latest) {
constrained = true;
this.earliest = earliest;
@@ -39,6 +46,10 @@
this.latest = latest;
}
+ public void setFormatPattern(String dateFormat) {
+ this.formatPattern = dateFormat;
+ }
+
public Object cast(Object value, Context context) throws CastException {
Date date = null;
if ( value instanceof Date) {
@@ -50,7 +61,11 @@
date = new Date(Long.valueOf(valueAsString).longValue());
} catch ( NumberFormatException exception ) {
try {
- date = DateFormat.getInstance().parse(valueAsString);
+ if ( formatPattern == null ) {
+ date = DateFormat.getInstance().parse(valueAsString);
+ } else {
+ date = new SimpleDateFormat(formatPattern).parse(valueAsString);
+ }
} catch (ParseException e) {
throw new CastException(new LocalizedError("uncastableDateValue", new Object[] { value }), e);
}
1.3 +14 -0 jakarta-commons-sandbox/contract/src/java/org/apache/commons/contract/constraints/ArrayConstraints.java
Index: ArrayConstraints.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/contract/src/java/org/apache/commons/contract/constraints/ArrayConstraints.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ArrayConstraints.java 18 Dec 2004 13:52:16 -0000 1.2
+++ ArrayConstraints.java 30 Dec 2004 15:05:53 -0000 1.3
@@ -40,6 +40,13 @@
int counter = 0;
for ( Iterator i = ((List)value).iterator(); i.hasNext(); ) {
Object entry = i.next();
+ if ( entry instanceof Evaluatable ) {
+ try {
+ entry = ((Evaluatable)entry).evaluate(context);
+ } catch (Exception e) {
+ throw new CastException(new LocalizedError("evaluatingAnyFailed"), e);
+ }
+ }
array[counter] = entryValueDescriptor.cast(entry, context);
counter++;
}
@@ -47,6 +54,13 @@
array = new Object[((Object[])value).length];
for ( int i = 0; i < array.length; i++ ) {
Object entry = ((Object[])value)[i];
+ if ( entry instanceof Evaluatable ) {
+ try {
+ entry = ((Evaluatable)entry).evaluate(context);
+ } catch (Exception e) {
+ throw new CastException(new LocalizedError("evaluatingAnyFailed"), e);
+ }
+ }
array[i] = entryValueDescriptor.cast(entry, context);
}
} else {
1.3 +14 -0 jakarta-commons-sandbox/contract/src/java/org/apache/commons/contract/constraints/ListConstraints.java
Index: ListConstraints.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/contract/src/java/org/apache/commons/contract/constraints/ListConstraints.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ListConstraints.java 18 Dec 2004 13:52:16 -0000 1.2
+++ ListConstraints.java 30 Dec 2004 15:05:53 -0000 1.3
@@ -36,11 +36,25 @@
} else if ( value instanceof List ) {
for ( Iterator i = ((List)value).iterator(); i.hasNext(); ) {
Object entry = i.next();
+ if ( entry instanceof Evaluatable ) {
+ try {
+ entry = ((Evaluatable)entry).evaluate(context);
+ } catch (Exception e) {
+ throw new CastException(new LocalizedError("evaluatingAnyFailed"), e);
+ }
+ }
list.add(entryValueDescriptor.cast(entry, context));
}
} else if ( value instanceof Object[] ) {
for ( int i = 0; i < ((Object [])value).length; i++ ) {
Object entry = ((Object [])value)[i];
+ if ( entry instanceof Evaluatable ) {
+ try {
+ entry = ((Evaluatable)entry).evaluate(context);
+ } catch (Exception e) {
+ throw new CastException(new LocalizedError("evaluatingAnyFailed"), e);
+ }
+ }
list.add(entryValueDescriptor.cast(entry, context));
}
} else {
1.1 jakarta-commons-sandbox/contract/src/java/org/apache/commons/contract/constraints/Evaluatable.java
Index: Evaluatable.java
===================================================================
/*
*
* ====================================================================
*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.commons.contract.constraints;
import org.apache.commons.contract.Context;
/**
* @author Daniel Florey
*
*/
public interface Evaluatable {
public Object evaluate(Context context) throws Exception;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org