-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProductsAPIRequest.java
More file actions
154 lines (123 loc) · 4.22 KB
/
ProductsAPIRequest.java
File metadata and controls
154 lines (123 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package api.model;
import java.util.Arrays;
import java.util.Map.Entry;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class ProductsAPIRequest extends APIRequest {
private static ProductsAPIRequest singleton = null;
// Array of fields that Product must have, or partially have if Product-like.
private static final String[] ProductFields = {
"id", "name", "description",
"category", "vendor",
"quantity", "cost", "msrp"
};
private ProductsAPIRequest() {
super();
}
/// Validation
/**
* Test if the given JSON is a Product or Product-like.
* If it is a Product, it should contain all of the fields within
* a Product object. If it is Product-like, it will contains
* some of the fields, but not all. It will however not contain
* any fields that aren't recognized fields within a Product object.
*
* @param value
* @param productLike
* @return
*/
public boolean isProduct(JsonElement value, boolean productLike) {
return productLike ? isProductLike(value) : isProduct(value);
}
public boolean isProduct(JsonElement value) {
if (!value.isJsonObject())
return false;
JsonObject json = value.getAsJsonObject();
return Arrays.stream(ProductFields).allMatch(m -> json.has(m));
}
public boolean isProductLike(JsonElement value) {
if (!value.isJsonObject())
return false;
JsonObject json = value.getAsJsonObject();
for (Entry<String, JsonElement> e : json.entrySet())
if (Arrays.stream(ProductFields).noneMatch(f -> f.equals(e.getKey())))
return false;
return true;
}
/**
* Test if the given JSON element is a Products array or a
* Products object containing Products or Product-like objects.
* If it is a Products array, it is a JsonArray containing
* Product or Product-like objects. If it is a Products object,
* then it is an object that contains the "products" field which
* is an array of Product or Product-like objects.
*
* @param value
* @param productLike
* @return
*/
public boolean isProducts(JsonElement value, boolean productLike) {
return isProductsArray(value, productLike) ||
isProductsObject(value, productLike);
}
public boolean isProductsArray(JsonElement value, boolean productLike) {
if (!value.isJsonArray())
return false;
for (JsonElement elem : value.getAsJsonArray())
if (!isProduct(elem, productLike))
return false;
return true;
}
public boolean isProductsObject(JsonElement value, boolean productLike) {
if (!value.isJsonObject())
return false;
JsonObject obj = value.getAsJsonObject();
return obj.has("products") &&
isProductsArray(obj.get("products"), productLike);
}
/// Deserialize
/**
* Given a JSON element, deserialize it and return it
* represented as a Product object. The JSON can be Product-like
* and only partially complete, with missing fields.
*
* @param value
* @param productLike
* @return
*/
public Product getProductFromJson(JsonElement value, boolean productLike) {
return isProduct(value, productLike)
? gson.fromJson(value, Product.class)
: null;
}
/**
* Given a JSON element, deserialize it and return it
* represented as a Products object, containing a list of Product
* objects. The JSON can contain objects that are Product-like or only
* contain some of the Product fields, not all of them.
*
* @param value
* @param productLike
* @return
*/
public Products getProductsFromJson(JsonElement value, boolean productLike) {
if (isProductsObject(value, productLike))
return gson.fromJson(value, Products.class);
if (isProductsArray(value, productLike)) {
JsonObject json = new JsonObject();
json.add("products", value);
return gson.fromJson(json, Products.class);
}
if (!isProduct(value, productLike))
return null;
Products products = new Products();
products.add(getProductFromJson(value, productLike));
return products;
}
public static ProductsAPIRequest getInstance() {
if (singleton == null) {
singleton = new ProductsAPIRequest();
}
return singleton;
}
}