org.entur.netex.index.impl.VersionedNetexEntityIndexImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of netex-parser-java Show documentation
Show all versions of netex-parser-java Show documentation
Library for parsing NeTEx files and looking up entities in an index.
package org.entur.netex.index.impl;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import org.entur.netex.index.api.VersionedNetexEntityIndex;
import org.rutebanken.netex.model.EntityInVersionStructure;
import org.rutebanken.netex.model.EntityStructure;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import static org.entur.netex.support.NetexVersionHelper.latestVersionedElementIn;
import static org.entur.netex.support.NetexVersionHelper.versionOfElementIn;
public class VersionedNetexEntityIndexImpl implements VersionedNetexEntityIndex {
private final Multimap map = Multimaps.synchronizedListMultimap(ArrayListMultimap.create());
private final Map latestMap = new ConcurrentHashMap<>();
@Override
public V getLatestVersion(String id) {
return latestMap.get(id);
}
@Override
public V getVersion(String id, String version) {
return versionOfElementIn(map.get(id), version);
}
@Override
public Collection getLatestVersions() {
return latestMap.values();
}
@Override
public Collection getAllVersions(String id) {
return map.get(id);
}
@Override
public Map> getAllVersions() {
synchronized (map) {
return map.asMap();
}
}
@Override
public void put(String id, Collection entities) {
map.replaceValues(id, entities);
latestMap.put(id, latestVersionedElementIn(entities));
}
@Override
public void putAll(Collection entities) {
Map> entityMap = entities.stream()
.collect(Collectors.groupingBy(V::getId));
entityMap.forEach(map::replaceValues);
latestMap.putAll(entityMap.keySet().stream()
.map(id -> latestVersionedElementIn(map.get(id)))
.collect(Collectors.toMap(EntityStructure::getId, e -> e)));
}
@Override
public void remove(String id) {
map.removeAll(id);
latestMap.remove(id);
}
}