Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit caa7114

Browse files
authored
reorder predicate handler (#127)
1 parent b523ecd commit caa7114

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>com.kttdevelopment</groupId>
1212
<artifactId>simplehttpserver</artifactId>
13-
<version>4.3.1</version>
13+
<version>4.4.0</version>
1414

1515
<name>simplehttpserver</name>
1616
<description>📕 SimpleHttpServer :: Simplified implementation of the sun http server :: Simplified handlers to execute complex operations</description>

src/main/java/com/kttdevelopment/simplehttpserver/ContextUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* A utility class used to generate uniform contexts. Applications do not use this class.
77
*
88
* @since 03.05.03
9-
* @version 4.3.1
9+
* @version 4.4.0
1010
* @author Ktt Development
1111
*/
1212
public abstract class ContextUtil {

src/main/java/com/kttdevelopment/simplehttpserver/handler/PredicateHandler.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @see SimpleHttpExchange
1717
* @see Predicate
1818
* @since 01.00.00
19-
* @version 03.05.00
19+
* @version 4.4.0
2020
* @author Ktt Development
2121
*/
2222
public class PredicateHandler implements HttpHandler {
@@ -27,6 +27,7 @@ public class PredicateHandler implements HttpHandler {
2727
/**
2828
* Creates a predicate handler.
2929
*
30+
* @deprecated use {@link PredicateHandler#PredicateHandler(Predicate, HttpHandler, HttpHandler)}
3031
* @param trueHandler handler to use if true
3132
* @param falseHandler handler to use if false
3233
* @param predicate predicate to test
@@ -38,12 +39,33 @@ public class PredicateHandler implements HttpHandler {
3839
* @since 01.00.00
3940
* @author Ktt Development
4041
*/
42+
@Deprecated
4143
public PredicateHandler(final HttpHandler trueHandler, final HttpHandler falseHandler, final Predicate<HttpExchange> predicate){
4244
T = trueHandler;
4345
F = falseHandler;
4446
this.predicate = predicate;
4547
}
4648

49+
/**
50+
* Creates a predicate handler.
51+
*
52+
* @param predicate predicate to test
53+
* @param trueHandler handler to use if true
54+
* @param falseHandler handler to use if false
55+
*
56+
* @see SimpleHttpHandler
57+
* @see HttpHandler
58+
* @see SimpleHttpExchange
59+
* @see Predicate
60+
* @since 4.4.0
61+
* @author Ktt Development
62+
*/
63+
public PredicateHandler(final Predicate<HttpExchange> predicate, final HttpHandler trueHandler, final HttpHandler falseHandler){
64+
this.predicate = predicate;
65+
T = trueHandler;
66+
F = falseHandler;
67+
}
68+
4769
@Override
4870
public final void handle(final HttpExchange exchange) throws IOException{
4971
(predicate.test(exchange) ? T : F).handle(exchange);

src/main/java/com/kttdevelopment/simplehttpserver/handler/RootHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @see SimpleHttpHandler
1010
* @see HttpHandler
1111
* @since 01.00.00
12-
* @version 02.00.00
12+
* @version 4.4.0
1313
* @author Ktt Development
1414
*/
1515
public class RootHandler extends PredicateHandler {
@@ -27,9 +27,9 @@ public class RootHandler extends PredicateHandler {
2727
*/
2828
public RootHandler(final HttpHandler rootHandler, final HttpHandler elseHandler){
2929
super(
30+
httpExchange -> httpExchange.getRequestURI().getPath().equals("/"),
3031
rootHandler,
31-
elseHandler,
32-
httpExchange -> httpExchange.getRequestURI().getPath().equals("/")
32+
elseHandler
3333
);
3434
}
3535

src/test/java/com/kttdevelopment/simplehttpserver/handlers/PredicateHandlerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void predicate() throws IOException, ExecutionException, InterruptedExcep
2222
final boolean condition = System.currentTimeMillis() % 2 == 1;
2323

2424
final String context = "";
25-
server.createContext(context, new PredicateHandler((SimpleHttpHandler) exchange -> exchange.send("A"), (SimpleHttpHandler) exchange -> exchange.send("B"), exchange -> condition));
25+
server.createContext(context, new PredicateHandler(exchange -> condition, (SimpleHttpHandler) exchange -> exchange.send("A"), (SimpleHttpHandler) exchange -> exchange.send("B")));
2626
server.start();
2727

2828
Assertions.assertFalse(server.getContexts().isEmpty(), "Server did not contain a temporary context");

0 commit comments

Comments
 (0)