monniasza.collects.indexar.ManyToOneIndex Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of multimachinebuilder Show documentation
Show all versions of multimachinebuilder Show documentation
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<
The newest version!
/**
*
*/
package monniasza.collects.indexar;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
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 not repeat between objects
* @author oskar
* @param type of indexed objects
* @param type of indexed properties
*/
public class ManyToOneIndex implements Index> {
/** Function which defines a property of an object */
@NN public final Function> fn;
@NN private final Map map0 = new HashMap<>();
@NN public final Map map = Collections.unmodifiableMap(map0);
/**
* Creates a m-1 index
* @param fn property to be indexed
*/
public ManyToOneIndex(Function> fn) {
this.fn = fn;
}
@Override
public boolean add(T value) {
boolean result = false;
Set set = fn.apply(value);
for(U key: set) {
result |= (map.put(key, value) != null);
}
return result;
}
@Override
public boolean test(T value) {
return !map.containsKey(fn.apply(value));
}
@Override
public boolean remove(T value) {
boolean result = false;
Set set = fn.apply(value);
for(U key: set) {
result |= (map.remove(key) != null);
}
return result;
}
@Override
public SetMultimap multimap() {
return Multimaps.forMap(map);
}
@Override
public void clear() {
map0.clear();
}
}