com.softicar.platform.common.container.map.index.IndexHashMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.container.map.index;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
/**
* Implementation of {@link IIndexMap} using {@link HashMap}.
*
* @author Oliver Richers
*/
public class IndexHashMap implements IIndexMap {
private final HashMap map;
public IndexHashMap() {
this(Collections.emptyList());
}
public IndexHashMap(Collection values) {
this.map = new HashMap<>();
int index = 0;
for (T column: values) {
map.put(column, index);
index++;
}
}
@Override
public int add(T value) {
var newIndex = map.size();
var oldIndex = map.putIfAbsent(value, newIndex);
return oldIndex != null? oldIndex : newIndex;
}
@Override
public Integer getIndex(T value) {
return map.get(value);
}
@Override
public Set keySet() {
return Collections.unmodifiableSet(map.keySet());
}
}