All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.entur.netex.index.impl.VersionedNetexEntityIndexImpl Maven / Gradle / Ivy

There is a newer version: 3.1.29
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy