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

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

There is a newer version: 0.9.9
Show newest version
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