|
| 1 | +package net.swofty.redisapi.util; |
| 2 | + |
| 3 | +import lombok.Getter; |
| 4 | +import org.json.JSONArray; |
| 5 | +import org.json.JSONObject; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.UUID; |
| 11 | + |
| 12 | +/** |
| 13 | + * This is a utility class, used for sending JSONObjects over Redis instead of working with raw Strings. |
| 14 | + */ |
| 15 | +@Getter |
| 16 | +public class RedisParsableMessage { |
| 17 | + private final JSONObject json; |
| 18 | + |
| 19 | + protected RedisParsableMessage(JSONObject json) { |
| 20 | + this.json = json; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Builds a new RedisParsableMessage from a JSONObject. |
| 25 | + * @param fields The fields to build the JSONObject from. |
| 26 | + * @return The built RedisParsableMessage. |
| 27 | + */ |
| 28 | + public static RedisParsableMessage from( Map<String, Object> fields) { |
| 29 | + return from(new JSONObject(fields)); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Builds a new RedisParsableMessage from a JSONObject. |
| 34 | + * @param obj The JSONObject to build the RedisParsableMessage from. |
| 35 | + * @return The built RedisParsableMessage. |
| 36 | + */ |
| 37 | + public static RedisParsableMessage from(JSONObject obj) { |
| 38 | + return new RedisParsableMessage(obj); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Parse a RedisParsableMessage from a raw String. |
| 43 | + * @param raw The raw String to parse. |
| 44 | + * @return The parsed RedisParsableMessage. |
| 45 | + * @throws IllegalArgumentException if the raw String is not a valid JSONObject. |
| 46 | + */ |
| 47 | + public static RedisParsableMessage parse(String raw) { |
| 48 | + String toParse = raw; |
| 49 | + if (raw.contains(";")) { |
| 50 | + String[] split = raw.split(";"); |
| 51 | + toParse = split[1]; |
| 52 | + } |
| 53 | + return new RedisParsableMessage(new JSONObject(toParse)); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Formats the JSONObject into a String to send over Redis, this is the same as {@link #json#toString()}. |
| 58 | + * @return The formatted String. |
| 59 | + */ |
| 60 | + public String formatForSend() { |
| 61 | + return json.toString(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public String toString() { |
| 66 | + return formatForSend(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get an object from the JSONObject. |
| 71 | + * @param key The key to get the object from. |
| 72 | + * @param defaultValue The default value to return if the key is not found. |
| 73 | + * @return The object. |
| 74 | + * @param <T> The type of the object. |
| 75 | + */ |
| 76 | + public <T> T get(String key, T defaultValue) { |
| 77 | + return json.has(key) ? (T) json.get(key) : defaultValue; |
| 78 | + } |
| 79 | + |
| 80 | + /* |
| 81 | + * Beyond here are some utility methods for getting data from the JSONObject. |
| 82 | + */ |
| 83 | + |
| 84 | + /** |
| 85 | + * Get a UUID from the JSONObject. |
| 86 | + * @param key The key to get the UUID from. |
| 87 | + * @return The UUID. |
| 88 | + */ |
| 89 | + public UUID getUUID(String key) { |
| 90 | + return UUID.fromString(get(key, "")); |
| 91 | + } |
| 92 | + |
| 93 | + public JSONArray getJsonArray(String key) { |
| 94 | + return json.has(key) ? json.getJSONArray(key) : new JSONArray(); |
| 95 | + } |
| 96 | + |
| 97 | + public List<String> getStringList(String key) { |
| 98 | + return json.has(key) ? json.getJSONArray(key).toList().stream().map(String::valueOf).toList() : new ArrayList<>(); |
| 99 | + } |
| 100 | + |
| 101 | + public boolean getBoolean(String key) { |
| 102 | + return json.has(key) && json.getBoolean(key); |
| 103 | + } |
| 104 | +} |
0 commit comments