A Transformer from a Set<ByteEnum> -> Integer, plus some new tests.

This commit is contained in:
Ray Greenwell
2010-07-06 18:30:32 +00:00
parent 5ec15839d3
commit 95f2fc6938
3 changed files with 78 additions and 2 deletions
@@ -31,6 +31,9 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import com.samskivert.util.ByteEnum;
import com.samskivert.util.ByteEnumUtil;
import com.samskivert.depot.annotation.Column; import com.samskivert.depot.annotation.Column;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
@@ -108,6 +111,25 @@ public class Transformers
} }
} }
public static class ByteEnumSet<E extends Enum<E> & ByteEnum>
implements Transformer<Set<E>, Integer>
{
public Integer toPersistent (Set<E> value)
{
return (value == null) ? null : ByteEnumUtil.setToInt(value);
}
public Set<E> fromPersistent (Type ftype, Integer encoded)
{
if (encoded == null) {
return null;
}
@SuppressWarnings("unchecked")
Class<E> eclass = (Class<E>) ((ParameterizedType) ftype).getActualTypeArguments()[0];
return ByteEnumUtil.intToSet(eclass, encoded);
}
}
protected abstract static class StringBase<F> implements Transformer<F, String> protected abstract static class StringBase<F> implements Transformer<F, String>
{ {
protected static String encode (Iterable<String> value) protected static String encode (Iterable<String> value)
@@ -22,6 +22,7 @@ package com.samskivert.depot.tests;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@@ -31,6 +32,8 @@ import com.google.common.base.Objects;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.samskivert.util.ByteEnum;
import com.samskivert.depot.Key; import com.samskivert.depot.Key;
import com.samskivert.depot.expression.ColumnExp; import com.samskivert.depot.expression.ColumnExp;
@@ -93,6 +96,14 @@ public class TransformTest extends TestBase
protected short _code; protected short _code;
} }
public enum ExtraOrdinal implements ByteEnum {
ONE, TWO, THREE;
public byte toByte () {
return (byte)ordinal(); // sufficient for testing
}
}
public static class CustomTypeTransformer implements Transformer<CustomType, String> public static class CustomTypeTransformer implements Transformer<CustomType, String>
{ {
public String toPersistent (CustomType value) { public String toPersistent (CustomType value) {
@@ -129,6 +140,7 @@ public class TransformTest extends TestBase
public static final ColumnExp STRING_SET = colexp(_R, "stringSet"); public static final ColumnExp STRING_SET = colexp(_R, "stringSet");
public static final ColumnExp CUSTOM = colexp(_R, "custom"); public static final ColumnExp CUSTOM = colexp(_R, "custom");
public static final ColumnExp ORDINAL = colexp(_R, "ordinal"); public static final ColumnExp ORDINAL = colexp(_R, "ordinal");
public static final ColumnExp BOBS = colexp(_R, "bobs");
// AUTO-GENERATED: FIELDS END // AUTO-GENERATED: FIELDS END
public static final int SCHEMA_VERSION = 1; public static final int SCHEMA_VERSION = 1;
@@ -148,6 +160,9 @@ public class TransformTest extends TestBase
public Ordinal ordinal; public Ordinal ordinal;
@Transform(Transformers.ByteEnumSet.class)
public Set<ExtraOrdinal> bobs;
// AUTO-GENERATED: METHODS START // AUTO-GENERATED: METHODS START
/** /**
* Create and return a primary {@link Key} to identify a {@link TestRecord} * Create and return a primary {@link Key} to identify a {@link TestRecord}
@@ -225,6 +240,7 @@ public class TransformTest extends TestBase
in.stringSet = (strings == null) ? null : Sets.newHashSet(strings); in.stringSet = (strings == null) ? null : Sets.newHashSet(strings);
in.custom = new CustomType("custom"); in.custom = new CustomType("custom");
in.ordinal = Ordinal.THREE; in.ordinal = Ordinal.THREE;
in.bobs = EnumSet.of(ExtraOrdinal.TWO);
_repo.insert(in); _repo.insert(in);
TransformRecord out = _repo.loadNoCache(in.recordId); TransformRecord out = _repo.loadNoCache(in.recordId);
@@ -238,6 +254,7 @@ public class TransformTest extends TestBase
assertTrue(Objects.equal(in.stringSet, out.stringSet)); assertTrue(Objects.equal(in.stringSet, out.stringSet));
assertEquals(in.custom, out.custom); assertEquals(in.custom, out.custom);
assertEquals(in.ordinal, out.ordinal); assertEquals(in.ordinal, out.ordinal);
assertEquals(in.bobs, out.bobs);
// finally clean up after ourselves // finally clean up after ourselves
_repo.delete(TransformRecord.getKey(in.recordId)); _repo.delete(TransformRecord.getKey(in.recordId));
@@ -20,6 +20,7 @@
package com.samskivert.depot.tests; package com.samskivert.depot.tests;
import java.util.EnumSet;
import java.util.Set; import java.util.Set;
import org.junit.Test; import org.junit.Test;
@@ -27,6 +28,8 @@ import static org.junit.Assert.*;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.samskivert.util.ByteEnum;
import com.samskivert.depot.Transformers; import com.samskivert.depot.Transformers;
/** /**
@@ -36,12 +39,13 @@ public class TransformersTest
{ {
@Test public void testStringArray () @Test public void testStringArray ()
{ {
String[] data = { "notabs", "\tpretab", "posttab\t", "in\ttab", "\t\t\tOMGtabs!\t\t" }; String[] data = { "something", "", null, "\nprenewline", "postnewline\n", "in\nnewline",
"a slash \\", "\\ two slashes \\", "\n\n\n" };
Transformers.StringArray xform = new Transformers.StringArray(); Transformers.StringArray xform = new Transformers.StringArray();
assertArrayEquals(data, xform.fromPersistent(null, xform.toPersistent(data))); assertArrayEquals(data, xform.fromPersistent(null, xform.toPersistent(data)));
} }
@Test public void testEmpties () @Test public void testEmptyStringArrays ()
{ {
Transformers.StringArray xform = new Transformers.StringArray(); Transformers.StringArray xform = new Transformers.StringArray();
Set<String> set = Sets.newHashSet(); Set<String> set = Sets.newHashSet();
@@ -51,4 +55,37 @@ public class TransformersTest
set.add(xform.toPersistent(new String[] {""})); set.add(xform.toPersistent(new String[] {""}));
assertTrue(set.size() == 3); assertTrue(set.size() == 3);
} }
@Test public void testByteEnumSets ()
{
Transformers.ByteEnumSet<LilEnum> xform = new Transformers.ByteEnumSet<LilEnum>();
Set<Integer> set = Sets.newHashSet();
set.add(xform.toPersistent(null));
set.add(xform.toPersistent(EnumSet.noneOf(LilEnum.class)));
set.add(xform.toPersistent(EnumSet.of(LilEnum.ONE)));
set.add(xform.toPersistent(EnumSet.of(LilEnum.TWO, LilEnum.THREE)));
set.add(xform.toPersistent(EnumSet.allOf(LilEnum.class)));
assertTrue(set.size() == 5);
set.clear();
for (Set<LilEnum> subset : Sets.powerSet(EnumSet.allOf(LilEnum.class))) {
set.add(xform.toPersistent(subset));
}
assertTrue(set.size() == 8);
}
private static enum LilEnum
implements ByteEnum
{
ONE,
TWO,
THREE;
// from ByteEnum
public byte toByte ()
{
return (byte)ordinal(); // we're just testing here, chill out
}
}
} }