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

monniasza.collects.indexar.ManyToManyIndex Maven / Gradle / Ivy

Go to download

Dependency for the MultiMachineBuilder, a voxel game about building an industrial empire in a finite world. THIS RELEASE IS NOT PLAYABLE. To play the game, donwload from >ITCH.IO LINK HERE< or >GH releases link here<

There is a newer version: 0.6
Show newest version
/**
 * 
 */
package monniasza.collects.indexar;

import java.util.Set;
import java.util.function.Function;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;

import mmb.NN;

/**
 * An index, where property is a set of values and may repeat between objects
 * @param  type of indexed objects
 * @param  type of indexed properties
 * @author oskar
 */
public class ManyToManyIndex implements Index> {
	/** Function which defines a property of an object */
	@NN public final Function> fn;
	@NN private final SetMultimap multimap = HashMultimap.create();
	@NN private final SetMultimap pmultimap = Multimaps.unmodifiableSetMultimap(multimap);
	
	public ManyToManyIndex(Function> fn) {
		this.fn = fn;
	}
	
	@Override
	public boolean add(T value) {
		Set set = fn.apply(value);
		boolean result = false;
		for(U key: set) {
			result |= multimap.put(key, value);
		}
		return result;
	}

	@Override
	public boolean remove(T value) {
		Set set = fn.apply(value);
		boolean result = false;
		for(U key: set) {
			result |= multimap.remove(key, value);
		}
		return result;
	}

	@Override
	public boolean test(T value) {
		return true;
	}

	@Override
	public SetMultimap multimap() {
		return pmultimap;
	}

	@Override
	public void clear() {
		multimap.clear();
	}

}