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

com.g2forge.alexandria.adt.relationship.CollectionRelationship Maven / Gradle / Ivy

There is a newer version: 0.0.18
Show newest version
package com.g2forge.alexandria.adt.relationship;

import java.util.Collection;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class CollectionRelationship> extends ARelationship {
	protected final Supplier supplier;

	public CollectionRelationship(Function get, BiConsumer set, BiConsumer setRemote, Supplier supplier) {
		super(get, set, setRemote);
		this.supplier = supplier;
	}

	public L add(L local, R remote) {
		C collection = get.apply(local);
		if (collection == null) set.accept(local, collection = supplier.get());
		if (!collection.contains(remote)) {
			collection.add(remote);
			if (setRemote != null) setRemote.accept(remote, local);
		}
		return local;
	}

	@Override
	protected boolean isInitialized(C field) {
		return (field != null) && !field.isEmpty();
	}

	@Override
	protected void setRemote(C field, L local) {
		if (setRemote != null) field.forEach(r -> setRemote.accept(r, local));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy