Skip to content

Commit 9719960

Browse files
committed
Partially on the way to new Constraint/Transform system
1 parent 5f3ad2c commit 9719960

38 files changed

Lines changed: 406 additions & 127 deletions

‎api/src/main/java/com/ocpsoft/rewrite/bind/BindingBuilder.java‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.ocpsoft.rewrite.bind;
1717

18+
import java.lang.reflect.Array;
1819
import java.util.Collection;
1920

2021
import com.ocpsoft.common.services.ServiceLoader;
@@ -64,13 +65,44 @@ public BindingBuilder validatedBy(final Validator<?> validator)
6465
@SuppressWarnings({ "unchecked", "rawtypes" })
6566
public boolean validates(final Rewrite event, final EvaluationContext context, final Object value)
6667
{
67-
// We must assume the value was properly converted.
68+
if (value != null && value.getClass().isArray())
69+
{
70+
Object[] values = (Object[]) value;
71+
for (int i = 0; i < values.length; i++) {
72+
if (!((Validator) validator).validate(event, context, values[i]))
73+
{
74+
return false;
75+
}
76+
}
77+
return true;
78+
}
6879
return ((Validator) validator).validate(event, context, value);
6980
}
7081

7182
@Override
7283
public Object convert(final Rewrite event, final EvaluationContext context, final Object value)
7384
{
85+
if (value != null && value.getClass().isArray())
86+
{
87+
Object[] values = (Object[]) value;
88+
Object[] convertedValues = new Object[values.length];
89+
for (int i = 0; i < convertedValues.length; i++) {
90+
convertedValues[i] = converter.convert(event, context, values[i]);
91+
}
92+
93+
Class<?> type = Object.class;
94+
for (Object object : convertedValues) {
95+
if (object != null)
96+
{
97+
type = object.getClass();
98+
break;
99+
}
100+
}
101+
102+
Object[] result = (Object[]) Array.newInstance(type, convertedValues.length);
103+
System.arraycopy(convertedValues, 0, result, 0, result.length);
104+
return result;
105+
}
74106
return converter.convert(event, context, value);
75107
}
76108

‎api/src/main/java/com/ocpsoft/rewrite/bind/Bindings.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,5 +299,12 @@ public void perform(final Rewrite event, final EvaluationContext context)
299299
{
300300
binding.submit(event, context, value);
301301
}
302+
303+
@Override
304+
public String toString()
305+
{
306+
return "BindingOperation [binding=" + binding + ", value=" + value + "]";
307+
}
308+
302309
}
303310
}

‎api/src/main/java/com/ocpsoft/rewrite/bind/Evaluation.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,10 @@ public boolean supportsSubmission()
110110
return true;
111111
}
112112

113+
@Override
114+
public String toString()
115+
{
116+
return "Evaluation [property=" + property + "]";
117+
}
118+
113119
}

‎api/src/main/java/com/ocpsoft/rewrite/bind/RetrievalBuilder.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@
2323
*/
2424
public interface RetrievalBuilder extends Retrieval
2525
{
26+
/**
27+
* TODO implement? {@link SubmissionBuilder}
28+
*/
2629
}

‎api/src/main/java/com/ocpsoft/rewrite/context/ContextBase.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.HashMap;
1919
import java.util.Map;
2020

21-
2221
/**
2322
* Base {@link Context} abstract class.
2423
*
@@ -48,4 +47,10 @@ public boolean containsKey(final Object key)
4847
return map.containsKey(key);
4948
}
5049

50+
@Override
51+
public String toString()
52+
{
53+
return "ContextBase [map=" + map + "]";
54+
}
55+
5156
}

‎api/src/main/java/com/ocpsoft/rewrite/convert/IntegerConverter.java‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@ public Integer convert(final Rewrite event, final EvaluationContext context, fin
3232
{
3333
throw new IllegalArgumentException("Value to convert cannot be null.");
3434
}
35-
else if (value.getClass().isArray())
36-
{
37-
if (((Object[]) value).length > 0)
38-
{
39-
return Integer.valueOf(((Object[]) value)[0].toString());
40-
}
41-
else
42-
{
43-
throw new IllegalArgumentException("Value to convert was empty array.");
44-
}
45-
}
4635
return Integer.valueOf(value.toString());
4736
}
4837

‎api/src/main/java/com/ocpsoft/rewrite/event/InboundRewrite.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@
2323
*/
2424
public interface InboundRewrite extends Rewrite
2525
{
26+
/*
27+
* Marker interface.
28+
*/
2629
}

‎api/src/main/java/com/ocpsoft/rewrite/event/OutboundRewrite.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@
2323
*/
2424
public interface OutboundRewrite extends Rewrite
2525
{
26+
/*
27+
* Marker interface.
28+
*/
2629
}

‎api/src/main/java/com/ocpsoft/rewrite/param/ConditionParameterBuilder.java‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/**
2525
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2626
*/
27-
public class ConditionParameterBuilder<T> implements Condition, ParameterizedCondition<ConditionParameterBuilder<T>, T>
27+
public class ConditionParameterBuilder<T> implements ParameterizedCondition<ConditionParameterBuilder<T>, T>
2828
{
2929
private final ParameterizedCondition<ConditionParameterBuilder<T>, T> parent;
3030
private final Parameter<T> parameter;
@@ -37,11 +37,21 @@ public ConditionParameterBuilder(final ParameterizedCondition<ConditionParameter
3737
}
3838

3939
/**
40-
* The {@link Parameter} must match the given pattern.
40+
* The {@link Parameter} must meet the given {@link Constraint}.
4141
*/
42-
public ConditionParameterBuilder<T> matches(final T pattern)
42+
public ConditionParameterBuilder<T> constrainedBy(final Constraint<T> constraint)
4343
{
44-
parameter.matches(pattern);
44+
parameter.constrainedBy(constraint);
45+
return this;
46+
}
47+
48+
/**
49+
* Apply the given {@link Transform} to this {@link Parameter}; it will be applied in the order in which it was
50+
* added. All transforms are applied before {@link Binding} occurs.
51+
*/
52+
public ConditionParameterBuilder<T> transformedBy(final Transform<T> constraint)
53+
{
54+
parameter.transformedBy(constraint);
4555
return this;
4656
}
4757

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2011, Red Hat, Inc., and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* This is free software; you can redistribute it and/or modify it
8+
* under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation; either version 2.1 of
10+
* the License, or (at your option) any later version.
11+
*
12+
* This software is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this software; if not, write to the Free
19+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21+
*/
22+
package com.ocpsoft.rewrite.param;
23+
24+
/**
25+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
26+
*
27+
*/
28+
public interface Constraint<T>
29+
{
30+
/**
31+
* Return true if this {@link Constraint} is satisfied by the given value; otherwise, return false.
32+
*/
33+
public boolean isSatisfiedBy(T value);
34+
}

0 commit comments

Comments
 (0)