monniasza.collects.indexar.OneToOneIndex 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<
/**
*
*/
package monniasza.collects.indexar;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
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 single value and may not repeat between objects
* @author oskar
* @param type of indexed objects
* @param type of indexed properties
*/
public class OneToOneIndex 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 1-1 index
* @param fn property to be indexed
*/
public OneToOneIndex(Function fn) {
this.fn = fn;
}
@Override
public boolean add(T value) {
return map.putIfAbsent(fn.apply(value), value) == null;
}
@Override
public boolean test(T value) {
return !map.containsKey(fn.apply(value));
}
@Override
public boolean remove(T value) {
return map.remove(fn.apply(value)) != null;
}
@Override
public SetMultimap multimap() {
return Multimaps.forMap(map);
}
@Override
public void clear() {
map0.clear();
}
}