Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 1 | package eu.mulk.quarkus.googlecloud.jsonlogging; |
| 2 | |
| 3 | import java.math.BigDecimal; |
| 4 | import java.math.BigInteger; |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 5 | import java.util.Objects; |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 6 | import javax.json.Json; |
| 7 | import javax.json.JsonObjectBuilder; |
| 8 | import javax.json.JsonValue; |
| 9 | |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 10 | public final class KeyValueParameter implements StructuredParameter { |
| 11 | |
| 12 | private final String key; |
| 13 | private final JsonValue value; |
| 14 | |
| 15 | private KeyValueParameter(String key, JsonValue value) { |
| 16 | this.key = key; |
| 17 | this.value = value; |
| 18 | } |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 19 | |
| 20 | public static KeyValueParameter of(String key, String value) { |
| 21 | return new KeyValueParameter(key, Json.createValue(value)); |
| 22 | } |
| 23 | |
| 24 | public static KeyValueParameter of(String key, int value) { |
| 25 | return new KeyValueParameter(key, Json.createValue(value)); |
| 26 | } |
| 27 | |
| 28 | public static KeyValueParameter of(String key, long value) { |
| 29 | return new KeyValueParameter(key, Json.createValue(value)); |
| 30 | } |
| 31 | |
| 32 | public static KeyValueParameter of(String key, double value) { |
| 33 | return new KeyValueParameter(key, Json.createValue(value)); |
| 34 | } |
| 35 | |
| 36 | public static KeyValueParameter of(String key, BigDecimal value) { |
| 37 | return new KeyValueParameter(key, Json.createValue(value)); |
| 38 | } |
| 39 | |
| 40 | public static KeyValueParameter of(String key, BigInteger value) { |
| 41 | return new KeyValueParameter(key, Json.createValue(value)); |
| 42 | } |
| 43 | |
| 44 | public static KeyValueParameter of(String key, boolean value) { |
| 45 | return new KeyValueParameter(key, value ? JsonValue.TRUE : JsonValue.FALSE); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public JsonObjectBuilder json() { |
| 50 | return Json.createObjectBuilder().add(key, value); |
| 51 | } |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 52 | |
| 53 | public String key() { |
| 54 | return key; |
| 55 | } |
| 56 | |
| 57 | public JsonValue value() { |
| 58 | return value; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean equals(Object obj) { |
| 63 | if (obj == this) return true; |
| 64 | if (obj == null || obj.getClass() != this.getClass()) return false; |
| 65 | var that = (KeyValueParameter) obj; |
| 66 | return Objects.equals(this.key, that.key) && Objects.equals(this.value, that.value); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public int hashCode() { |
| 71 | return Objects.hash(key, value); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public String toString() { |
| 76 | return "KeyValueParameter[" + "key=" + key + ", " + "value=" + value + ']'; |
| 77 | } |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 78 | } |