# Thread: Integer Constraints in RulesSource?

1. Member
Join Date
Oct 2004
Posts
69

## Integer Constraints in RulesSource?

I experiment with making RulesSource similar to PetClinicValidationRulesSource. In PetClinicValidationRulesSource, all Constraints are based on String properties in domain objects. I cannot find any example for other property types like Integer. Example, how can I do each of the following for Integer properties (assume each is an independent constraint...that is, don't combine them in your reply please)?

1) required and must be positive Integer
2) required and must be Integer between -180 and 180
3) required and must be Integer greater than 100
4) required and must be Integer in set (1, 3, 5, 7)
5) NOT required but if filled in it must be between 5 and 10

Please forgiving me if I miss something in Petclinic or in docs!

2. Code:
```        Rules integerRules = new Rules&#40;TestBean.class&#41; &#123;
protected void initRules&#40;&#41; &#123;
add&#40;"number1", all&#40;new Constraint&#91;&#93; &#123; required&#40;&#41;, gt&#40;0&#41; &#125;&#41;;
add&#40;"number3", all&#40;new Constraint&#91;&#93; &#123; required&#40;&#41;, gt&#40;100&#41; &#125;&#41;;
add&#40;"number4", all&#40;new Constraint&#91;&#93; &#123; required&#40;&#41;, inGroup&#40;new Object&#91;&#93; &#123; 1, 3, 5 7 &#125;&#41;;
&#125;

&#125;;```
Pretty slick huh?

3. Member
Join Date
Oct 2004
Posts
69
Originally Posted by kdonald
Pretty slick huh?
Well, yes, except some compiler error. :cry:

I fix first four like this:
Code:
```add&#40;"number1", all&#40;new Constraint&#91;&#93; &#123; required&#40;&#41;, gt&#40;"number1", new Integer&#40;0&#41;&#41; &#125;&#41;&#41;;

add&#40;"number2", inRange&#40;"number2", new Integer&#40;-180&#41;, new Integer&#40;180&#41;&#41;&#41;;

add&#40;"number3", all&#40;new Constraint&#91;&#93; &#123; required&#40;&#41;, gt&#40;"number3", new Integer&#40;100&#41;&#41; &#125;&#41;&#41;;

add&#40;"number4", all&#40;new Constraint&#91;&#93; &#123; required&#40;&#41;, inGroup&#40;new Object&#91;&#93; &#123;new Integer&#40;1&#41;, new Integer&#40;3&#41;, new Integer&#40;5&#41;, new Integer&#40;7&#41;&#125;&#41;&#125;&#41;&#41;;```
Seems first four correct now with my changes? Main thing is need to make int to Integer.

But problem is fifth example. First, "if" is Java keyword so can't use like this for method name. Second, present() and between() don't seem to exist in lastest code from CVS. Maybe I look in wrong place but I thinks these two method should be in org.springframework.rules.factory.Constraints class, no? Maybe you not check in yet? Or maybe not coming across in anonymous CVS yet because propagation delay?

4. Steve--um, I haven't yet added those convenience methods to the Constraints factory yet. I should've mentioned!

Actually, what you're doing is incorrect: you're added a property constraint with a value constraint that is also a property constraint! (I'll put a check in constrants that won't allow that to happen).

I'm going to add them now ;-)

5. Member
Join Date
Oct 2004
Posts
69
Actually, what you're doing is incorrect: you're added a property constraint with a value constraint that is also a property constraint! (I'll put a check in constrants that won't allow that to happen).

6. Member
Join Date
Oct 2004
Posts
69
Originally Posted by kdonald
Actually, what you're doing is incorrect: you're added a property constraint with a value constraint that is also a property constraint!
Maybe you talking about how I added property names to constraint factory methods? For example I changed gt(0) to gt("number1", new Integer(0)) })...I added "number1" parameter here. If I don't add, it does not compiles. I do note that the one parameter version of gt() is actually declared in PropertyConstraints. However, DefaultRulesSource extends Constraints, not PropertyConstraints. So maybe this is the problem? How to fix?

7. Senior Member
Join Date
Sep 2004
Location
Posts
135
Originally Posted by steve_smith
But problem is fifth example. First, "if" is Java keyword so can't use like this for method name.
Keith -- I concur with Stefano. AFAIK you cannot use "if" as a method name. Perhaps you could call it "iff"...but this might be confusing because "iff" is a common abbreviation for "if and only if" in mathematics. Maybe "if_"?

8. Guys, that was psudo code for convenience methods we should add.

Yes, if() is invalid.

I'm going with ifTrue()

gimme a sec and I'll check it all in!!

9. Here are the new methods

Code:
```	public Constraint eq&#40;Object value&#41; &#123;
return getConstraints&#40;&#41;.eq&#40;value&#41;;
&#125;

public Constraint eq&#40;int value&#41; &#123;
return getConstraints&#40;&#41;.eq&#40;value&#41;;
&#125;

public Constraint eq&#40;Object value, Comparator comparator&#41; &#123;
return getConstraints&#40;&#41;.eq&#40;value, comparator&#41;;
&#125;

public Constraint gt&#40;Comparable value&#41; &#123;
return getConstraints&#40;&#41;.gt&#40;value&#41;;
&#125;

public Constraint gt&#40;int value&#41; &#123;
return getConstraints&#40;&#41;.gt&#40;value&#41;;
&#125;

public Constraint gt&#40;Comparable value, Comparator comparator&#41; &#123;
return getConstraints&#40;&#41;.gt&#40;value&#41;;
&#125;

public Constraint gte&#40;Comparable value&#41; &#123;
return getConstraints&#40;&#41;.gte&#40;value&#41;;
&#125;

public Constraint gte&#40;int value&#41; &#123;
return getConstraints&#40;&#41;.gte&#40;value&#41;;
&#125;

public Constraint gte&#40;Comparable value, Comparator comparator&#41; &#123;
return getConstraints&#40;&#41;.gte&#40;value, comparator&#41;;
&#125;

public Constraint lt&#40;Comparable value&#41; &#123;
return getConstraints&#40;&#41;.lt&#40;value&#41;;
&#125;

public Constraint lt&#40;int value&#41; &#123;
return getConstraints&#40;&#41;.lt&#40;value&#41;;
&#125;

public Constraint lt&#40;Comparable value, Comparator comparator&#41; &#123;
return getConstraints&#40;&#41;.lt&#40;value, comparator&#41;;
&#125;

public Constraint lte&#40;Comparable value&#41; &#123;
return getConstraints&#40;&#41;.lte&#40;value&#41;;
&#125;

public Constraint lte&#40;int value&#41; &#123;
return getConstraints&#40;&#41;.lte&#40;value&#41;;
&#125;

public Constraint lte&#40;Comparable value, Comparator comparator&#41; &#123;
return getConstraints&#40;&#41;.lte&#40;value, comparator&#41;;
&#125;

public Constraint range&#40;Comparable min, Comparable max&#41; &#123;
return getConstraints&#40;&#41;.range&#40;min, max&#41;;
&#125;

public Constraint range&#40;Object min, Object max, Comparator comparator&#41; &#123;
return getConstraints&#40;&#41;.range&#40;min, max, comparator&#41;;
&#125;

public Constraint range&#40;int min, int max&#41; &#123;
return getConstraints&#40;&#41;.range&#40;min, max&#41;;
&#125;

public Constraint between&#40;Comparable min, Comparable max&#41; &#123;
return getConstraints&#40;&#41;.between&#40;min, max&#41;;
&#125;

public Constraint between&#40;Object min, Object max, Comparator comparator&#41; &#123;
return getConstraints&#40;&#41;.between&#40;min, max, comparator&#41;;
&#125;

public Constraint between&#40;int min, int max&#41; &#123;
return getConstraints&#40;&#41;.between&#40;min, max&#41;;
&#125;

public Constraint present&#40;&#41; &#123;
return getConstraints&#40;&#41;.present&#40;&#41;;
&#125;

public Constraint ifTrue&#40;Constraint constraint, Constraint mustAlsoBeTrue&#41; &#123;
return getConstraints&#40;&#41;.ifTrue&#40;constraint, mustAlsoBeTrue&#41;;
&#125;

public Constraint ifTrueElse&#40;Constraint constraint, Constraint mustAlsoBeTrue, Constraint elseMustAlsoBeTrue&#41; &#123;
return getConstraints&#40;&#41;.ifTrue&#40;constraint, mustAlsoBeTrue, elseMustAlsoBeTrue&#41;;
&#125;```
This is ConstraintsAccessor, which Rules, RulesSource and AbstractConstraint extend, for convenience constraints building. It'll be CVS'ed shortly.

10. Member
Join Date
Oct 2004
Posts
69
Okay this is great! Now you allowed to take 15 minute break for Starbucks coffee! Maybe I let you have pumpkin scone too. :lol:

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•