
monniasza.collects.indexar.ManyToOneIndex Maven / Gradle / Ivy
/**
*
*/
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();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy