mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-12-29 11:42:15 +00:00
Conform to the project's singleton pattern.
This commit is contained in:
parent
8b3652a309
commit
24a6503d19
7 changed files with 27 additions and 10 deletions
|
@ -5,6 +5,8 @@ import java.lang.reflect.Array;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import static com.moandjiezana.toml.ValueWriters.WRITERS;
|
||||
|
||||
abstract class ArrayValueWriter implements ValueWriter {
|
||||
static protected boolean isArrayish(Object value) {
|
||||
return value instanceof Collection || value.getClass().isArray();
|
||||
|
@ -23,7 +25,7 @@ abstract class ArrayValueWriter implements ValueWriter {
|
|||
static boolean isArrayOfPrimitive(Object array) {
|
||||
Object first = peek(array);
|
||||
if (first != null) {
|
||||
ValueWriter valueWriter = ValueWriters.findWriterFor(first);
|
||||
ValueWriter valueWriter = WRITERS.findWriterFor(first);
|
||||
return valueWriter.isPrimitiveType() || isArrayish(first);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import java.util.regex.Pattern;
|
|||
|
||||
import static com.moandjiezana.toml.PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER;
|
||||
import static com.moandjiezana.toml.TableArrayValueWriter.TABLE_ARRAY_VALUE_WRITER;
|
||||
import static com.moandjiezana.toml.ValueWriters.WRITERS;
|
||||
|
||||
class MapValueWriter implements ValueWriter {
|
||||
static final ValueWriter MAP_VALUE_WRITER = new MapValueWriter();
|
||||
|
@ -33,7 +34,7 @@ class MapValueWriter implements ValueWriter {
|
|||
continue;
|
||||
}
|
||||
|
||||
ValueWriter valueWriter = ValueWriters.findWriterFor(fromValue);
|
||||
ValueWriter valueWriter = WRITERS.findWriterFor(fromValue);
|
||||
if (valueWriter.isPrimitiveType()) {
|
||||
context.indent();
|
||||
context.output.append(quoteKey(key)).append(" = ");
|
||||
|
@ -53,7 +54,7 @@ class MapValueWriter implements ValueWriter {
|
|||
continue;
|
||||
}
|
||||
|
||||
ValueWriter valueWriter = ValueWriters.findWriterFor(fromValue);
|
||||
ValueWriter valueWriter = WRITERS.findWriterFor(fromValue);
|
||||
if (valueWriter.isTable() || valueWriter == TABLE_ARRAY_VALUE_WRITER) {
|
||||
valueWriter.write(fromValue, context.extend(quoteKey(key)));
|
||||
}
|
||||
|
@ -87,7 +88,7 @@ class MapValueWriter implements ValueWriter {
|
|||
continue;
|
||||
}
|
||||
|
||||
ValueWriter valueWriter = ValueWriters.findWriterFor(fromValue);
|
||||
ValueWriter valueWriter = WRITERS.findWriterFor(fromValue);
|
||||
if (valueWriter.isPrimitiveType() || valueWriter == PRIMITIVE_ARRAY_VALUE_WRITER) {
|
||||
return true;
|
||||
}
|
||||
|
@ -95,4 +96,6 @@ class MapValueWriter implements ValueWriter {
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
private MapValueWriter() {}
|
||||
}
|
||||
|
|
|
@ -72,4 +72,6 @@ class ObjectValueWriter implements ValueWriter {
|
|||
|
||||
return value;
|
||||
}
|
||||
|
||||
private ObjectValueWriter() {}
|
||||
}
|
||||
|
|
|
@ -20,9 +20,11 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter {
|
|||
if (!first) {
|
||||
context.output.append(", ");
|
||||
}
|
||||
ValueWriters.write(elem, context);
|
||||
ValueWriters.WRITERS.write(elem, context);
|
||||
first = false;
|
||||
}
|
||||
context.output.append(" ]");
|
||||
}
|
||||
|
||||
private PrimitiveArrayValueWriter() {}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@ package com.moandjiezana.toml;
|
|||
|
||||
import java.util.Collection;
|
||||
|
||||
import static com.moandjiezana.toml.ValueWriters.WRITERS;
|
||||
|
||||
class TableArrayValueWriter extends ArrayValueWriter {
|
||||
static final ValueWriter TABLE_ARRAY_VALUE_WRITER = new TableArrayValueWriter();
|
||||
|
||||
|
@ -17,7 +19,9 @@ class TableArrayValueWriter extends ArrayValueWriter {
|
|||
WriterContext subContext = context.extend().setIsArrayOfTable(true);
|
||||
|
||||
for (Object elem : values) {
|
||||
ValueWriters.write(elem, subContext);
|
||||
WRITERS.write(elem, subContext);
|
||||
}
|
||||
}
|
||||
|
||||
private TableArrayValueWriter() {}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ import java.io.*;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.moandjiezana.toml.ValueWriters.WRITERS;
|
||||
|
||||
/**
|
||||
* <p>Converts Objects to TOML</p>
|
||||
*
|
||||
|
@ -34,7 +36,7 @@ public class TomlWriter {
|
|||
* @return a string containing the TOML representation of the given Object
|
||||
*/
|
||||
public String write(Object from) {
|
||||
return ValueWriters.write(from);
|
||||
return WRITERS.write(from);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -11,7 +11,9 @@ import static com.moandjiezana.toml.TableArrayValueWriter.TABLE_ARRAY_VALUE_WRIT
|
|||
|
||||
class ValueWriters {
|
||||
|
||||
static ValueWriter findWriterFor(Object value) {
|
||||
static final ValueWriters WRITERS = new ValueWriters();
|
||||
|
||||
ValueWriter findWriterFor(Object value) {
|
||||
for (ValueWriter valueWriter : VALUE_WRITERs) {
|
||||
if (valueWriter.canWrite(value)) {
|
||||
return valueWriter;
|
||||
|
@ -21,14 +23,14 @@ class ValueWriters {
|
|||
return OBJECT_VALUE_WRITER;
|
||||
}
|
||||
|
||||
static String write(Object value) {
|
||||
String write(Object value) {
|
||||
WriterContext context = new WriterContext();
|
||||
write(value, context);
|
||||
|
||||
return context.output.toString();
|
||||
}
|
||||
|
||||
static void write(Object value, WriterContext context) {
|
||||
void write(Object value, WriterContext context) {
|
||||
findWriterFor(value).write(value, context);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue