com.brettonw.bag.FormatWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bag Show documentation
Show all versions of bag Show documentation
Bag provides two container classes for text-based storage of constrained types in
an array (BagArray) or as a map (BagObject), with a means of serializing objects to and from
these container types.
package com.brettonw.bag;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
abstract public class FormatWriter {
protected static final String[] QUOTES = { "\"" };
protected String enclose (String input, String[] bracket) {
String bracket0 = bracket[0];
String bracket1 = (bracket.length > 1) ? bracket[1] : bracket0;
return bracket0 + input + bracket1;
}
protected String quote (String input) {
return enclose (input, QUOTES);
}
abstract public String write (BagObject bagObject);
abstract public String write (BagArray bagArray);
// static type registration by name
private static final Map formatWriters = new HashMap<>();
public static void registerFormatWriter (String format, boolean replace, Supplier supplier) {
if ((! replace) || (! formatWriters.containsKey(format))) {
formatWriters.put(format, supplier.get());
}
}
public static String write (BagObject bagObject, String format) {
if (formatWriters.containsKey(format)) {
return formatWriters.get(format).write (bagObject);
}
return null;
}
public static String write (BagArray bagArray, String format) {
if (formatWriters.containsKey(format)) {
return formatWriters.get(format).write (bagArray);
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy