Skip to content

Commit 8fcb1e1

Browse files
committed
Constraints/Transforms are WORKING - Hell Yea\!
1 parent 9719960 commit 8fcb1e1

13 files changed

Lines changed: 153 additions & 78 deletions

File tree

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
/**
2525
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2626
*/
27-
public class ConditionParameterBuilder<T> implements ParameterizedCondition<ConditionParameterBuilder<T>, T>
27+
public class ConditionParameterBuilder<P extends ConditionParameterBuilder<P, T>, T> implements
28+
ParameterizedCondition<ConditionParameterBuilder<P, T>, T>
2829
{
29-
private final ParameterizedCondition<ConditionParameterBuilder<T>, T> parent;
30+
private final ParameterizedCondition<ConditionParameterBuilder<P, T>, T> parent;
3031
private final Parameter<T> parameter;
3132

32-
public ConditionParameterBuilder(final ParameterizedCondition<ConditionParameterBuilder<T>, T> parent,
33+
public ConditionParameterBuilder(final ParameterizedCondition<ConditionParameterBuilder<P, T>, T> parent,
3334
final Parameter<T> parameter)
3435
{
3536
this.parent = parent;
@@ -39,53 +40,60 @@ public ConditionParameterBuilder(final ParameterizedCondition<ConditionParameter
3940
/**
4041
* The {@link Parameter} must meet the given {@link Constraint}.
4142
*/
42-
public ConditionParameterBuilder<T> constrainedBy(final Constraint<T> constraint)
43+
@SuppressWarnings("unchecked")
44+
public P constrainedBy(final Constraint<T> constraint)
4345
{
4446
parameter.constrainedBy(constraint);
45-
return this;
47+
return (P) this;
4648
}
4749

4850
/**
4951
* Apply the given {@link Transform} to this {@link Parameter}; it will be applied in the order in which it was
5052
* added. All transforms are applied before {@link Binding} occurs.
5153
*/
52-
public ConditionParameterBuilder<T> transformedBy(final Transform<T> constraint)
54+
@SuppressWarnings("unchecked")
55+
public P transformedBy(final Transform<T> constraint)
5356
{
5457
parameter.transformedBy(constraint);
55-
return this;
58+
return (P) this;
5659
}
5760

5861
/**
5962
* The {@link Parameter} binds to the given {@link Binding}.
6063
*/
61-
public ConditionParameterBuilder<T> bindsTo(final Binding binding)
64+
@SuppressWarnings("unchecked")
65+
public P bindsTo(final Binding binding)
6266
{
6367
parameter.bindsTo(binding);
64-
return this;
68+
return (P) this;
6569
}
6670

6771
@Override
68-
public ConditionParameterBuilder<T> where(final String param)
72+
@SuppressWarnings("unchecked")
73+
public P where(final String param)
6974
{
70-
return parent.where(param);
75+
return (P) parent.where(param);
7176
}
7277

7378
@Override
74-
public ConditionParameterBuilder<T> where(final String param, final T pattern)
79+
@SuppressWarnings("unchecked")
80+
public P where(final String param, final T pattern)
7581
{
76-
return parent.where(param, pattern);
82+
return (P) parent.where(param, pattern);
7783
}
7884

7985
@Override
80-
public ConditionParameterBuilder<T> where(final String param, final T pattern, final Binding binding)
86+
@SuppressWarnings("unchecked")
87+
public P where(final String param, final T pattern, final Binding binding)
8188
{
82-
return parent.where(param, pattern, binding);
89+
return (P) parent.where(param, pattern, binding);
8390
}
8491

8592
@Override
86-
public ConditionParameterBuilder<T> where(final String param, final Binding binding)
93+
@SuppressWarnings("unchecked")
94+
public P where(final String param, final Binding binding)
8795
{
88-
return parent.where(param, binding);
96+
return (P) parent.where(param, binding);
8997
}
9098

9199
@Override

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
/**
2525
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2626
*/
27-
public class OperationParameterBuilder<T> implements ParameterizedOperation<OperationParameterBuilder<T>, T>
27+
public class OperationParameterBuilder<P extends OperationParameterBuilder<P, T>, T> implements
28+
ParameterizedOperation<OperationParameterBuilder<P, T>, T>
2829
{
29-
private final ParameterizedOperation<OperationParameterBuilder<T>, T> parent;
30+
private final ParameterizedOperation<OperationParameterBuilder<P, T>, T> parent;
3031
private final Parameter<T> parameter;
3132

32-
public OperationParameterBuilder(final ParameterizedOperation<OperationParameterBuilder<T>, T> parent,
33+
public OperationParameterBuilder(final ParameterizedOperation<OperationParameterBuilder<P, T>, T> parent,
3334
final Parameter<T> parameter)
3435
{
3536
this.parent = parent;
@@ -39,53 +40,60 @@ public OperationParameterBuilder(final ParameterizedOperation<OperationParameter
3940
/**
4041
* The {@link Parameter} must meet the given {@link Constraint}.
4142
*/
42-
public OperationParameterBuilder<T> constrainedBy(final Constraint<T> constraint)
43+
@SuppressWarnings("unchecked")
44+
public P constrainedBy(final Constraint<T> constraint)
4345
{
4446
parameter.constrainedBy(constraint);
45-
return this;
47+
return (P) this;
4648
}
4749

4850
/**
4951
* Apply the given {@link Transform} to this {@link Parameter}; it will be applied in the order in which it was
5052
* added. All transforms are applied before {@link Binding} occurs.
5153
*/
52-
public OperationParameterBuilder<T> transformedBy(final Transform<T> constraint)
54+
@SuppressWarnings("unchecked")
55+
public P transformedBy(final Transform<T> constraint)
5356
{
5457
parameter.transformedBy(constraint);
55-
return this;
58+
return (P) this;
5659
}
5760

5861
/**
5962
* The {@link Parameter} binds to the given {@link Binding}.
6063
*/
61-
public OperationParameterBuilder<T> bindsTo(final Binding binding)
64+
@SuppressWarnings("unchecked")
65+
public P bindsTo(final Binding binding)
6266
{
6367
parameter.bindsTo(binding);
64-
return this;
68+
return (P) this;
6569
}
6670

6771
@Override
68-
public OperationParameterBuilder<T> where(final String param)
72+
@SuppressWarnings("unchecked")
73+
public P where(final String param)
6974
{
70-
return parent.where(param);
75+
return (P) parent.where(param);
7176
}
7277

7378
@Override
74-
public OperationParameterBuilder<T> where(final String param, final T pattern)
79+
@SuppressWarnings("unchecked")
80+
public P where(final String param, final T pattern)
7581
{
76-
return parent.where(param, pattern);
82+
return (P) parent.where(param, pattern);
7783
}
7884

7985
@Override
80-
public OperationParameterBuilder<T> where(final String param, final T pattern, final Binding binding)
86+
@SuppressWarnings("unchecked")
87+
public P where(final String param, final T pattern, final Binding binding)
8188
{
82-
return parent.where(param, pattern, binding);
89+
return (P) parent.where(param, pattern, binding);
8390
}
8491

8592
@Override
86-
public OperationParameterBuilder<T> where(final String param, final Binding binding)
93+
@SuppressWarnings("unchecked")
94+
public P where(final String param, final Binding binding)
8795
{
88-
return parent.where(param, binding);
96+
return (P) parent.where(param, binding);
8997
}
9098

9199
@Override

‎impl-config/src/main/java/com/ocpsoft/rewrite/bind/RegexConditionParameterBuilder.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2929
*
3030
*/
31-
public class RegexConditionParameterBuilder extends ConditionParameterBuilder<String>
31+
public class RegexConditionParameterBuilder extends ConditionParameterBuilder<RegexConditionParameterBuilder, String>
3232
{
3333
private final RegexParameter parameter;
3434

35-
public RegexConditionParameterBuilder(ParameterizedCondition<ConditionParameterBuilder<String>, String> parent,
35+
public RegexConditionParameterBuilder(
36+
ParameterizedCondition<ConditionParameterBuilder<RegexConditionParameterBuilder, String>, String> parent,
3637
RegexParameter parameter)
3738
{
3839
super(parent, parameter);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.bind;
23+
24+
import com.ocpsoft.rewrite.param.OperationParameterBuilder;
25+
import com.ocpsoft.rewrite.param.ParameterizedOperation;
26+
27+
/**
28+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
29+
*
30+
*/
31+
public class RegexOperationParameterBuilder extends OperationParameterBuilder<RegexOperationParameterBuilder, String>
32+
{
33+
private final RegexParameter parameter;
34+
35+
public RegexOperationParameterBuilder(
36+
ParameterizedOperation<OperationParameterBuilder<RegexOperationParameterBuilder, String>, String> parent,
37+
RegexParameter parameter)
38+
{
39+
super(parent, parameter);
40+
this.parameter = parameter;
41+
}
42+
43+
public RegexOperationParameterBuilder matches(String pattern)
44+
{
45+
parameter.matches(pattern);
46+
return this;
47+
}
48+
49+
}

‎impl-config/src/test/java/com/ocpsoft/rewrite/parameters/ParameterizedPatternTest.java‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import org.junit.Test;
2424

2525
import com.ocpsoft.rewrite.bind.ParameterizedPattern;
26+
import com.ocpsoft.rewrite.bind.RegexParameter;
2627
import com.ocpsoft.rewrite.bind.parse.CaptureType;
2728
import com.ocpsoft.rewrite.bind.util.Maps;
28-
import com.ocpsoft.rewrite.param.Parameter;
2929

3030
/**
3131
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
@@ -40,7 +40,7 @@ public void testMatchesEmptyPath()
4040

4141
Assert.assertEquals(0, path.getParameters().size());
4242
Assert.assertTrue(path.matches(""));
43-
Map<Parameter<String>, String[]> results = path.parse("");
43+
Map<RegexParameter, String[]> results = path.parse("");
4444
Assert.assertNotNull(results);
4545
}
4646

@@ -52,7 +52,7 @@ public void testMatchesBarePath()
5252
Assert.assertEquals(0, path.getParameters().size());
5353
Assert.assertTrue(path.matches("/"));
5454

55-
Map<Parameter<String>, String[]> results = path.parse("/");
55+
Map<RegexParameter, String[]> results = path.parse("/");
5656
Assert.assertNotNull(results);
5757
}
5858

@@ -61,12 +61,12 @@ public void testMatchesWithParameters()
6161
{
6262
ParameterizedPattern path = new ParameterizedPattern("[^/]+", "/{customer}/orders/{id}");
6363

64-
Map<String, Parameter<String>> parameters = path.getParameters();
64+
Map<String, RegexParameter> parameters = path.getParameters();
6565
Assert.assertEquals(2, parameters.size());
6666
Assert.assertEquals("customer", parameters.get("customer").getName());
6767
Assert.assertEquals("id", parameters.get("id").getName());
6868

69-
Map<Parameter<String>, String[]> results = path.parse("/lincoln/orders/24");
69+
Map<RegexParameter, String[]> results = path.parse("/lincoln/orders/24");
7070
Assert.assertEquals("lincoln", results.get(path.getParameter("customer"))[0]);
7171
Assert.assertEquals("24", results.get(path.getParameter("id"))[0]);
7272
}
@@ -84,11 +84,11 @@ public void testParsesWithParametersRespectsTrailingCharsWithWildcardParameter()
8484
{
8585
ParameterizedPattern path = new ParameterizedPattern(".*", "/{customer}/");
8686

87-
Map<String, Parameter<String>> parameters = path.getParameters();
87+
Map<String, RegexParameter> parameters = path.getParameters();
8888
Assert.assertEquals(1, parameters.size());
8989
Assert.assertEquals("customer", parameters.get("customer").getName());
9090

91-
Map<Parameter<String>, String[]> results = path.parse("/lincoln/");
91+
Map<RegexParameter, String[]> results = path.parse("/lincoln/");
9292
Assert.assertEquals("lincoln", results.get(path.getParameter("customer"))[0]);
9393
}
9494

@@ -97,12 +97,12 @@ public void testMatchesWithParametersAndTrailingSlash()
9797
{
9898
ParameterizedPattern path = new ParameterizedPattern("[^/]+", "/{customer}/orders/{id}/");
9999

100-
Map<String, Parameter<String>> parameters = path.getParameters();
100+
Map<String, RegexParameter> parameters = path.getParameters();
101101
Assert.assertEquals(2, parameters.size());
102102
Assert.assertEquals("customer", parameters.get("customer").getName());
103103
Assert.assertEquals("id", parameters.get("id").getName());
104104

105-
Map<Parameter<String>, String[]> results = path.parse("/lincoln/orders/24/");
105+
Map<RegexParameter, String[]> results = path.parse("/lincoln/orders/24/");
106106
Assert.assertEquals("lincoln", results.get(path.getParameter("customer"))[0]);
107107
Assert.assertEquals("24", results.get(path.getParameter("id"))[0]);
108108
}

‎impl-servlet/src/main/java/com/ocpsoft/rewrite/servlet/config/Domain.java‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import com.ocpsoft.rewrite.bind.Bindings;
2525
import com.ocpsoft.rewrite.bind.Evaluation;
2626
import com.ocpsoft.rewrite.bind.ParameterizedPattern;
27+
import com.ocpsoft.rewrite.bind.RegexConditionParameterBuilder;
28+
import com.ocpsoft.rewrite.bind.RegexParameter;
2729
import com.ocpsoft.rewrite.config.Condition;
2830
import com.ocpsoft.rewrite.context.EvaluationContext;
2931
import com.ocpsoft.rewrite.param.ConditionParameterBuilder;
@@ -38,7 +40,8 @@
3840
*
3941
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
4042
*/
41-
public class Domain extends HttpCondition implements ParameterizedCondition<ConditionParameterBuilder<String>, String>
43+
public class Domain extends HttpCondition implements
44+
ParameterizedCondition<ConditionParameterBuilder<RegexConditionParameterBuilder, String>, String>
4245
{
4346
private final ParameterizedPattern expression;
4447

@@ -72,26 +75,26 @@ public static Domain matches(final String pattern)
7275
}
7376

7477
@Override
75-
public ConditionParameterBuilder<String> where(final String param)
78+
public RegexConditionParameterBuilder where(final String param)
7679
{
77-
return new ConditionParameterBuilder<String>(this, expression.getParameter(param));
80+
return new RegexConditionParameterBuilder(this, expression.getParameter(param));
7881
}
7982

8083
@Override
81-
public ConditionParameterBuilder<String> where(final String param, final String pattern)
84+
public RegexConditionParameterBuilder where(final String param, final String pattern)
8285
{
8386
return where(param).matches(pattern);
8487
}
8588

8689
@Override
87-
public ConditionParameterBuilder<String> where(final String param, final String pattern,
90+
public RegexConditionParameterBuilder where(final String param, final String pattern,
8891
final Binding binding)
8992
{
9093
return where(param, pattern).bindsTo(binding);
9194
}
9295

9396
@Override
94-
public ConditionParameterBuilder<String> where(final String param, final Binding binding)
97+
public RegexConditionParameterBuilder where(final String param, final Binding binding)
9598
{
9699
return where(param).bindsTo(binding);
97100
}
@@ -114,7 +117,7 @@ public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationCont
114117

115118
if (expression.matches(hostName))
116119
{
117-
Map<Parameter<String>, String[]> parameters = expression.parse(hostName);
120+
Map<RegexParameter, String[]> parameters = expression.parse(hostName);
118121
if (Bindings.enqueuePreOperationSubmissions(event, context, parameters))
119122
return true;
120123
}

0 commit comments

Comments
 (0)