org.aksw.commons.accessors.SingleValuedAccessorFromMap Maven / Gradle / Ivy
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