Skip to content

Commit c3a8d39

Browse files
committed
* parameter is never configurable
1 parent 56f7c97 commit c3a8d39

26 files changed

Lines changed: 201 additions & 76 deletions

File tree

‎api-tests/src/test/java/org/ocpsoft/rewrite/config/ConfigurationBuilderTest.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,12 @@ public void testWhereAPI() throws Exception
204204
.where("s").matches("oh").bindsTo(El.property("ee.flee"))
205205
.constrainedBy(null).convertedBy(null).transformedBy(null).validatedBy(null)
206206
.withId("id")
207-
.withPriority(0);
207+
.withPriority(0)
208+
209+
.addRule()
210+
.when(new True())
211+
.otherwise(operation)
212+
.where("p");
208213
}
209214

210215
@Test

‎api-tests/src/test/java/org/ocpsoft/rewrite/param/ParameterBuilderTest.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ public String convert(Rewrite event, EvaluationContext context, Object value)
4040

4141
Parameter<?> parameter = new DefaultParameter("foo").validatedBy(validator).convertedBy(converter);
4242

43-
Object value = parameter.convert(new MockRewrite(), new MockEvaluationContext(), new Object());
44-
boolean valid = parameter.validate(new MockRewrite(), new MockEvaluationContext(), value);
43+
Object value = parameter.getConverter().convert(new MockRewrite(), new MockEvaluationContext(), new Object());
44+
45+
@SuppressWarnings("unchecked")
46+
boolean valid = ((Validator<Object>) parameter.getValidator()).validate(new MockRewrite(),
47+
new MockEvaluationContext(), value);
4548

4649
Assert.assertTrue(validated);
4750
Assert.assertFalse(valid);

‎api-tests/src/test/java/org/ocpsoft/rewrite/param/ParameterizedPatternTest.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public void testComplexMatchingWithMutipleAdjacentParameters() throws Exception
6565
DefaultParameterStore store = new DefaultParameterStore();
6666
initialize(store, path);
6767

68-
store.get("prefix").constrainedBy(new RegexConstraint(".*"));
69-
store.get("seg").constrainedBy(new RegexConstraint("[^/]+"));
70-
store.get("suffix").constrainedBy(new RegexConstraint("\\?.*"));
68+
((ConfigurableParameter<?>) store.get("prefix")).constrainedBy(new RegexConstraint(".*"));
69+
((ConfigurableParameter<?>) store.get("seg")).constrainedBy(new RegexConstraint("[^/]+"));
70+
((ConfigurableParameter<?>) store.get("suffix")).constrainedBy(new RegexConstraint("\\?.*"));
7171

7272
initialize(store, path);
7373

@@ -213,7 +213,7 @@ public void testParametersUsedMultipleRequireSingleConfiguration()
213213
ParameterStore parameters = new DefaultParameterStore();
214214
initialize(parameters, path);
215215

216-
parameters.get("f").constrainedBy(new RegexConstraint("foo"));
216+
((ConfigurableParameter<?>) parameters.get("f")).constrainedBy(new RegexConstraint("foo"));
217217

218218
Assert.assertTrue(path.matches(rewrite, context, "/foo/foo/"));
219219
Assert.assertFalse(path.matches(rewrite, context, "/foo/bar/"));

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import org.ocpsoft.common.util.Iterators;
2929
import org.ocpsoft.logging.Logger;
3030
import org.ocpsoft.rewrite.bind.Evaluation;
31+
import org.ocpsoft.rewrite.param.ConfigurableParameter;
3132
import org.ocpsoft.rewrite.param.DefaultParameter;
33+
import org.ocpsoft.rewrite.param.Parameter;
3234
import org.ocpsoft.rewrite.param.ParameterStore;
3335
import org.ocpsoft.rewrite.param.Parameterized;
3436
import org.ocpsoft.rewrite.spi.ConfigurationCacheProvider;
@@ -185,7 +187,9 @@ public void call(Parameterized parameterized)
185187
ParameterStore store = ((RuleBuilder) rule).getParameterStore();
186188

187189
for (String name : names) {
188-
store.get(name, new DefaultParameter(name)).bindsTo(Evaluation.property(name));
190+
Parameter<?> parameter = store.get(name, new DefaultParameter(name));
191+
if (parameter instanceof ConfigurableParameter<?>)
192+
((ConfigurableParameter<?>) parameter).bindsTo(Evaluation.property(name));
189193
}
190194

191195
parameterized.setParameterStore(store);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
public interface ConfigurationRuleParameter extends ParameterConfiguration<ConfigurationRuleParameterBuilder>
2929
{
3030
/**
31-
* Configure the regular expression {@link Pattern} to which this parameter must match.
31+
* Configure the regular expression {@link Pattern} to which this {@link Parameter} must match.
3232
*/
3333
ConfigurationRuleParameterMatches matches(String string);
3434
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.ocpsoft.rewrite.bind.Converter;
2222
import org.ocpsoft.rewrite.bind.Evaluation;
2323
import org.ocpsoft.rewrite.bind.Validator;
24+
import org.ocpsoft.rewrite.param.ConfigurableParameter;
2425
import org.ocpsoft.rewrite.param.Constraint;
2526
import org.ocpsoft.rewrite.param.ParameterBuilder;
2627
import org.ocpsoft.rewrite.param.RegexConstraint;
@@ -41,10 +42,10 @@ public class ConfigurationRuleParameterBuilder extends ParameterBuilder<Configur
4142
{
4243

4344
private final ConfigurationRuleBuilder parent;
44-
private ParameterBuilder<?> param;
45+
private ConfigurableParameter<?> param;
4546

4647
public ConfigurationRuleParameterBuilder(ConfigurationRuleBuilder parent,
47-
final ParameterBuilder<?> param)
48+
final ConfigurableParameter<?> param)
4849
{
4950
super(param.getName());
5051
this.parent = parent;

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.ocpsoft.rewrite.bind.Binding;
1919
import org.ocpsoft.rewrite.bind.Converter;
2020
import org.ocpsoft.rewrite.bind.Validator;
21-
import org.ocpsoft.rewrite.param.ConfigurableParameter;
2221
import org.ocpsoft.rewrite.param.Constraint;
2322
import org.ocpsoft.rewrite.param.Parameter;
2423
import org.ocpsoft.rewrite.param.Transform;
@@ -37,22 +36,22 @@ public interface ConfigurationRuleParameterMatches extends ConfigurationBuilderR
3736
ConfigurationRuleParameterWhere where(String string);
3837

3938
/**
40-
* Add a {@link Binding} to this {@link ConfigurableParameter}.
39+
* Add a {@link Binding} to this {@link Parameter}.
4140
*/
4241
ConfigurationRuleParameterMatches bindsTo(Binding binding);
4342

4443
/**
45-
* Set the {@link Converter} with which this {@link ConfigurableParameter} value will be converted.
44+
* Set the {@link Converter} with which this {@link Parameter} value will be converted.
4645
*/
4746
public ConfigurationRuleParameterMatches convertedBy(final Converter<?> converter);
4847

4948
/**
50-
* Set the {@link Validator} with which this {@link ConfigurableParameter} value will be validated.
49+
* Set the {@link Validator} with which this {@link Parameter} value will be validated.
5150
*/
5251
public ConfigurationRuleParameterMatches validatedBy(final Validator<?> validator);
5352

5453
/**
55-
* Add a constraint to which this object {@link IMPLTYPE} must match.
54+
* Add a {@link Constraint} to which this object {@link IMPLTYPE} must match.
5655
*/
5756
public ConfigurationRuleParameterMatches constrainedBy(Constraint<String> pattern);
5857

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@
1515
*/
1616
package org.ocpsoft.rewrite.config;
1717

18+
import java.util.regex.Pattern;
19+
20+
import org.ocpsoft.rewrite.param.Parameter;
21+
import org.ocpsoft.rewrite.param.ParameterConfiguration;
1822

1923
/**
2024
* A {@link ConfigurationRuleParameter} with a where clause.
2125
*
2226
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2327
*/
24-
public interface ConfigurationRuleParameterWhere extends ConfigurationRuleParameter
28+
public interface ConfigurationRuleParameterWhere extends ParameterConfiguration<ConfigurationRuleParameterBuilder>
2529
{
30+
/**
31+
* Configure the regular expression {@link Pattern} to which this {@link Parameter} must match.
32+
*/
33+
ConfigurationRuleParameterMatches matches(String string);
2634

2735
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020

2121
import org.ocpsoft.rewrite.context.EvaluationContext;
2222
import org.ocpsoft.rewrite.event.Rewrite;
23+
import org.ocpsoft.rewrite.exception.RewriteException;
24+
import org.ocpsoft.rewrite.param.ConfigurableParameter;
2325
import org.ocpsoft.rewrite.param.DefaultParameterStore;
26+
import org.ocpsoft.rewrite.param.Parameter;
2427
import org.ocpsoft.rewrite.param.ParameterBuilder;
2528
import org.ocpsoft.rewrite.param.ParameterStore;
2629
import org.ocpsoft.rewrite.util.Visitor;
@@ -201,11 +204,12 @@ public ParameterStore getParameterStore()
201204
}
202205

203206
@SuppressWarnings({ "rawtypes" })
204-
public ParameterBuilder<?> where(String name)
207+
public ConfigurableParameter<?> where(String name)
205208
{
206209
ParameterBuilder<?> parameter = new ParameterBuilder(name) {};
207-
// FIXME: This cast isn't very nice.
208-
return (ParameterBuilder<?>) getParameterStore().get(name, parameter);
210+
Parameter<?> result = getParameterStore().get(name, parameter);
211+
if (result instanceof ConfigurableParameter)
212+
return (ConfigurableParameter<?>) result;
213+
throw new RewriteException("Cannot configure read-only parameter [" + name + "].");
209214
}
210-
211215
}

‎api/src/main/java/org/ocpsoft/rewrite/param/DefaultParameterStore.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@
2727
*/
2828
public class DefaultParameterStore implements ParameterStore
2929
{
30-
private final Map<String, ConfigurableParameter<?>> parameters = new LinkedHashMap<String, ConfigurableParameter<?>>();
30+
private final Map<String, Parameter<?>> parameters = new LinkedHashMap<String, Parameter<?>>();
3131

3232
public DefaultParameterStore()
3333
{
34-
get("*", new DefaultParameter("*")).constrainedBy(new RegexConstraint(".*"));
34+
get("*", new ImmutableParameter(new DefaultParameter("*").constrainedBy(new RegexConstraint(".*"))));
3535
}
3636

37-
public ConfigurableParameter<?> get(final String name, ConfigurableParameter<?> deflt)
37+
public Parameter<?> get(final String name, Parameter<?> deflt)
3838
{
39-
ConfigurableParameter<?> parameter = null;
39+
Parameter<?> parameter = null;
4040
if (parameters.get(name) != null)
4141
{
4242
parameter = parameters.get(name);
@@ -53,7 +53,7 @@ public ConfigurableParameter<?> get(final String name, ConfigurableParameter<?>
5353
return parameter;
5454
}
5555

56-
public ConfigurableParameter<?> get(String name)
56+
public Parameter<?> get(String name)
5757
{
5858
if (!parameters.containsKey(name))
5959
throw new IllegalArgumentException("No such parameter [" + name + "] exists.");
@@ -65,7 +65,7 @@ public boolean isEmpty()
6565
return parameters.isEmpty();
6666
}
6767

68-
public ConfigurableParameter<?> store(ConfigurableParameter<?> value)
68+
public Parameter<?> store(Parameter<?> value)
6969
{
7070
Assert.notNull(value, "Parameter to store must not be null.");
7171
return parameters.put(value.getName(), value);
@@ -77,7 +77,7 @@ public int size()
7777
}
7878

7979
@Override
80-
public Iterator<Entry<String, ConfigurableParameter<?>>> iterator()
80+
public Iterator<Entry<String, Parameter<?>>> iterator()
8181
{
8282
return parameters.entrySet().iterator();
8383
}

0 commit comments

Comments
 (0)