
org.aksw.commons.accessors.SingleValuedAccessorFromCollection Maven / Gradle / Ivy
package org.aksw.commons.accessors;
import java.util.Collection;
/**
* 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 SingleValuedAccessorFromCollection
implements SingleValuedAccessor {
protected Collection collection;
// Only clear the underlying when adding a null, but do not add the null itself
protected boolean insertNulls;
public SingleValuedAccessorFromCollection(Collection collection) {
this(collection, false);
}
public SingleValuedAccessorFromCollection(Collection set, boolean insertNulls) {
super();
this.collection = set;
this.insertNulls = insertNulls;
}
@Override
public T get() {
T result = collection.isEmpty() ? null : collection.iterator().next();
return result;
}
@Override
public void set(T value) {
collection.clear();
if(value != null || insertNulls) {
collection.add(value);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy