All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.l4.commons.serialization.collections.SetSerializerResolver Maven / Gradle / Ivy

There is a newer version: 0.4.2
Show newest version
package se.l4.commons.serialization.collections;

import java.lang.annotation.Annotation;
import java.util.Set;

import com.google.common.collect.ImmutableSet;

import se.l4.commons.serialization.SerializationException;
import se.l4.commons.serialization.Serializer;
import se.l4.commons.serialization.spi.AbstractSerializerResolver;
import se.l4.commons.serialization.spi.Type;
import se.l4.commons.serialization.spi.TypeEncounter;
import se.l4.commons.serialization.spi.TypeViaClass;

public class SetSerializerResolver
	extends AbstractSerializerResolver>
{
	private static final Set> HINTS =
		ImmutableSet.>of(AllowAnyItem.class, Item.class);

	@Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Serializer> find(TypeEncounter encounter)
	{
		Type[] params = encounter.getType().getParameters();
		Type type = params.length == 0 ? new TypeViaClass(Object.class) : params[0];  
		
		// Check that we can create the type of list requested
		Class erasedType = encounter.getType().getErasedType();
		if(erasedType != Set.class)
		{
			throw new SerializationException("Sets can only be serialized if they are declared as the interface Set");
		}
		
		Serializer itemSerializer = CollectionSerializers.resolveSerializer(encounter, type);
			
		return new SetSerializer(itemSerializer);
	}

	@Override
	public Set> getHints()
	{
		return HINTS;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy