Skip to content

Commit

Permalink
adding generic typing to JsonView
Browse files Browse the repository at this point in the history
  • Loading branch information
monitorjbl committed May 30, 2015
1 parent fb31cf9 commit 698cff4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions src/main/java/com/monitorjbl/json/JsonView.java
Expand Up @@ -6,13 +6,13 @@
/**
* Allows runtime alteration of JSON responses
*/
public class JsonView {
public class JsonView<T> {
private static final ThreadLocal<JsonView> current = new ThreadLocal<>();

private final Object value;
private final T value;
private final Map<Class<?>, Match> matches = new HashMap<>();

private JsonView(Object value) {
private JsonView(T value) {
this.value = value;
current.set(this);
}
Expand All @@ -25,18 +25,23 @@ Match getMatch(Class<?> cls) {
return matches.get(cls);
}

public JsonView onClass(Class<?> cls, Match match) {
public JsonView<T> onClass(Class<?> cls, Match match) {
matches.put(cls, match);
return this;
}

/**
* Returns the object the {@code JsonView} was initiated with
*
* @return
*/
@SuppressWarnings("unchecked")
public <E> E build() {
return (E) value;
public T returnValue() {
return value;
}

public static JsonView with(Object value) {
return new JsonView(value);
public static <E> JsonView<E> with(E value) {
return new JsonView<>(value);
}

static JsonView get() {
Expand Down
Expand Up @@ -64,7 +64,7 @@ public TestObject beanWithReturn() {
.onClass(TestObject.class, Match.match()
.exclude("int1")
.include("ignoredDirect"))
.build();
.returnValue();
}

@RequestMapping(method = RequestMethod.GET, value = "/list")
Expand Down

0 comments on commit 698cff4

Please sign in to comment.