Skip to content

Commit e422726

Browse files
Rene SprietsmaRene Sprietsma
authored andcommitted
refactor thingy
1 parent 2055cb0 commit e422726

File tree

23 files changed

+301
-113
lines changed

23 files changed

+301
-113
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Inspired by ['Build You Own Web Framework In Go'](https://www.nicolasmerouze.com
88

99
[![Build Status](https://travis-ci.org/spriet2000/vertx-handlers-http.svg?branch=master)](https://travis-ci.org/spriet2000/vertx-handlers-http)
1010

11-
## Example
11+
## Setup server request handlers
1212

1313
```java
1414

@@ -30,3 +30,24 @@ Inspired by ['Build You Own Web Framework In Go'](https://www.nicolasmerouze.com
3030

3131
```
3232

33+
## Implement server request handlers
34+
35+
```java
36+
37+
public class ExampleHandler implements BiFunction<BiConsumer<StringBuilder, Throwable>,
38+
BiConsumer<StringBuilder, Void>, BiConsumer<StringBuilder, Void>> {
39+
40+
@Override
41+
public BiConsumer<StringBuilder, Void> apply(BiConsumer<StringBuilder, Throwable> fail,
42+
BiConsumer<StringBuilder, Void> next) {
43+
return next;
44+
}
45+
}
46+
47+
```
48+
49+
50+
### Remarks
51+
52+
* When var, or something like, enters the JAVA language, defining handlers is quite less verbose. http://openjdk.java.net/jeps/286
53+
* Implementing handler still is quite verbose.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.spriet2000.vertx.handlers.core;
2+
3+
import com.github.spriet2000.vertx.handlers.core.impl.BiHandlersImpl;
4+
5+
import java.util.List;
6+
import java.util.function.BiConsumer;
7+
import java.util.function.BiFunction;
8+
9+
10+
public interface BiHandlers<E, A> {
11+
12+
BiConsumer<E, A> apply(BiConsumer<E, Throwable> exceptionHandler, BiConsumer<E, A> successHandler);
13+
14+
BiHandlersImpl<E, A> andThen(BiConsumer<E, A>... consumers);
15+
16+
BiHandlersImpl<E, A> andThen(BiFunction<BiConsumer<E, Throwable>, BiConsumer<E, A>, BiConsumer<E, A>>... handlers);
17+
18+
BiHandlersImpl<E, A> andThen(BiHandlersImpl<E, A>... handlers);
19+
20+
List<BiFunction<BiConsumer<E, Throwable>, BiConsumer<E, A>, BiConsumer<E, A>>> handlers();
21+
}

src/main/java/com/github/spriet2000/vertx/handlers/core/http/ServerRequestHandlers.java

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.github.spriet2000.vertx.handlers.core.impl;
2+
3+
import com.github.spriet2000.vertx.handlers.core.BiHandlers;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.List;
8+
import java.util.function.BiConsumer;
9+
import java.util.function.BiFunction;
10+
import java.util.stream.Collectors;
11+
12+
public final class BiHandlersImpl<E, A> implements BiHandlers<E, A> {
13+
14+
private List<BiFunction<BiConsumer<E, Throwable>, BiConsumer<E, A>, BiConsumer<E, A>>> handlers;
15+
16+
public BiHandlersImpl() {
17+
}
18+
19+
@Override
20+
public BiConsumer<E, A> apply(BiConsumer<E, Throwable> exceptionHandler, BiConsumer<E, A> successHandler) {
21+
BiConsumer<E, A> last = successHandler;
22+
for (int i = handlers.size() - 1; i >= 0; i--) {
23+
final BiConsumer<E, A> previous = last;
24+
last = handlers.get(i).apply(
25+
exceptionHandler,
26+
previous);
27+
}
28+
return last;
29+
}
30+
31+
@Override
32+
@SafeVarargs
33+
public final BiHandlersImpl<E, A> andThen(BiConsumer<E, A>... consumers) {
34+
for (BiConsumer<E, A> consumer : consumers) {
35+
handlers().add((f, n) -> consumer);
36+
}
37+
return this;
38+
}
39+
40+
@Override
41+
@SafeVarargs
42+
public final BiHandlersImpl<E, A> andThen(BiFunction<BiConsumer<E, Throwable>, BiConsumer<E, A>, BiConsumer<E, A>>... handlers) {
43+
Collections.addAll(handlers(), handlers);
44+
return this;
45+
}
46+
47+
@Override
48+
@SafeVarargs
49+
public final BiHandlersImpl<E, A> andThen(BiHandlersImpl<E, A>... handlers) {
50+
for (BiHandlersImpl<E, A> handler : handlers) {
51+
handlers().addAll(handler.handlers().stream().collect(Collectors.toList()));
52+
}
53+
return this;
54+
}
55+
56+
@Override
57+
public List<BiFunction<BiConsumer<E, Throwable>, BiConsumer<E, A>, BiConsumer<E, A>>> handlers() {
58+
if (handlers == null) {
59+
handlers = new ArrayList<>();
60+
}
61+
return handlers;
62+
}
63+
}

src/main/java/com/github/spriet2000/vertx/handlers/extensions/bodyParser/Body.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/java/com/github/spriet2000/vertx/handlers/extensions/timeout/TimeoutException.java

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.github.spriet2000.vertx.handlers.http.handlers.bodyParser;
2+
3+
4+
public interface Body<T> {
5+
void body(T body);
6+
7+
T body();
8+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.spriet2000.vertx.handlers.extensions.bodyParser;
1+
package com.github.spriet2000.vertx.handlers.http.handlers.bodyParser;
22

33

44
public final class BodyParseException extends Exception {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package com.github.spriet2000.vertx.handlers.extensions.bodyParser.impl;
1+
package com.github.spriet2000.vertx.handlers.http.handlers.bodyParser.impl;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4-
import com.github.spriet2000.vertx.handlers.extensions.bodyParser.Body;
5-
import com.github.spriet2000.vertx.handlers.extensions.bodyParser.BodyParseException;
4+
import com.github.spriet2000.vertx.handlers.http.handlers.bodyParser.Body;
5+
import com.github.spriet2000.vertx.handlers.http.handlers.bodyParser.BodyParseException;
66
import io.netty.handler.codec.http.HttpHeaders;
77
import io.vertx.core.buffer.Buffer;
88
import io.vertx.core.http.HttpMethod;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package com.github.spriet2000.vertx.handlers.extensions.bodyParser.impl;
1+
package com.github.spriet2000.vertx.handlers.http.handlers.bodyParser.impl;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
5-
import com.github.spriet2000.vertx.handlers.extensions.bodyParser.Body;
6-
import com.github.spriet2000.vertx.handlers.extensions.bodyParser.BodyParseException;
5+
import com.github.spriet2000.vertx.handlers.http.handlers.bodyParser.Body;
6+
import com.github.spriet2000.vertx.handlers.http.handlers.bodyParser.BodyParseException;
77
import io.netty.handler.codec.http.HttpHeaders;
88
import io.vertx.core.buffer.Buffer;
99
import io.vertx.core.http.HttpMethod;

0 commit comments

Comments
 (0)