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