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

de.intarsys.tools.reflect.MapRelationHandler Maven / Gradle / Ivy

There is a newer version: 4.11
Show newest version
/**
 * 
 */
package de.intarsys.tools.reflect;

import java.util.Map;


/**
 * The relation is implemented in a {@link Map}.
 * 
 */
public class MapRelationHandler extends RelationHandlerAdapter {

	@Override
	public Object[] get(Object owner) {
		if (owner == null) {
			return new Object[0];
		}
		return ((Map) owner).entrySet().toArray();
	}

	@Override
	public Object insert(Object owner, Object value) {
		if (owner == null) {
			return null;
		}
		Map.Entry entry = (Map.Entry) value;
		((Map) owner).put(entry.getKey(), entry.getValue());
		// this is not truly the object inserted...
		return entry;
	}

	@Override
	public Object remove(Object owner, Object value) {
		if (owner == null) {
			return null;
		}
		Map.Entry entry = (Map.Entry) value;
		((Map) owner).remove(entry.getKey());
		return entry;
	}

	@Override
	public Object update(Object owner, Object value, Object newValue) {
		if (owner == null) {
			return null;
		}
		Map.Entry entry = (Map.Entry) value;
		Map.Entry newEntry = (Map.Entry) newValue;
		if (entry.getKey().equals(newEntry.getKey())) {
			entry.setValue(newEntry.getValue());
			return entry;
		} else {
			((Map) owner).remove(entry.getKey());
			((Map) owner).put(newEntry.getKey(), newEntry.getValue());
			return newEntry;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy