Skip to content

Commit aa1aaa6

Browse files
committed
Implemented basic binding comparison operations
1 parent aa4a76b commit aa1aaa6

4 files changed

Lines changed: 113 additions & 4 deletions

File tree

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.Map;
2222
import java.util.Map.Entry;
2323

24+
import com.ocpsoft.rewrite.config.Condition;
25+
import com.ocpsoft.rewrite.config.ConditionBuilder;
2426
import com.ocpsoft.rewrite.config.Operation;
2527
import com.ocpsoft.rewrite.context.EvaluationContext;
2628
import com.ocpsoft.rewrite.event.Rewrite;
@@ -44,7 +46,7 @@ public static void enqueueSubmission(final Rewrite event, final EvaluationContex
4446
{
4547
Map<Bindable, Object> map = new LinkedHashMap<Bindable, Object>();
4648
map.put(bindable, value);
47-
enqueueSubmissions(event, context, map);
49+
enqueuePreOperationSubmissions(event, context, map);
4850
}
4951

5052
/**
@@ -54,7 +56,7 @@ public static void enqueueSubmission(final Rewrite event, final EvaluationContex
5456
*
5557
* @return false if validation fails.
5658
*/
57-
public static boolean enqueueSubmissions(final Rewrite event, final EvaluationContext context,
59+
public static boolean enqueuePreOperationSubmissions(final Rewrite event, final EvaluationContext context,
5860
final Map<? extends Bindable, ? extends Object> map)
5961
{
6062
List<Operation> operations = new ArrayList<Operation>();
@@ -104,6 +106,54 @@ public static List<Object> performRetrieval(final Rewrite event, final Evaluatio
104106
return result;
105107
}
106108

109+
/**
110+
* Return a new {@link Condition} which compares the expected value against the actual retrieved {@link Retrieval}
111+
* value.
112+
*/
113+
public static ConditionBuilder equals(final Object expected, final Retrieval binding)
114+
{
115+
return new ConditionBuilder() {
116+
@Override
117+
public boolean evaluate(Rewrite event, EvaluationContext context)
118+
{
119+
Object actual = binding.retrieve(event, context);
120+
return compare(expected, actual);
121+
}
122+
};
123+
}
124+
125+
/**
126+
* Return a new {@link Condition} which compares the expected value against the actual retrieved {@link Submission}
127+
* value.
128+
*/
129+
public static ConditionBuilder equals(final Object expected, final Submission binding, final Object submission)
130+
{
131+
return new ConditionBuilder() {
132+
@Override
133+
public boolean evaluate(Rewrite event, EvaluationContext context)
134+
{
135+
Object actual = binding.submit(event, context, submission);
136+
return compare(expected, actual);
137+
}
138+
};
139+
}
140+
141+
/**
142+
* Return true if the two values are equal.
143+
*/
144+
private static boolean compare(final Object expected, Object actual)
145+
{
146+
if (expected == actual)
147+
{
148+
return true;
149+
}
150+
else if (expected != null && expected.equals(actual))
151+
{
152+
return true;
153+
}
154+
return false;
155+
}
156+
107157
/**
108158
* Used to store bindings until all conditions have been met.
109159
*

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public interface Submission
3737
boolean validates(Rewrite event, EvaluationContext context, Object value);
3838

3939
/**
40-
* Store a value into the designated storage location.
40+
* Store a value into the designated storage location. Return any value that is produces as a result of this
41+
* operation.
4142
*/
4243
Object submit(Rewrite event, EvaluationContext context, Object value);
4344
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 com.ocpsoft.rewrite.util;
17+
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.io.OutputStream;
21+
22+
/**
23+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
24+
*/
25+
public final class Streams
26+
{
27+
private static final int BUFFER_SIZE = 2048;
28+
29+
/**
30+
* Copy all contents from the {@link InputStream} to the {@link OutputStream}, using a buffer of size 2048.
31+
*/
32+
public static boolean copy(InputStream input, OutputStream output)
33+
{
34+
return copy(input, output, BUFFER_SIZE);
35+
}
36+
37+
/**
38+
* Copy all contents from the {@link InputStream} to the {@link OutputStream}, using a buffer of the given size.
39+
*/
40+
public static boolean copy(InputStream input, OutputStream output, int bufferSize)
41+
{
42+
try
43+
{
44+
byte[] buffer = new byte[bufferSize];
45+
46+
int read = input.read(buffer);
47+
while (read != -1) {
48+
output.write(buffer, 0, read);
49+
read = input.read(buffer);
50+
}
51+
52+
output.flush();
53+
}
54+
catch (IOException e) {
55+
return false;
56+
}
57+
return true;
58+
}
59+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ else if ((last == null) && (capture.getStart() > 0))
327327
Pattern segmentPattern = Pattern.compile(segmentBuilder.toString() + ".*");
328328
Matcher segmentMatcher = segmentPattern.matcher(temp);
329329

330-
// TODO this needs to handle URLDecoding
331330
if (segmentMatcher.matches())
332331
{
333332
String value = segmentMatcher.group(1);

0 commit comments

Comments
 (0)