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

se.l4.commons.serialization.spi.SerializerResolverChain Maven / Gradle / Ivy

The newest version!
package se.l4.commons.serialization.spi;

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

import com.google.common.collect.ImmutableSet;

import se.l4.commons.serialization.Serializer;

public class SerializerResolverChain
	implements SerializerResolver
{
	private final SerializerResolver[] resolvers;
	private final Set> hints;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public SerializerResolverChain(Collection> resolvers)
	{
		int i = 0;
		ImmutableSet.Builder> builder = ImmutableSet.builder();
		SerializerResolver[] resolverArray = new SerializerResolver[resolvers.size()];
		for(SerializerResolver r : resolvers)
		{
			resolverArray[i++] = r;
			builder.addAll(r.getHints());
		}
		
		this.hints = builder.build();
		this.resolvers = resolverArray;
	}
	
	@Override
	public Serializer find(TypeEncounter encounter)
	{
		for(SerializerResolver resolver : resolvers)
		{
			Serializer serializer = resolver.find(encounter);
			if(serializer != null) return serializer;
		}
		
		return null;
	}
	
	@Override
	public Set> getHints()
	{
		return hints;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy