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

com.jdroid.java.marshaller.MarshallerProvider Maven / Gradle / Ivy

The newest version!
package com.jdroid.java.marshaller;

import com.jdroid.java.collections.Lists;
import com.jdroid.java.collections.Maps;
import com.jdroid.java.json.JSONArray;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public class MarshallerProvider {
	
	private Map, Marshaller> marshallers = Maps.newHashMap();
	private Marshaller defaultMarshaller = new DefaultMarshaller();
	private MarshallerTypeEvaluator marshallerTypeEvaluator = new DefaultMarshallerTypeEvaluator();
	
	private static final MarshallerProvider INSTANCE = new MarshallerProvider();
	
	private MarshallerProvider() {
		// singleton
	}
	
	/**
	 * @return The instance of this provider
	 */
	public static MarshallerProvider get() {
		return MarshallerProvider.INSTANCE;
	}
	
	public Object marshall(Object object, MarshallerMode mode, Map extras) {
		if (object != null) {
			if (object instanceof Collection) {
				Collection collection = (Collection)object;
				List list = Lists.newArrayList();
				
				for (Object each : collection) {
					list.add(innerMarshall(each, mode, extras));
				}
				return new JSONArray(list);
			} else {
				return innerMarshall(object, mode, extras);
			}
		} else {
			return null;
		}
	}
	
	public Object innerMarshall(Object object, MarshallerMode mode, Map extras) {
		if (object != null) {
			if (object instanceof Collection) {
				Collection collection = (Collection)object;
				List list = Lists.newArrayList();
				
				for (Object each : collection) {
					list.add(innerMarshall(each, mode, extras));
				}
				return list;
			} else if (object instanceof Map) {
				Map map = (Map)object;
				if (map.isEmpty()) {
					return null;
				} else {
					Map marshalledMap = Maps.newHashMap();
					for (Map.Entry entry : map.entrySet()) {
						marshalledMap.put(entry.getKey(), innerMarshall(entry.getValue(), mode, extras));
					}
					return marshalledMap;
				}
			} else {
				return getMarshaller(object).marshall(object, mode, extras);
			}
		} else {
			return null;
		}
	}
	
	public Marshaller getMarshaller(Object marshallerType) {
		Class clazz = marshallerTypeEvaluator.evaluate(marshallerType);
		return marshallers.containsKey(clazz) ? marshallers.get(clazz) : defaultMarshaller;
	}
	
	public void setMarshallers(Map, Marshaller> marshallers) {
		this.marshallers = marshallers;
	}
	
	@SuppressWarnings("unchecked")
	public void addMarshaller(Class clazz, Marshaller marshaller) {
		marshallers.put(clazz, (Marshaller)marshaller);
	}
	
	public static interface MarshallerTypeEvaluator {
		
		public Class evaluate(Object marshallerType);
	}
	
	private class DefaultMarshallerTypeEvaluator implements MarshallerTypeEvaluator {
		
		@Override
		public Class evaluate(Object marshallerType) {
			return marshallerType.getClass();
		}
	}
	
	public void setMarshallerTypeEvaluator(MarshallerTypeEvaluator marshallerTypeEvaluator) {
		this.marshallerTypeEvaluator = marshallerTypeEvaluator;
	}
	
	public void setDefaultMarshaller(Marshaller defaultMarshaller) {
		this.defaultMarshaller = defaultMarshaller;
	}
}