Refactor ReceiveMessageHandler

Introduce PlainTextWriter to improve indentation handling.
This commit is contained in:
AsamK 2021-02-20 19:22:36 +01:00
parent 6c33a89f82
commit 03c30519b1
6 changed files with 641 additions and 507 deletions

View file

@ -33,7 +33,7 @@ public abstract class GroupId {
}
}
public GroupId(final byte[] id) {
protected GroupId(final byte[] id) {
this.id = id;
}

View file

@ -6,18 +6,20 @@ import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
public class JsonWriter {
private final OutputStreamWriter writer;
private final Writer writer;
private final ObjectMapper objectMapper;
public JsonWriter(final OutputStream writer) {
this.writer = new OutputStreamWriter(writer, StandardCharsets.UTF_8);
this.writer = new BufferedWriter(new OutputStreamWriter(writer, StandardCharsets.UTF_8));
objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PUBLIC_ONLY);

View file

@ -0,0 +1,23 @@
package org.asamk.signal;
import java.io.IOException;
public interface PlainTextWriter {
void println(String format, Object... args) throws IOException;
PlainTextWriter indentedWriter();
default void println() throws IOException {
println("");
}
default void indent(final WriterConsumer subWriter) throws IOException {
subWriter.consume(indentedWriter());
}
interface WriterConsumer {
void consume(PlainTextWriter writer) throws IOException;
}
}

View file

@ -0,0 +1,67 @@
package org.asamk.signal;
import org.slf4j.helpers.MessageFormatter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
public final class PlainTextWriterImpl implements PlainTextWriter {
private final Writer writer;
private PlainTextWriter indentedWriter;
public PlainTextWriterImpl(final OutputStream outputStream) {
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream));
}
@Override
public void println(String format, Object... args) throws IOException {
final String message = MessageFormatter.arrayFormat(format, args).getMessage();
writer.write(message);
writer.write(System.lineSeparator());
writer.flush();
}
@Override
public PlainTextWriter indentedWriter() {
if (indentedWriter == null) {
indentedWriter = new IndentedPlainTextWriter(this, writer);
}
return indentedWriter;
}
private static final class IndentedPlainTextWriter implements PlainTextWriter {
private final static int INDENTATION = 2;
private final String spaces = " ".repeat(INDENTATION);
private final PlainTextWriter plainTextWriter;
private final Writer writer;
private PlainTextWriter indentedWriter;
private IndentedPlainTextWriter(final PlainTextWriter plainTextWriter, final Writer writer) {
this.plainTextWriter = plainTextWriter;
this.writer = writer;
}
@Override
public void println(final String format, final Object... args) throws IOException {
writer.write(spaces);
plainTextWriter.println(format, args);
}
@Override
public PlainTextWriter indentedWriter() {
if (indentedWriter == null) {
indentedWriter = new IndentedPlainTextWriter(this, writer);
}
return indentedWriter;
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -14,7 +14,7 @@ public class DateUtils {
public static String formatTimestamp(long timestamp) {
Date date = new Date(timestamp);
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); // Quoted "Z" to indicate UTC, no timezone offset
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"); // Quoted "Z" to indicate UTC, no timezone offset
df.setTimeZone(tzUTC);
return timestamp + " (" + df.format(date) + ")";
}