mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-30 11:00:38 +00:00
Clean up base package
This commit is contained in:
parent
382d8d22d0
commit
fc8b6d0fcb
60 changed files with 102 additions and 100 deletions
6
src/main/java/org/asamk/signal/output/JsonWriter.java
Normal file
6
src/main/java/org/asamk/signal/output/JsonWriter.java
Normal file
|
@ -0,0 +1,6 @@
|
|||
package org.asamk.signal.output;
|
||||
|
||||
public interface JsonWriter extends OutputWriter {
|
||||
|
||||
void write(final Object object);
|
||||
}
|
35
src/main/java/org/asamk/signal/output/JsonWriterImpl.java
Normal file
35
src/main/java/org/asamk/signal/output/JsonWriterImpl.java
Normal file
|
@ -0,0 +1,35 @@
|
|||
package org.asamk.signal.output;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.asamk.signal.util.Util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
public class JsonWriterImpl implements JsonWriter {
|
||||
|
||||
private final Writer writer;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public JsonWriterImpl(final Writer writer) {
|
||||
this.writer = writer;
|
||||
this.objectMapper = Util.createJsonObjectMapper();
|
||||
}
|
||||
|
||||
public synchronized void write(final Object object) {
|
||||
try {
|
||||
try {
|
||||
objectMapper.writeValue(writer, object);
|
||||
} catch (JsonProcessingException e) {
|
||||
// Some issue with json serialization, probably caused by a bug
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
writer.write(System.lineSeparator());
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
}
|
5
src/main/java/org/asamk/signal/output/OutputWriter.java
Normal file
5
src/main/java/org/asamk/signal/output/OutputWriter.java
Normal file
|
@ -0,0 +1,5 @@
|
|||
package org.asamk.signal.output;
|
||||
|
||||
public interface OutputWriter {
|
||||
|
||||
}
|
18
src/main/java/org/asamk/signal/output/PlainTextWriter.java
Normal file
18
src/main/java/org/asamk/signal/output/PlainTextWriter.java
Normal file
|
@ -0,0 +1,18 @@
|
|||
package org.asamk.signal.output;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface PlainTextWriter extends OutputWriter {
|
||||
|
||||
void println(String format, Object... args);
|
||||
|
||||
PlainTextWriter indentedWriter();
|
||||
|
||||
default void println() {
|
||||
println("");
|
||||
}
|
||||
|
||||
default void indent(final Consumer<PlainTextWriter> subWriter) {
|
||||
subWriter.accept(indentedWriter());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package org.asamk.signal.output;
|
||||
|
||||
import org.slf4j.helpers.MessageFormatter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
public final class PlainTextWriterImpl implements PlainTextWriter {
|
||||
|
||||
private final Writer writer;
|
||||
|
||||
private PlainTextWriter indentedWriter;
|
||||
|
||||
public PlainTextWriterImpl(final Writer writer) {
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(String format, Object... args) {
|
||||
final var message = MessageFormatter.arrayFormat(format, args).getMessage();
|
||||
|
||||
try {
|
||||
writer.write(message);
|
||||
writer.write(System.lineSeparator());
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@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) {
|
||||
try {
|
||||
writer.write(spaces);
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
plainTextWriter.println(format, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlainTextWriter indentedWriter() {
|
||||
if (indentedWriter == null) {
|
||||
indentedWriter = new IndentedPlainTextWriter(this, writer);
|
||||
}
|
||||
return indentedWriter;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue