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

at.chrl.nutils.xml.GenericMapAdapter Maven / Gradle / Ivy

The newest version!
package at.chrl.nutils.xml;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.*;

/**
 * 
 * @author Oleh_Faizulin
 * 
 * @param 
 *            Map Key
 * @param 
 *            Map Value
 */
public class GenericMapAdapter extends XmlAdapter, Map> {

	@Override
	public KeyValuePairContainer marshal(Map v) throws Exception {
		if (v == null) {
			return null;
		}

		KeyValuePairContainer result = new KeyValuePairContainer();
		for (Map.Entry entry : v.entrySet()) {
			result.addElement(entry);
		}
		return result;
	}

	@Override
	@SuppressWarnings({ "unchecked" })
	public Map unmarshal(KeyValuePairContainer v) throws Exception {
		Map result = new HashMap();
		for (KeyValuePair kvp : v.getValues()) {
			if (kvp.getMapValue() != null) {
				result.put(kvp.getKey(), (V) kvp.getMapValue());
			} else if (kvp.getCollectionValue() != null) {
				result.put(kvp.getKey(), (V) kvp.getCollectionValue());
			} else {
				result.put(kvp.getKey(), kvp.getValue());
			}
		}
		return result;
	}

	@XmlRootElement
	@XmlAccessorType(XmlAccessType.NONE)
	public static class KeyValuePairContainer {

		@XmlElement(name = "mapEntry")
		private List> values;

		public void addElement(Map.Entry entry) {
			if (values == null) {
				values = new ArrayList>();
			}
			values.add(new KeyValuePair(entry));
		}

		public List> getValues() {
			if (values == null) {
				return Collections.emptyList();
			}
			return values;
		}
	}

	@XmlRootElement
	@XmlAccessorType(XmlAccessType.NONE)
	public static class KeyValuePair {
		public KeyValuePair() {

		}

		public KeyValuePair(Map.Entry entry) {
			this(entry.getKey(), entry.getValue());
		}

		@SuppressWarnings("rawtypes")
		public KeyValuePair(K key, V value) {
			this.key = key;

			if (value instanceof Collection) {
				this.collectionValue = (Collection) value;
			} else if (value instanceof Map) {
				this.mapValue = (Map) value;
			} else {
				this.value = value;
			}
		}

		@XmlElement
		private K key;

		@XmlElement
		private V value;

		@XmlElement
		@SuppressWarnings("rawtypes")
		private Collection collectionValue;

		@XmlElement
		@SuppressWarnings("rawtypes")
		@XmlJavaTypeAdapter(value = GenericMapAdapter.class)
		private Map mapValue;

		public K getKey() {
			return key;
		}

		public V getValue() {
			return value;
		}

		@SuppressWarnings("rawtypes")
		public Collection getCollectionValue() {
			return collectionValue;
		}

		@SuppressWarnings("rawtypes")
		public Map getMapValue() {
			return mapValue;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy