From f29bb2c627a3d5bfff91e82a3f0deb90db7e49cd Mon Sep 17 00:00:00 2001 From: Bertrand Renuart Date: Wed, 8 Sep 2021 11:43:32 +0200 Subject: [PATCH] Code cleanup and additional javadoc --- .../argument/DeferredStructuredArgument.java | 13 ++++++++++ .../marker/DeferredLogstashMarker.java | 19 +++++++++++--- .../logback/marker/EmptyLogstashMarker.java | 1 + .../marker/MapEntriesAppendingMarker.java | 8 +----- .../logback/marker/ObjectAppendingMarker.java | 26 +++++++------------ .../marker/ObjectFieldsAppendingMarker.java | 12 +++------ .../marker/RawJsonAppendingMarker.java | 8 +----- .../marker/SingleFieldAppendingMarker.java | 8 +----- 8 files changed, 46 insertions(+), 49 deletions(-) diff --git a/src/main/java/net/logstash/logback/argument/DeferredStructuredArgument.java b/src/main/java/net/logstash/logback/argument/DeferredStructuredArgument.java index 57568a46..0cb09486 100644 --- a/src/main/java/net/logstash/logback/argument/DeferredStructuredArgument.java +++ b/src/main/java/net/logstash/logback/argument/DeferredStructuredArgument.java @@ -50,8 +50,15 @@ */ public class DeferredStructuredArgument implements StructuredArgument { + /** + * Supplier for the deferred {@link StructuredArgument} + */ private final Supplier structureArgumentSupplier; + /** + * Cached value of the structured argument returned by {@link #structureArgumentSupplier}. + * {@code null} until {@link #getSuppliedValue()} is first called. + */ private volatile StructuredArgument suppliedValue; public DeferredStructuredArgument(Supplier structureArgumentSupplier) { @@ -63,6 +70,12 @@ public void writeTo(JsonGenerator generator) throws IOException { getSuppliedValue().writeTo(generator); } + /** + * Get the deferred structure argument from the supplier or return it from the cache + * if already initialized. + * + * @return the deferred structured argument + */ private StructuredArgument getSuppliedValue() { if (suppliedValue == null) { synchronized (this) { diff --git a/src/main/java/net/logstash/logback/marker/DeferredLogstashMarker.java b/src/main/java/net/logstash/logback/marker/DeferredLogstashMarker.java index 2187342d..86e9d118 100644 --- a/src/main/java/net/logstash/logback/marker/DeferredLogstashMarker.java +++ b/src/main/java/net/logstash/logback/marker/DeferredLogstashMarker.java @@ -39,12 +39,20 @@ * the supplier will be invoked when the first appender encodes the marker. * That same supplied value will be used when the next appender encodes the marker.

*/ +@SuppressWarnings("serial") public class DeferredLogstashMarker extends LogstashMarker { public static final String DEFERRED_MARKER_NAME = "DEFERRED"; - private final Supplier logstashMarkerSupplier; + /** + * Supplier for the deferred marker + */ + private final Supplier logstashMarkerSupplier; + /** + * Cached value of the marker returned by {@link #logstashMarkerSupplier}. + * {@code null} until {@link #getSuppliedValue()} is first called. + */ private volatile LogstashMarker suppliedValue; public DeferredLogstashMarker(Supplier logstashMarkerSupplier) { @@ -57,6 +65,12 @@ public void writeTo(JsonGenerator generator) throws IOException { writeMarker(generator, getSuppliedValue()); } + /** + * Get the deferred marker from the supplier or return it from the cache + * if already initialized. + * + * @return the deferred marker + */ private LogstashMarker getSuppliedValue() { if (suppliedValue == null) { synchronized (this) { @@ -82,8 +96,7 @@ private void writeMarker(JsonGenerator generator, Marker marker) throws IOExcept if (marker.hasReferences()) { for (Iterator i = marker.iterator(); i.hasNext();) { - Marker next = i.next(); - writeMarker(generator, next); + writeMarker(generator, i.next()); } } } diff --git a/src/main/java/net/logstash/logback/marker/EmptyLogstashMarker.java b/src/main/java/net/logstash/logback/marker/EmptyLogstashMarker.java index 423de354..44855e8f 100644 --- a/src/main/java/net/logstash/logback/marker/EmptyLogstashMarker.java +++ b/src/main/java/net/logstash/logback/marker/EmptyLogstashMarker.java @@ -36,6 +36,7 @@ * } * */ +@SuppressWarnings("serial") public class EmptyLogstashMarker extends LogstashMarker implements StructuredArgument { public static final String EMPTY_MARKER_NAME = "EMPTY"; diff --git a/src/main/java/net/logstash/logback/marker/MapEntriesAppendingMarker.java b/src/main/java/net/logstash/logback/marker/MapEntriesAppendingMarker.java index 6f2a1d8f..884d556c 100755 --- a/src/main/java/net/logstash/logback/marker/MapEntriesAppendingMarker.java +++ b/src/main/java/net/logstash/logback/marker/MapEntriesAppendingMarker.java @@ -99,13 +99,7 @@ public String toStringSelf() { @Override public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!super.equals(obj)) { - return false; - } - if (!(obj instanceof MapEntriesAppendingMarker)) { + if (!(obj instanceof MapEntriesAppendingMarker && super.equals(obj))) { return false; } diff --git a/src/main/java/net/logstash/logback/marker/ObjectAppendingMarker.java b/src/main/java/net/logstash/logback/marker/ObjectAppendingMarker.java index 2480782e..63873a45 100755 --- a/src/main/java/net/logstash/logback/marker/ObjectAppendingMarker.java +++ b/src/main/java/net/logstash/logback/marker/ObjectAppendingMarker.java @@ -76,42 +76,36 @@ public class ObjectAppendingMarker extends SingleFieldAppendingMarker { * The object to write as the field's value. * Can be a {@link String}, {@link Number}, array, or some other object that can be processed by an {@link ObjectMapper} */ - private final Object object; + private final Object fieldValue; - public ObjectAppendingMarker(String fieldName, Object object) { + public ObjectAppendingMarker(String fieldName, Object fieldValue) { super(MARKER_NAME, fieldName); - this.object = object; + this.fieldValue = fieldValue; } - public ObjectAppendingMarker(String fieldName, Object object, String messageFormatPattern) { + public ObjectAppendingMarker(String fieldName, Object fieldValue, String messageFormatPattern) { super(MARKER_NAME, fieldName, messageFormatPattern); - this.object = object; + this.fieldValue = fieldValue; } @Override protected void writeFieldValue(JsonGenerator generator) throws IOException { - generator.writeObject(object); + generator.writeObject(fieldValue); } @Override public Object getFieldValue() { - return StructuredArguments.toString(object); + return StructuredArguments.toString(fieldValue); } @Override public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!super.equals(obj)) { - return false; - } - if (!(obj instanceof ObjectAppendingMarker)) { + if (!(obj instanceof ObjectAppendingMarker && super.equals(obj))) { return false; } ObjectAppendingMarker other = (ObjectAppendingMarker) obj; - return Objects.equals(this.object, other.object); + return Objects.equals(this.fieldValue, other.fieldValue); } @Override @@ -119,7 +113,7 @@ public int hashCode() { final int prime = 31; int result = 1; result = prime * result + super.hashCode(); - result = prime * result + (this.object == null ? 0 : this.object.hashCode()); + result = prime * result + (this.fieldValue == null ? 0 : this.fieldValue.hashCode()); return result; } } diff --git a/src/main/java/net/logstash/logback/marker/ObjectFieldsAppendingMarker.java b/src/main/java/net/logstash/logback/marker/ObjectFieldsAppendingMarker.java index e97d89c2..816a2d50 100755 --- a/src/main/java/net/logstash/logback/marker/ObjectFieldsAppendingMarker.java +++ b/src/main/java/net/logstash/logback/marker/ObjectFieldsAppendingMarker.java @@ -91,8 +91,8 @@ public class ObjectFieldsAppendingMarker extends LogstashMarker implements Struc * * Since apps will typically serialize the same types of objects repeatedly, they shouldn't grow too much. */ - private static final ConcurrentHashMap, JsonSerializer> beanSerializers = new ConcurrentHashMap, JsonSerializer>(); - private static final ConcurrentHashMap serializerProviders = new ConcurrentHashMap(); + private static final ConcurrentHashMap, JsonSerializer> beanSerializers = new ConcurrentHashMap<>(); + private static final ConcurrentHashMap serializerProviders = new ConcurrentHashMap<>(); public ObjectFieldsAppendingMarker(Object object) { super(MARKER_NAME); @@ -168,13 +168,7 @@ private SerializerProvider getSerializerProvider(ObjectMapper mapper) { @Override public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!super.equals(obj)) { - return false; - } - if (!(obj instanceof ObjectFieldsAppendingMarker)) { + if (!(obj instanceof ObjectFieldsAppendingMarker && super.equals(obj))) { return false; } diff --git a/src/main/java/net/logstash/logback/marker/RawJsonAppendingMarker.java b/src/main/java/net/logstash/logback/marker/RawJsonAppendingMarker.java index 9852c0f6..93666c91 100755 --- a/src/main/java/net/logstash/logback/marker/RawJsonAppendingMarker.java +++ b/src/main/java/net/logstash/logback/marker/RawJsonAppendingMarker.java @@ -72,13 +72,7 @@ public Object getFieldValue() { @Override public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!super.equals(obj)) { - return false; - } - if (!(obj instanceof RawJsonAppendingMarker)) { + if (!(obj instanceof RawJsonAppendingMarker && super.equals(obj))) { return false; } diff --git a/src/main/java/net/logstash/logback/marker/SingleFieldAppendingMarker.java b/src/main/java/net/logstash/logback/marker/SingleFieldAppendingMarker.java index 1b1679f7..3e2cbf51 100755 --- a/src/main/java/net/logstash/logback/marker/SingleFieldAppendingMarker.java +++ b/src/main/java/net/logstash/logback/marker/SingleFieldAppendingMarker.java @@ -133,13 +133,7 @@ public String toStringSelf() { @Override public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!super.equals(obj)) { - return false; - } - if (!(obj instanceof SingleFieldAppendingMarker)) { + if (!(obj instanceof SingleFieldAppendingMarker && super.equals(obj))) { return false; }