Skip to content

Commit

Permalink
Code cleanup and additional javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
brenuart committed Sep 8, 2021
1 parent eab26c6 commit f29bb2c
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@
*/
public class DeferredStructuredArgument implements StructuredArgument {

/**
* Supplier for the deferred {@link StructuredArgument}
*/
private final Supplier<? extends StructuredArgument> 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<? extends StructuredArgument> structureArgumentSupplier) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.</p>
*/
@SuppressWarnings("serial")
public class DeferredLogstashMarker extends LogstashMarker {

public static final String DEFERRED_MARKER_NAME = "DEFERRED";

private final Supplier<? extends LogstashMarker> logstashMarkerSupplier;
/**
* Supplier for the deferred marker
*/
private final Supplier<? extends LogstashMarker> 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<? extends LogstashMarker> logstashMarkerSupplier) {
Expand All @@ -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) {
Expand All @@ -82,8 +96,7 @@ private void writeMarker(JsonGenerator generator, Marker marker) throws IOExcept

if (marker.hasReferences()) {
for (Iterator<Marker> i = marker.iterator(); i.hasNext();) {
Marker next = i.next();
writeMarker(generator, next);
writeMarker(generator, i.next());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* }
* </pre>
*/
@SuppressWarnings("serial")
public class EmptyLogstashMarker extends LogstashMarker implements StructuredArgument {

public static final String EMPTY_MARKER_NAME = "EMPTY";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,50 +76,44 @@ 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
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class<?>, JsonSerializer<Object>> beanSerializers = new ConcurrentHashMap<Class<?>, JsonSerializer<Object>>();
private static final ConcurrentHashMap<ObjectMapper, SerializerProvider> serializerProviders = new ConcurrentHashMap<ObjectMapper, SerializerProvider>();
private static final ConcurrentHashMap<Class<?>, JsonSerializer<Object>> beanSerializers = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<ObjectMapper, SerializerProvider> serializerProviders = new ConcurrentHashMap<>();

public ObjectFieldsAppendingMarker(Object object) {
super(MARKER_NAME);
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit f29bb2c

Please sign in to comment.