Merge branch 'release-0.9.6'
[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.JsonAnyGetter;
10 import com.fasterxml.jackson.annotation.JsonProperty;
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         private final Map<String, JsonNode> content = Maps.newHashMap();
31
32         /**
33          * Creates a new JSON return object.
34          *
35          * @param success
36          *              {@code true} if the request was successful, {@code false} otherwise
37          */
38         public JsonReturnObject(boolean success) {
39                 this.success = success;
40         }
41
42         //
43         // ACCESSORS
44         //
45
46         /**
47          * Returns whether the request was successful.
48          *
49          * @return {@code true} if the request was successful, {@code false} otherwise
50          */
51         @VisibleForTesting
52         public boolean isSuccess() {
53                 return success;
54         }
55
56         /**
57          * Returns the value stored under the given key.
58          *
59          * @param key
60          *              The key of the value to retrieve
61          * @return The value of the key, or {@code null} if there is no value for the
62          *         given key
63          */
64         @VisibleForTesting
65         public JsonNode get(String key) {
66                 return content.get(key);
67         }
68
69         /**
70          * Returns the content of this object for serialization.
71          *
72          * @return The content of this object
73          */
74         @JsonAnyGetter
75         public Map<String, JsonNode> getContent() {
76                 return content;
77         }
78
79         //
80         // ACTIONS
81         //
82
83         /**
84          * Stores the given value under the given key.
85          *
86          * @param key
87          *              The key under which to store the value
88          * @param value
89          *              The value to store
90          * @return This JSON return object
91          */
92         public JsonReturnObject put(String key, boolean value) {
93                 return put(key, BooleanNode.valueOf(value));
94         }
95
96         /**
97          * Stores the given value under the given key.
98          *
99          * @param key
100          *              The key under which to store the value
101          * @param value
102          *              The value to store
103          * @return This JSON return object
104          */
105         public JsonReturnObject put(String key, int value) {
106                 return put(key, new IntNode(value));
107         }
108
109         /**
110          * Stores the given value under the given key.
111          *
112          * @param key
113          *              The key under which to store the value
114          * @param value
115          *              The value to store
116          * @return This JSON return object
117          */
118         public JsonReturnObject put(String key, String value) {
119                 return put(key, new TextNode(value));
120         }
121
122         /**
123          * Stores the given value under the given key.
124          *
125          * @param key
126          *              The key under which to store the value
127          * @param value
128          *              The value to store
129          * @return This JSON return object
130          */
131         public JsonReturnObject put(String key, JsonNode value) {
132                 content.put(key, value);
133                 return this;
134         }
135
136 }