Add accessors for testing.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / JsonReturnObject.java
1 /*
2  * © 2013 xplosion interactive
3  */
4
5 package net.pterodactylus.sone.web.ajax;
6
7 import java.util.Map;
8
9 import com.fasterxml.jackson.annotation.JsonProperty;
10 import com.fasterxml.jackson.annotation.JsonUnwrapped;
11 import com.fasterxml.jackson.databind.JsonNode;
12 import com.fasterxml.jackson.databind.node.BooleanNode;
13 import com.fasterxml.jackson.databind.node.IntNode;
14 import com.fasterxml.jackson.databind.node.TextNode;
15 import com.google.common.annotations.VisibleForTesting;
16 import com.google.common.collect.Maps;
17
18 /**
19  * JSON return object for AJAX requests.
20  *
21  * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
22  */
23 public class JsonReturnObject {
24
25         /** Whether the request was successful. */
26         @JsonProperty
27         private final boolean success;
28
29         /** The returned values. */
30         @JsonUnwrapped
31         private final Map<String, JsonNode> content = Maps.newHashMap();
32
33         /**
34          * Creates a new JSON return object.
35          *
36          * @param success
37          *              {@code true} if the request was successful, {@code false} otherwise
38          */
39         public JsonReturnObject(boolean success) {
40                 this.success = success;
41         }
42
43         //
44         // ACCESSORS
45         //
46
47         /**
48          * Returns whether the request was successful.
49          *
50          * @return {@code true} if the request was successful, {@code false} otherwise
51          */
52         @VisibleForTesting
53         public boolean isSuccess() {
54                 return success;
55         }
56
57         /**
58          * Returns the value stored under the given key.
59          *
60          * @param key
61          *              The key of the value to retrieve
62          * @return The value of the key, or {@code null} if there is no value for the
63          *         given key
64          */
65         @VisibleForTesting
66         public JsonNode get(String key) {
67                 return content.get(key);
68         }
69
70         //
71         // ACTIONS
72         //
73
74         /**
75          * Stores the given value under the given key.
76          *
77          * @param key
78          *              The key under which to store the value
79          * @param value
80          *              The value to store
81          * @return This JSON return object
82          */
83         public JsonReturnObject put(String key, boolean value) {
84                 return put(key, BooleanNode.valueOf(value));
85         }
86
87         /**
88          * Stores the given value under the given key.
89          *
90          * @param key
91          *              The key under which to store the value
92          * @param value
93          *              The value to store
94          * @return This JSON return object
95          */
96         public JsonReturnObject put(String key, int value) {
97                 return put(key, new IntNode(value));
98         }
99
100         /**
101          * Stores the given value under the given key.
102          *
103          * @param key
104          *              The key under which to store the value
105          * @param value
106          *              The value to store
107          * @return This JSON return object
108          */
109         public JsonReturnObject put(String key, String value) {
110                 return put(key, new TextNode(value));
111         }
112
113         /**
114          * Stores the given value under the given key.
115          *
116          * @param key
117          *              The key under which to store the value
118          * @param value
119          *              The value to store
120          * @return This JSON return object
121          */
122         public JsonReturnObject put(String key, JsonNode value) {
123                 content.put(key, value);
124                 return this;
125         }
126
127 }