![JAR search and dependency download from the Maven repository](/logo.png)
monniasza.collects.indexar.OneToOneIndex Maven / Gradle / Ivy
/**
*
*/
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();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy