Skip to content

Commit 10bdebd

Browse files
chkallincolnthree
authored andcommitted
Basic support for configuration of condition parameters
1 parent 5afb4bf commit 10bdebd

2 files changed

Lines changed: 132 additions & 23 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.config;
17+
18+
import org.ocpsoft.common.util.Assert;
19+
import org.ocpsoft.rewrite.param.Parameter;
20+
import org.ocpsoft.rewrite.param.ParameterStore;
21+
import org.ocpsoft.rewrite.param.Parameterized;
22+
23+
/**
24+
* {@link Visitor} implementation for {@link Condition}s that executes a given callback for all conditions that provide
25+
* a specific parameter.
26+
*
27+
* @author Christian Kaltepoth
28+
*/
29+
class ConditionParameterEnricher implements Visitor<Condition>
30+
{
31+
32+
private final String param;
33+
34+
private final Enricher callback;
35+
36+
public ConditionParameterEnricher(String param, Enricher callback)
37+
{
38+
this.param = param;
39+
this.callback = callback;
40+
}
41+
42+
@Override
43+
@SuppressWarnings({ "rawtypes", "unchecked" })
44+
public void visit(Condition condition)
45+
{
46+
if (condition instanceof Parameterized) {
47+
ParameterStore store = ((Parameterized) condition).getParameterStore();
48+
49+
if (store.containsKey(param)) {
50+
51+
Parameter<?, String> parameter = store.where(param, null);
52+
Assert.notNull(parameter, "Didn't find parameter [" + param + "] in:" + condition);
53+
54+
callback.enrich(parameter);
55+
56+
}
57+
}
58+
59+
}
60+
61+
public interface Enricher
62+
{
63+
void enrich(Parameter<?, String> parameter);
64+
}
65+
66+
}

‎api/src/main/java/org/ocpsoft/rewrite/config/ConfigurationRuleParameterBuilder.java‎

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import org.ocpsoft.rewrite.bind.Converter;
99
import org.ocpsoft.rewrite.bind.Validatable;
1010
import org.ocpsoft.rewrite.bind.Validator;
11+
import org.ocpsoft.rewrite.config.ConditionParameterEnricher.Enricher;
1112
import org.ocpsoft.rewrite.param.Constrainable;
1213
import org.ocpsoft.rewrite.param.Constraint;
14+
import org.ocpsoft.rewrite.param.Parameter;
15+
import org.ocpsoft.rewrite.param.RegexConstraint;
1316
import org.ocpsoft.rewrite.param.Transform;
1417
import org.ocpsoft.rewrite.param.Transformable;
1518

@@ -63,59 +66,99 @@ public List<Rule> getRules()
6366
}
6467

6568
@Override
66-
public ConfigurationRuleParameterBuilder matches(String pattern)
69+
public ConfigurationRuleParameterBuilder matches(final String pattern)
6770
{
68-
// TODO Auto-generated method stub
69-
return this;
71+
return constrainedBy(new RegexConstraint(pattern));
7072
}
7173

7274
@Override
73-
public ConfigurationRuleParameterBuilder transformedBy(Transform<String> transform)
75+
public ConfigurationRuleParameterBuilder transformedBy(final Transform<String> transform)
7476
{
75-
// TODO Auto-generated method stub
76-
return null;
77+
Visitor<Condition> visitor = new ConditionParameterEnricher(parameter, new Enricher() {
78+
@Override
79+
public void enrich(Parameter<?, String> parameter)
80+
{
81+
parameter.transformedBy(transform);
82+
}
83+
});
84+
new ConditionVisit(parent.getRuleBuilder().getCondition()).accept(visitor);
85+
return this;
7786
}
7887

7988
@Override
80-
public ConfigurationRuleParameterBuilder constrainedBy(Constraint<String> pattern)
89+
public ConfigurationRuleParameterBuilder constrainedBy(final Constraint<String> constraint)
8190
{
82-
// TODO Auto-generated method stub
83-
return null;
91+
Visitor<Condition> visitor = new ConditionParameterEnricher(parameter, new Enricher() {
92+
@Override
93+
public void enrich(Parameter<?, String> parameter)
94+
{
95+
parameter.constrainedBy(constraint);
96+
}
97+
});
98+
new ConditionVisit(parent.getRuleBuilder().getCondition()).accept(visitor);
99+
return this;
84100
}
85101

86102
@Override
87-
public <X extends Validator<?>> ConfigurationRuleParameterBuilder validatedBy(Class<X> type)
103+
public <X extends Validator<?>> ConfigurationRuleParameterBuilder validatedBy(final Class<X> type)
88104
{
89-
// TODO Auto-generated method stub
90-
return null;
105+
Visitor<Condition> visitor = new ConditionParameterEnricher(parameter, new Enricher() {
106+
@Override
107+
public void enrich(Parameter<?, String> parameter)
108+
{
109+
parameter.validatedBy(type);
110+
}
111+
});
112+
new ConditionVisit(parent.getRuleBuilder().getCondition()).accept(visitor);
113+
return this;
91114
}
92115

93116
@Override
94-
public ConfigurationRuleParameterBuilder validatedBy(Validator<?> validator)
117+
public ConfigurationRuleParameterBuilder validatedBy(final Validator<?> validator)
95118
{
96-
// TODO Auto-generated method stub
97-
return null;
119+
Visitor<Condition> visitor = new ConditionParameterEnricher(parameter, new Enricher() {
120+
@Override
121+
public void enrich(Parameter<?, String> parameter)
122+
{
123+
parameter.validatedBy(validator);
124+
}
125+
});
126+
new ConditionVisit(parent.getRuleBuilder().getCondition()).accept(visitor);
127+
return this;
98128
}
99129

100130
@Override
101-
public <X extends Converter<?>> ConfigurationRuleParameterBuilder convertedBy(Class<X> type)
131+
public <X extends Converter<?>> ConfigurationRuleParameterBuilder convertedBy(final Class<X> type)
102132
{
103-
// TODO Auto-generated method stub
104-
return null;
133+
Visitor<Condition> visitor = new ConditionParameterEnricher(parameter, new Enricher() {
134+
@Override
135+
public void enrich(Parameter<?, String> parameter)
136+
{
137+
parameter.convertedBy(type);
138+
}
139+
});
140+
new ConditionVisit(parent.getRuleBuilder().getCondition()).accept(visitor);
141+
return this;
105142
}
106143

107144
@Override
108-
public ConfigurationRuleParameterBuilder convertedBy(Converter<?> converter)
145+
public ConfigurationRuleParameterBuilder convertedBy(final Converter<?> converter)
109146
{
110-
// TODO Auto-generated method stub
111-
return null;
147+
Visitor<Condition> visitor = new ConditionParameterEnricher(parameter, new Enricher() {
148+
@Override
149+
public void enrich(Parameter<?, String> parameter)
150+
{
151+
parameter.convertedBy(converter);
152+
}
153+
});
154+
new ConditionVisit(parent.getRuleBuilder().getCondition()).accept(visitor);
155+
return this;
112156
}
113157

114158
@Override
115159
public ConfigurationRuleParameterBuilder bindsTo(Binding binding)
116160
{
117-
// TODO Auto-generated method stub
118-
return null;
161+
return this;
119162
}
120163

121164
}

0 commit comments

Comments
 (0)