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 | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 10 | /** |
| 11 | * A simple single key--value pair forming a {@link StructuredParameter}. |
| 12 | * |
| 13 | * <p>This class is suitable for the common case of logging a key—value pair as parameter to the |
| 14 | * {@code *f} family of logging functions on {@link org.jboss.logging.Logger}. For advanced use |
| 15 | * cases, provide your own implementation of {@link StructuredParameter}. |
| 16 | * |
| 17 | * <p>Example: |
| 18 | * |
| 19 | * <pre>{@code |
| 20 | * logger.infof("Application starting.", StructuredParameter.of("version", "1.0")); |
| 21 | * }</pre> |
| 22 | * |
| 23 | * @see Label |
| 24 | * @see StructuredParameter |
| 25 | */ |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 26 | public final class KeyValueParameter implements StructuredParameter { |
| 27 | |
| 28 | private final String key; |
| 29 | private final JsonValue value; |
| 30 | |
| 31 | private KeyValueParameter(String key, JsonValue value) { |
| 32 | this.key = key; |
| 33 | this.value = value; |
| 34 | } |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 35 | |
| 36 | public static KeyValueParameter of(String key, String value) { |
| 37 | return new KeyValueParameter(key, Json.createValue(value)); |
| 38 | } |
| 39 | |
| 40 | public static KeyValueParameter of(String key, int value) { |
| 41 | return new KeyValueParameter(key, Json.createValue(value)); |
| 42 | } |
| 43 | |
| 44 | public static KeyValueParameter of(String key, long value) { |
| 45 | return new KeyValueParameter(key, Json.createValue(value)); |
| 46 | } |
| 47 | |
| 48 | public static KeyValueParameter of(String key, double value) { |
| 49 | return new KeyValueParameter(key, Json.createValue(value)); |
| 50 | } |
| 51 | |
| 52 | public static KeyValueParameter of(String key, BigDecimal value) { |
| 53 | return new KeyValueParameter(key, Json.createValue(value)); |
| 54 | } |
| 55 | |
| 56 | public static KeyValueParameter of(String key, BigInteger value) { |
| 57 | return new KeyValueParameter(key, Json.createValue(value)); |
| 58 | } |
| 59 | |
| 60 | public static KeyValueParameter of(String key, boolean value) { |
| 61 | return new KeyValueParameter(key, value ? JsonValue.TRUE : JsonValue.FALSE); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public JsonObjectBuilder json() { |
| 66 | return Json.createObjectBuilder().add(key, value); |
| 67 | } |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 68 | |
| 69 | public String key() { |
| 70 | return key; |
| 71 | } |
| 72 | |
| 73 | public JsonValue value() { |
| 74 | return value; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean equals(Object obj) { |
| 79 | if (obj == this) return true; |
| 80 | if (obj == null || obj.getClass() != this.getClass()) return false; |
| 81 | var that = (KeyValueParameter) obj; |
| 82 | return Objects.equals(this.key, that.key) && Objects.equals(this.value, that.value); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public int hashCode() { |
| 87 | return Objects.hash(key, value); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public String toString() { |
| 92 | return "KeyValueParameter[" + "key=" + key + ", " + "value=" + value + ']'; |
| 93 | } |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 94 | } |