From c7eb6f56ef1d457d87b853aeb7e5c31497850d80 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Thu, 30 Sep 2010 22:25:16 +0000 Subject: [PATCH] Roll this back until I do some more testing.. :( --- .../com/samskivert/depot/Transformers.java | 173 ++++-------------- 1 file changed, 34 insertions(+), 139 deletions(-) diff --git a/src/main/java/com/samskivert/depot/Transformers.java b/src/main/java/com/samskivert/depot/Transformers.java index 7d91fb8..16412e7 100644 --- a/src/main/java/com/samskivert/depot/Transformers.java +++ b/src/main/java/com/samskivert/depot/Transformers.java @@ -38,10 +38,6 @@ import com.samskivert.depot.annotation.Column; import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Interner; -import com.google.common.collect.Interners; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Sets; @@ -66,23 +62,14 @@ public class Transformers */ public static class StringArray extends StringBase { - protected Iterable toIterable (String[] value) + public String toPersistent (String[] value) { - return Arrays.asList(value); + return (value == null) ? null : encode(Arrays.asList(value)); } - protected Builder createBuilder (Type ftype, String encoded) + public String[] fromPersistent (Type ftype, String encoded) { - final String[] result = new String[countElements(encoded)]; - return new Builder() { - public void add (String s) { - result[idx++] = s; - } - public String[] build () { - return result; - } - protected int idx = 0; - }; + return (encoded == null) ? null : Iterables.toArray(decode(encoded), String.class); } } @@ -92,85 +79,36 @@ public class Transformers * backslash, newlines will be encoded as "\n", and null elements will be encoded as "\0" (but * not terminated by a newline). */ - public static class StringIterable extends StringIterableBase + public static class StringIterable extends StringBase> { - protected Builder> createBuilder (Type ftype, String encoded) + public String toPersistent (Iterable value) { - Type fclass = (ftype instanceof ParameterizedType) ? - ((ParameterizedType)ftype).getRawType() : ftype; - final Collection collection = createCollection(fclass, encoded); - return new Builder>() { - public void add (String s) { - collection.add(s); - } - public Iterable build () { - return collection; - } - }; + return (value == null) ? null : encode(value); } - protected Collection createCollection (Type fclass, String encoded) - { - // TODO: TreeSet, etc, etc - if (fclass == HashSet.class || fclass == Set.class) { - return Sets.newHashSet(); - - } else if (fclass == LinkedList.class) { - return Lists.newLinkedList(); - - } else { - return Lists.newArrayList(); - } - } - } - - public static class ImmutableStringIterable extends StringIterableBase - { - protected Builder> createBuilder (Type ftype, String encoded) - { - Type fclass = (ftype instanceof ParameterizedType) ? - ((ParameterizedType)ftype).getRawType() : ftype; - // TODO: SortedSet - if (fclass == Set.class) { - return new Builder>() { - public void add (String s) { - _builder.add(s); - } - public Iterable build () { - return _builder.build(); - } - protected ImmutableSet.Builder _builder = ImmutableSet.builder(); - }; - - } else { //if (fclass == List.class) - return new Builder>() { - public void add (String s) { - _builder.add(s); - } - public Iterable build () { - return _builder.build(); - } - protected ImmutableList.Builder _builder = ImmutableList.builder(); - }; - } - } - } - - public static class InternedImmutableStringIterable extends ImmutableStringIterable - { - @Override public Iterable fromPersistent (Type ftype, String encoded) { - Iterable result = super.fromPersistent(ftype, encoded); - return (result == null) ? result : INTERNER.intern(result); - } - - @Override protected boolean doInterning () - { - return true; - } + if (encoded == null) { + return null; + } - protected static final Interner> INTERNER = Interners.newWeakInterner(); + ArrayList value = decode(encoded); + Type fclass = (ftype instanceof ParameterizedType) ? + ((ParameterizedType)ftype).getRawType() : ftype; + if (fclass == ArrayList.class || fclass == List.class || + fclass == Collection.class || fclass == Iterable.class) { + return value; + } + if (fclass == LinkedList.class) { + return Lists.newLinkedList(value); + } + if (fclass == HashSet.class || fclass == Set.class) { + return Sets.newHashSet(value); + } + // else: reflection? See if it's a collection, call the 0-arg constructor, add all + // and return? Something? + return value; + } } public static class ByteEnumSet & ByteEnum> @@ -194,14 +132,10 @@ public class Transformers protected abstract static class StringBase implements Transformer { - public String toPersistent (F value) + protected static String encode (Iterable value) { - if (value == null) { - return null; - } - StringBuilder buf = new StringBuilder(); - for (String s : toIterable(value)) { + for (String s : value) { if (s == null) { buf.append("\\\n"); // encode nulls as slash followed by the terminator } else { @@ -213,21 +147,15 @@ public class Transformers return buf.toString(); } - public F fromPersistent (Type ftype, String encoded) + protected static ArrayList decode (String encoded) { - if (encoded == null) { - return null; - } - - Builder builder = createBuilder(ftype, encoded); - boolean intern = doInterning(); + ArrayList value = Lists.newArrayList(); StringBuilder buf = new StringBuilder(encoded.length()); for (int ii = 0, nn = encoded.length(); ii < nn; ii++) { char c = encoded.charAt(ii); switch (c) { case '\n': - String s = buf.toString(); - builder.add(intern ? s.intern() : s); + value.add(buf.toString()); // TODO: intern? buf.setLength(0); break; @@ -237,7 +165,7 @@ public class Transformers switch (slashed) { case '\n': // turn back into a null element Preconditions.checkArgument(buf.length() == 0, "Invalid encoded string"); - builder.add(null); + value.add(null); break; case 'n': // turn \n back into a newline buf.append('\n'); @@ -253,42 +181,9 @@ public class Transformers break; } } + // make sure the last element was terminated Preconditions.checkArgument(buf.length() == 0, "Invalid encoded string"); - return builder.build(); - } - - protected boolean doInterning () - { - return false; - } - - protected abstract Iterable toIterable (F value); - - protected abstract Builder createBuilder (Type ftype, String encoded); - - /** - * Count the number of elements in the encoded non-null string. - */ - protected static int countElements (String encoded) - { - int count = 0; - for (int pos = 0; 0 != (pos = 1 + encoded.indexOf('\n', pos)); count++) {} - return count; - } - - protected interface Builder - { - void add (String s); - - F build (); - } - } - - protected static abstract class StringIterableBase extends StringBase> - { - protected Iterable toIterable (Iterable value) - { return value; } }