Revision: 1686
          http://svn.sourceforge.net/spring-rich-c/?rev=1686&view=rev
Author:   jhoskens
Date:     2007-01-24 00:58:25 -0800 (Wed, 24 Jan 2007)

Log Message:
-----------
Added ConditionalPropertyConstraint which provides if...then...else pattern
applied to several properties (not a propertyValue)

Provided factory methods in Constraints

Modified Paths:
--------------
    
trunk/spring-richclient/support/src/main/java/org/springframework/rules/factory/Constraints.java

Added Paths:
-----------
    
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/property/ConditionalPropertyConstraint.java

Added: 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/property/ConditionalPropertyConstraint.java
===================================================================
--- 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/property/ConditionalPropertyConstraint.java
                              (rev 0)
+++ 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/constraint/property/ConditionalPropertyConstraint.java
      2007-01-24 08:58:25 UTC (rev 1686)
@@ -0,0 +1,123 @@
+package org.springframework.rules.constraint.property;
+
+import org.springframework.binding.PropertyAccessStrategy;
+import org.springframework.rules.constraint.IfTrue;
+import org.springframework.util.Assert;
+
+/**
+ * <p>
+ * Provides a way to trigger rules for propertyB when propertyA satisfies a
+ * certain condition:
+ * </p>
+ * 
+ * <pre>
+ *     if (propertyA satisfies the conditional constraint)
+ *     {
+ *             check the rules for propertyB
+ *     }
+ * </pre>
+ * 
+ * with an optional part:
+ * 
+ * <pre>
+ *  else
+ *  {
+ *      check the rules for propertyC
+ *  }
+ * </pre>
+ * 
+ * <p>
+ * More complex situations are possible by using compound constraints which
+ * leverages the previous to:
+ * </p>
+ * 
+ * <pre>
+ * if (constraint(propertyA, propertyB,...) == true)
+ * {
+ *     checkConstraint(property1, property2,...);
+ * }
+ * \\ optional part
+ * else
+ * {
+ *     checkConstraint(propertyX, propertyY,...);
+ * }
+ * </pre>
+ * 
+ * <p>
+ * This class can be compared to the [EMAIL PROTECTED] IfTrue} class: it 
applies the same
+ * pattern but on different <b>properties</b> instead of on a <b>property 
value</b>.
+ * </p>
+ * 
+ * @author jh
+ * 
+ */
+public class ConditionalPropertyConstraint extends AbstractPropertyConstraint {
+
+       /** The condition which triggers further rules to be checked. */
+       private final PropertyConstraint ifConstraint;
+
+       /** The constraint to be checked when the condition is satisfied. */
+       private final PropertyConstraint thenConstraint;
+
+       /** The constraint to be checked when the condition is <b>NOT</b> 
satisfied. */
+       private final PropertyConstraint elseConstraint;
+
+       /**
+        * Create a constraint which simulates the if...then pattern applied
+        * on separate properties.
+        * 
+        * @param ifConstraint the PropertyConstraint that triggers the test
+        * (satisfying a certain condition).
+        * @param thenConstraint the PropertyConstraint to test in the specified
+        * condition.
+        */
+       public ConditionalPropertyConstraint(PropertyConstraint ifConstraint, 
PropertyConstraint thenConstraint) {
+               this(ifConstraint, thenConstraint, null);
+       }
+       
+       /**
+        * Create a constraint which simulates the if...then...else pattern 
applied
+        * on separate properties.
+        * 
+        * @param ifConstraint the PropertyConstraint that triggers the test
+        * (satisfying a certain condition).
+        * @param thenConstraint the PropertyConstraint to test in the specified
+        * condition.
+        * @param elseConstraint the PropertyConstraint to test if the 
condition is
+        * <b>NOT</b> satisfied. May be <code>null</code>.
+        */
+       public ConditionalPropertyConstraint(PropertyConstraint ifConstraint, 
PropertyConstraint thenConstraint,
+                       PropertyConstraint elseConstraint) {
+               super(ifConstraint.getPropertyName());
+               Assert.notNull(ifConstraint);
+               Assert.notNull(thenConstraint);
+               this.ifConstraint = ifConstraint;
+               this.thenConstraint = thenConstraint;
+               this.elseConstraint = elseConstraint;
+       }
+
+       @Override
+       public boolean isCompoundRule() {
+               return true;
+       }
+
+       @Override
+       public boolean isDependentOn(String propertyName) {
+               if (elseConstraint == null)
+                       return ifConstraint.isDependentOn(propertyName) || 
thenConstraint.isDependentOn(propertyName);
+
+               return ifConstraint.isDependentOn(propertyName) || 
thenConstraint.isDependentOn(propertyName)
+                               || elseConstraint.isDependentOn(propertyName);
+       }
+
+       @Override
+       protected boolean test(PropertyAccessStrategy 
domainObjectAccessStrategy) {
+               if (ifConstraint.test(domainObjectAccessStrategy))
+                       return thenConstraint.test(domainObjectAccessStrategy);
+               if (elseConstraint != null)
+                       return elseConstraint.test(domainObjectAccessStrategy);
+
+               return true;
+       }
+
+}

Modified: 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/factory/Constraints.java
===================================================================
--- 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/factory/Constraints.java
    2007-01-22 14:53:27 UTC (rev 1685)
+++ 
trunk/spring-richclient/support/src/main/java/org/springframework/rules/factory/Constraints.java
    2007-01-24 08:58:25 UTC (rev 1686)
@@ -44,6 +44,7 @@
 import org.springframework.rules.constraint.XOr;
 import org.springframework.rules.constraint.Like.LikeType;
 import 
org.springframework.rules.constraint.property.CompoundPropertyConstraint;
+import 
org.springframework.rules.constraint.property.ConditionalPropertyConstraint;
 import org.springframework.rules.constraint.property.NegatedPropertyConstraint;
 import 
org.springframework.rules.constraint.property.ParameterizedPropertyConstraint;
 import org.springframework.rules.constraint.property.PropertiesConstraint;
@@ -319,13 +320,57 @@
     public Constraint ifTrue(Constraint constraint, Constraint mustAlsoBeTrue, 
Constraint elseMustAlsoBeTrue) {
         return new IfTrue(constraint, mustAlsoBeTrue, elseMustAlsoBeTrue);
     }
+    
+    /**
+        * Returns a ConditionalPropertyConstraint: one property will trigger 
the
+        * validation of another.
+        * 
+        * @see ConditionalPropertyConstraint
+        */
+       public PropertyConstraint ifTrue(PropertyConstraint ifConstraint, 
PropertyConstraint thenConstraint) {
+               return new ConditionalPropertyConstraint(ifConstraint, 
thenConstraint);
+       }
+    
+    /**
+        * Returns a ConditionalPropertyConstraint: one property will trigger 
the
+        * validation of another.
+        * 
+        * @see ConditionalPropertyConstraint
+        */
+       public PropertyConstraint ifTrue(PropertyConstraint ifConstraint, 
PropertyConstraint thenConstraint,
+                       PropertyConstraint elseConstraint) {
+               return new ConditionalPropertyConstraint(ifConstraint, 
thenConstraint, elseConstraint);
+       }
+       
+       /**
+        * Returns a ConditionalPropertyConstraint: one property will trigger 
the
+        * validation of another.
+        * 
+        * @see ConditionalPropertyConstraint
+        */
+       public PropertyConstraint ifTrue(PropertyConstraint ifConstraint, 
PropertyConstraint[] thenConstraints) {
+               return new ConditionalPropertyConstraint(ifConstraint, new 
CompoundPropertyConstraint(new And(thenConstraints)));
+       }
+    
+    /**
+        * Returns a ConditionalPropertyConstraint: one property will trigger 
the
+        * validation of another.
+        * 
+        * @see ConditionalPropertyConstraint
+        */
+       public PropertyConstraint ifTrue(PropertyConstraint ifConstraint, 
PropertyConstraint[] thenConstraints,
+                       PropertyConstraint[] elseConstraints) {
+               return new ConditionalPropertyConstraint(ifConstraint,
+                               new CompoundPropertyConstraint(new 
And(thenConstraints)), new CompoundPropertyConstraint(new And(
+                                               elseConstraints)));
+       }
 
     /**
-     * Returns a maxlength constraint.
-     * 
-     * @param maxLength The maximum length in characters.
-     * @return The configured maxlength constraint.
-     */
+        * Returns a maxlength constraint.
+        * 
+        * @param maxLength The maximum length in characters.
+        * @return The configured maxlength constraint.
+        */
     public Constraint maxLength(int maxLength) {
         return new StringLengthConstraint(maxLength);
     }


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
spring-rich-c-cvs mailing list
spring-rich-c-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs

Reply via email to