|
| 1 | +package com.printnode.api; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonElement; |
| 5 | +import com.google.gson.JsonObject; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +/** |
| 11 | + * Object for a Whoami. |
| 12 | + * */ |
| 13 | +public class Scale { |
| 14 | + |
| 15 | + /** |
| 16 | + * response body that made this Whoami. |
| 17 | + * */ |
| 18 | + private String json; |
| 19 | + |
| 20 | + /** |
| 21 | + * The id of this account. |
| 22 | + * */ |
| 23 | + private int[] mass = new int[2]; |
| 24 | + /** |
| 25 | + * The firstname of this account. |
| 26 | + * */ |
| 27 | + private String deviceName; |
| 28 | + /** |
| 29 | + * The lastname of the authenticated account. |
| 30 | + * */ |
| 31 | + private int deviceNum; |
| 32 | + /** |
| 33 | + * Whether this account can create sub accounts or not. |
| 34 | + * */ |
| 35 | + private String port; |
| 36 | + /** |
| 37 | + * The email registered to this account. |
| 38 | + * */ |
| 39 | + private int count; |
| 40 | + /** |
| 41 | + * The email of the creator of this account. |
| 42 | + * */ |
| 43 | + private HashMap<String, Integer> measurement; |
| 44 | + /** |
| 45 | + * The reference the creator set to this account. |
| 46 | + * */ |
| 47 | + private String clientReportedCreateTimestamp; |
| 48 | + /** |
| 49 | + * An array of child account ids. |
| 50 | + * */ |
| 51 | + private int ntpOffset; |
| 52 | + /** |
| 53 | + * The amount of credits this account has. |
| 54 | + * */ |
| 55 | + private int ageOfData; |
| 56 | + /** |
| 57 | + * The amount of computers this account has. |
| 58 | + * */ |
| 59 | + private int computerId; |
| 60 | + /** |
| 61 | + * The total prints this account has done. |
| 62 | + * */ |
| 63 | + private String vendor; |
| 64 | + /** |
| 65 | + * The versions this account uses. |
| 66 | + * */ |
| 67 | + private String product; |
| 68 | + /** |
| 69 | + * The accounts connected via this account. |
| 70 | + * */ |
| 71 | + private int vendorId; |
| 72 | + /** |
| 73 | + * The tags associated with this account. |
| 74 | + * */ |
| 75 | + private int productId; |
| 76 | + |
| 77 | + |
| 78 | + /** |
| 79 | + * Parses a JsonObject into Whoami. |
| 80 | + * Firstly, it begins iterating over the object. |
| 81 | + * If the object we are converting is a solo JsonPrimitive, |
| 82 | + * we map it directly to the variable. |
| 83 | + * If the object we are converting is an array of JsonPrimitives, |
| 84 | + * we firstly create an array of the same size as it. |
| 85 | + * Then, iterate over it. |
| 86 | + * If the object is a JsonObject with the same mappings each time, it is mapped to a HashMap. |
| 87 | + * If the object is a JsonObject with different mappings, it is mapped to a Java Object. |
| 88 | + * @param response JsonObject of the response. |
| 89 | + * @see JsonObject |
| 90 | + * @see JsonArray |
| 91 | + * @see JsonPrimitive |
| 92 | + * @see JsonElement |
| 93 | + * */ |
| 94 | + public Scale(final JsonObject response) { |
| 95 | + if (!response.get("mass").isJsonNull()) { |
| 96 | + JsonArray jsonMass = response.get("mass").getAsJsonArray(); |
| 97 | + for (int i = 0; i < jsonMass.size(); i++) { |
| 98 | + mass[i] = jsonMass.get(i).isJsonNull() ? -1 : jsonMass.get(i).getAsInt(); |
| 99 | + } |
| 100 | + } |
| 101 | + if (!response.get("deviceName").isJsonNull()) { |
| 102 | + deviceName = response.get("deviceName").getAsString(); |
| 103 | + } |
| 104 | + if (!(response.get("deviceNum").isJsonNull())) { |
| 105 | + deviceNum = response.get("deviceNum").getAsInt(); |
| 106 | + } |
| 107 | + if (!response.get("port").isJsonNull()) { |
| 108 | + port = response.get("port").getAsString(); |
| 109 | + } |
| 110 | + if (!response.get("count").isJsonNull()) { |
| 111 | + count = response.get("count").getAsInt(); |
| 112 | + } |
| 113 | + if (!response.get("measurement").isJsonNull()) { |
| 114 | + JsonObject jsonMeasurement = response.get("measurement").getAsJsonObject(); |
| 115 | + Set<Map.Entry<String, JsonElement>> measurementEntries = jsonMeasurement.entrySet(); |
| 116 | + measurement = new HashMap<String, Integer>(); |
| 117 | + for (Map.Entry<String, JsonElement> measurementEntry : measurementEntries) { |
| 118 | + int jsonMeasurementValue = measurementEntry.getValue().getAsInt(); |
| 119 | + measurement.put(measurementEntry.getKey(), jsonMeasurementValue); |
| 120 | + } |
| 121 | + } |
| 122 | + if (!response.get("clientReportedCreateTimestamp").isJsonNull()) { |
| 123 | + clientReportedCreateTimestamp = response.get("clientReportedCreateTimestamp").getAsString(); |
| 124 | + } |
| 125 | + if (!response.get("ntpOffset").isJsonNull()) { |
| 126 | + ntpOffset = response.get("ntpOffset").getAsInt(); |
| 127 | + } |
| 128 | + if (!response.get("ageOfData").isJsonNull()) { |
| 129 | + ageOfData = response.get("ageOfData").getAsInt(); |
| 130 | + } |
| 131 | + if (!response.get("computerId").isJsonNull()) { |
| 132 | + computerId = response.get("computerId").getAsInt(); |
| 133 | + } |
| 134 | + if (!response.get("vendor").isJsonNull()) { |
| 135 | + vendor = response.get("vendor").getAsString(); |
| 136 | + } |
| 137 | + if (!response.get("product").isJsonNull()) { |
| 138 | + product = response.get("product").getAsString(); |
| 139 | + } |
| 140 | + if (!response.get("vendorId").isJsonNull()) { |
| 141 | + vendorId = response.get("vendorId").getAsInt(); |
| 142 | + } |
| 143 | + if (!response.get("productId").isJsonNull()) { |
| 144 | + productId = response.get("productId").getAsInt(); |
| 145 | + } |
| 146 | + json = response.toString(); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @return id of this account. |
| 151 | + * */ |
| 152 | + public final int[] getMass() { |
| 153 | + return mass; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @return first name of the account holder. |
| 158 | + * */ |
| 159 | + public final String getDeviceName() { |
| 160 | + return deviceName; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * @return last name of the account holder. |
| 165 | + * */ |
| 166 | + public final int getDeviceNum() { |
| 167 | + return deviceNum; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @return whether this account can make sub accounts (i.e is a parent account.) |
| 172 | + * */ |
| 173 | + public final String getPort() { |
| 174 | + return port; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * @return the email for this account. |
| 179 | + * */ |
| 180 | + public final int getCount() { |
| 181 | + return count; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * @return the email of the creator of this account. |
| 186 | + * */ |
| 187 | + public final HashMap<String, Integer> getMeasurement() { |
| 188 | + return measurement; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @return creator reference for this account. |
| 193 | + * */ |
| 194 | + public final String getClientReportedCreateTimestamp() { |
| 195 | + return clientReportedCreateTimestamp; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * @return array of child account ids for this account. |
| 200 | + * */ |
| 201 | + public final int getNtpOffset() { |
| 202 | + return ntpOffset; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * @return number of printing credits. |
| 207 | + * */ |
| 208 | + public final int getAgeOfData() { |
| 209 | + return ageOfData; |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * @return Number of computers used by this account. |
| 214 | + * */ |
| 215 | + public final int getComputerId() { |
| 216 | + return computerId; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * @return total prints done by this account. |
| 221 | + * */ |
| 222 | + public final String getVendor() { |
| 223 | + return vendor; |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * @return an array of client versions used for this account. |
| 228 | + * */ |
| 229 | + public final String getProduct() { |
| 230 | + return product; |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * @return an array of connected clients. |
| 235 | + * */ |
| 236 | + public final int getVendorId() { |
| 237 | + return vendorId; |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * @return a hashamp of the tags this account has. |
| 242 | + * */ |
| 243 | + public final int getProductId() { |
| 244 | + return productId; |
| 245 | + } |
| 246 | + |
| 247 | + /** |
| 248 | + * @return the original response string. |
| 249 | + * */ |
| 250 | + public final String toString() { |
| 251 | + return json; |
| 252 | + } |
| 253 | + |
| 254 | +} |
0 commit comments