Update to version 1.5.0 (breaking Bundle/Meta)
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / CustomSerializer.java
diff --git a/src/be/nikiroo/utils/serial/CustomSerializer.java b/src/be/nikiroo/utils/serial/CustomSerializer.java
new file mode 100644 (file)
index 0000000..b6928ee
--- /dev/null
@@ -0,0 +1,43 @@
+package be.nikiroo.utils.serial;
+
+public abstract class CustomSerializer {
+
+       protected abstract String toString(Object value);
+
+       protected abstract Object fromString(String content);
+
+       protected abstract String getType();
+
+       public void encode(StringBuilder builder, Object value) {
+               String customString = toString(value);
+               builder.append("custom:").append(getType()).append(":");
+               SerialUtils.encode(builder, customString);
+       }
+
+       public Object decode(String encodedValue) {
+               return fromString((String) SerialUtils.decode(contentOf(encodedValue)));
+       }
+
+       public static boolean isCustom(String encodedValue) {
+               int pos1 = encodedValue.indexOf(':');
+               int pos2 = encodedValue.indexOf(':', pos1 + 1);
+
+               return pos1 >= 0 && pos2 >= 0 && encodedValue.startsWith("custom:");
+       }
+
+       public static String typeOf(String encodedValue) {
+               int pos1 = encodedValue.indexOf(':');
+               int pos2 = encodedValue.indexOf(':', pos1 + 1);
+               String type = encodedValue.substring(pos1 + 1, pos2);
+
+               return type;
+       }
+
+       public static String contentOf(String encodedValue) {
+               int pos1 = encodedValue.indexOf(':');
+               int pos2 = encodedValue.indexOf(':', pos1 + 1);
+               String encodedContent = encodedValue.substring(pos2 + 1);
+
+               return encodedContent;
+       }
+}