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

org.beanone.flattener.MapUnflattener Maven / Gradle / Ivy

Go to download

A utility library that allows one to turn a JavaBean into a flat Map of attributes

There is a newer version: 1.4
Show newest version
package org.beanone.flattener;

import static org.beanone.flattener.FlattenerContants.KEY_SUFFIX;
import static org.beanone.flattener.FlattenerContants.VALUE_SUFFIX;

import java.util.Map;

import org.beanone.flattener.api.FlattenerRegistry;
import org.beanone.flattener.api.KeyStack;

public class MapUnflattener extends AbstractUnflattener {
	private Object attributeKey;

	protected MapUnflattener(FlattenerRegistry flattenerRegistry) {
		super(flattenerRegistry);
	}

	@Override
	protected Object doCreateObject(Map flatted,
	        KeyStack keyStack, Class clazz)
	        throws InstantiationException, IllegalAccessException {
		return getUtil().createObject(clazz);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	protected void doPopulate(Object object, String key, int suffixSize,
	        Object value) throws IllegalAccessException {
		final String indexStr = getUtil().extractFieldName(key, suffixSize);
		if (indexStr.endsWith(KEY_SUFFIX)) {
			attributeKey = value;
		} else if (indexStr.endsWith(VALUE_SUFFIX)) {
			if (attributeKey == null) {
				throw new IllegalStateException(
				        "Attribute Key not yet populated! Check to make sure that the key attribute is positioned before the value attribute in the map!");
			}
			final Map map = (Map) object;
			map.put(attributeKey, value);
			attributeKey = null;
		} else {
			throw new IllegalArgumentException(
			        "Expected suffix " + KEY_SUFFIX + " or " + VALUE_SUFFIX
			                + " do not exist or not in the next position!");
		}

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy