summaryrefslogtreecommitdiff
path: root/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/KeyValueParameter.java
blob: a5924b4156e0097e92331b91c6579548e5f6f80b (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package eu.mulk.quarkus.googlecloud.jsonlogging;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Objects;
import javax.json.Json;
import javax.json.JsonObjectBuilder;
import javax.json.JsonValue;

/**
 * A simple single key–value pair forming a {@link StructuredParameter}.
 *
 * <p>This class is suitable for the common case of logging a key–value pair as parameter to the
 * {@code *f} family of logging functions on {@link org.jboss.logging.Logger}. For advanced use
 * cases, provide your own implementation of {@link StructuredParameter}.
 *
 * <p><strong>Example:</strong>
 *
 * <pre>{@code
 * logger.infof("Application starting.", StructuredParameter.of("version", "1.0"));
 * }</pre>
 *
 * Result:
 *
 * <pre>{@code
 * {
 *   "jsonPayload": {
 *     "message": "Application starting.",
 *     "version": "1.0"
 *   }
 * }
 * }</pre>
 *
 * @see Label
 * @see StructuredParameter
 */
public final class KeyValueParameter implements StructuredParameter {

  private final String key;
  private final JsonValue value;

  private KeyValueParameter(String key, JsonValue value) {
    this.key = key;
    this.value = value;
  }

  /**
   * Creates a {@link KeyValueParameter} from a {@link String} value.
   *
   * <p>The resulting JSON value is of type {@code string}.
   *
   * @param key the key part of the key–value pair.
   * @param value the value part of the key–value pair.
   * @return the newly constructed parameter, ready to be passed to a logging function.
   */
  public static KeyValueParameter of(String key, String value) {
    return new KeyValueParameter(key, Json.createValue(value));
  }

  /**
   * Creates a {@link KeyValueParameter} from an {@code int} value.
   *
   * <p>The resulting JSON value is of type {@code number}.
   *
   * @param key the key part of the key–value pair.
   * @param value the value part of the key–value pair.
   * @return the newly constructed parameter, ready to be passed to a logging function.
   */
  public static KeyValueParameter of(String key, int value) {
    return new KeyValueParameter(key, Json.createValue(value));
  }

  /**
   * Creates a {@link KeyValueParameter} from a {@code long} value.
   *
   * <p>The resulting JSON value is of type {@code number}.
   *
   * @param key the key part of the key–value pair.
   * @param value the value part of the key–value pair.
   * @return the newly constructed parameter, ready to be passed to a logging function.
   */
  public static KeyValueParameter of(String key, long value) {
    return new KeyValueParameter(key, Json.createValue(value));
  }

  /**
   * Creates a {@link KeyValueParameter} from a {@code double} value.
   *
   * <p>The resulting JSON value is of type {@code number}.
   *
   * @param key the key part of the key–value pair.
   * @param value the value part of the key–value pair.
   * @return the newly constructed parameter, ready to be passed to a logging function.
   */
  public static KeyValueParameter of(String key, double value) {
    return new KeyValueParameter(key, Json.createValue(value));
  }

  /**
   * Creates a {@link KeyValueParameter} from a {@link BigDecimal} value.
   *
   * <p>The resulting JSON value is of type {@code number}.
   *
   * @param key the key part of the key–value pair.
   * @param value the value part of the key–value pair.
   * @return the newly constructed parameter, ready to be passed to a logging function.
   */
  public static KeyValueParameter of(String key, BigDecimal value) {
    return new KeyValueParameter(key, Json.createValue(value));
  }

  /**
   * Creates a {@link KeyValueParameter} from a {@link BigInteger} value.
   *
   * <p>The resulting JSON value is of type {@code number}.
   *
   * @param key the key part of the key–value pair.
   * @param value the value part of the key–value pair.
   * @return the newly constructed parameter, ready to be passed to a logging function.
   */
  public static KeyValueParameter of(String key, BigInteger value) {
    return new KeyValueParameter(key, Json.createValue(value));
  }

  /**
   * Creates a {@link KeyValueParameter} from a {@code boolean} value.
   *
   * <p>The resulting JSON value is of type {@code boolean}.
   *
   * @param key the key part of the key–value pair.
   * @param value the value part of the key–value pair.
   * @return the newly constructed parameter, ready to be passed to a logging function.
   */
  public static KeyValueParameter of(String key, boolean value) {
    return new KeyValueParameter(key, value ? JsonValue.TRUE : JsonValue.FALSE);
  }

  @Override
  public JsonObjectBuilder json() {
    return Json.createObjectBuilder().add(key, value);
  }

  /**
   * The key part of the key–value pair.
   *
   * @return the key part of the key–value pair.
   */
  public String key() {
    return key;
  }

  /**
   * The value part of the key–value pair.
   *
   * <p>Can be of any non-composite JSON type (i.e. {@code string}, {@code number}, or {@code
   * boolean}).
   *
   * @return the value pairt of the key–value pair.
   */
  public JsonValue value() {
    return value;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == this) return true;
    if (obj == null || obj.getClass() != this.getClass()) return false;
    var that = (KeyValueParameter) obj;
    return Objects.equals(this.key, that.key) && Objects.equals(this.value, that.value);
  }

  @Override
  public int hashCode() {
    return Objects.hash(key, value);
  }

  @Override
  public String toString() {
    return "KeyValueParameter[" + "key=" + key + ", " + "value=" + value + ']';
  }
}