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

org.aksw.commons.accessors.SingleValuedAccessorFromMap Maven / Gradle / Ivy

There is a newer version: 0.9.9
Show newest version
package org.aksw.commons.accessors;

import java.util.Map;

/**
 * This accessor treats the collection as the source for a single item that can be get or set.
 * This class is intended to be with collection views.
 * 
 * new SingleValuedAccessorFromCollection(new SetFromMappedPropertyValues(...))
 * 
 * @author raven Apr 9, 2018
 *
 * @param 
 */
public class SingleValuedAccessorFromMap
	implements SingleValuedAccessor {

	protected Map map;
	protected K key;
	
	// Only clear the underlying when adding a null, but do not add the null itself
	protected boolean insertNulls;

	public SingleValuedAccessorFromMap(Map map, K key) {
		this(map, key, false);
	}

	public SingleValuedAccessorFromMap(Map map, K key, boolean insertNulls) {
		super();
		this.map = map;
		this.key = key;
		this.insertNulls = insertNulls;
	}

	@Override
	public T get() {
		T result = map.get(key);
		return result;
	}

	@Override
	public void set(T value) {
		if(value == null && ! insertNulls) {
			map.remove(key);
		} else {
			map.put(key, value);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy