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

org.opencds.cqf.common.providers.InMemoryLibraryResourceProvider Maven / Gradle / Ivy

package org.opencds.cqf.common.providers;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Collection;

public class InMemoryLibraryResourceProvider implements LibraryResolutionProvider {

    private Map libraries = new HashMap<>();
    private Function getId;
    private Function getName;
    private Function getVersion;

    public InMemoryLibraryResourceProvider() {};

    public InMemoryLibraryResourceProvider(Collection initialLibraries, Function getId, Function getName, Function getVersion) {

        this.getId = getId;
        this.getName = getName;
        this.getVersion = getVersion;

        for (LibraryType library : initialLibraries) {
            this.update(library);
        }
    }

    @Override
    public LibraryType resolveLibraryById(String libraryId) {
        if (this.libraries.containsKey(libraryId)){
            return this.libraries.get(libraryId);
        }

        throw new IllegalArgumentException(String.format("Could not resolve library id %s", libraryId));
    }

    @Override
    public LibraryType resolveLibraryByName(String libraryName, String libraryVersion) {
        List libraries = this.libraries.values().stream().filter(x -> this.getName.apply(x).equals(libraryName)).collect(Collectors.toList());
        LibraryType library = LibraryResolutionProvider.selectFromList(libraries, libraryVersion, this.getVersion);

        if (library == null) {
            throw new IllegalArgumentException(String.format("Could not resolve library name %s", libraryName));
        }

        return library;
    }

    @Override
    public void update(LibraryType library) {
        this.libraries.put(this.getId.apply(library), library);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy