Skip to content

Instantly share code, notes, and snippets.

@mgenov
Created June 22, 2012 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgenov/2971101 to your computer and use it in GitHub Desktop.
Save mgenov/2971101 to your computer and use it in GitHub Desktop.
IterableTranslator.java
protected Collection<Object> createCollection(Type type)
{
// support reusing existing implementations
if (datastore.refresh != null)
{
@SuppressWarnings("unchecked")
Collection<Object> result = (Collection<Object>) datastore.refresh;
datastore.refresh = null;
return result;
}
else
{
if (type instanceof ParameterizedType)
{
ParameterizedType t = (ParameterizedType) type;
Class<?> rawType = (Class<?>) t.getRawType();
if (Set.class.isAssignableFrom(rawType))
{
return new HashSet<Object>();
}
}
return new ArrayList<Object>();
}
}
package com.google.code.twig;
import com.google.code.twig.annotation.AnnotationObjectDatastore;
import com.google.code.twig.annotation.Id;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* @author Miroslav Genov (mgenov@gmail.com)
*/
public class SetPersistenceTest extends LocalDatastoreTestCase {
static class Foo {
@Id
private String key;
private Set<String> items;
}
@Test
public void emptySetIsLoadedWhenEmptySetIsPersisted() {
ObjectDatastore datastore = new AnnotationObjectDatastore(true);
Foo foo = new Foo();
foo.key = "key1";
foo.items = new HashSet<String>();
datastore.store().instance(foo).now();
datastore.disassociateAll();
Foo existingFoo = datastore.load().type(Foo.class).id(foo.key).now();
assertThat(existingFoo.items.size(),is(equalTo(0)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment