001package org.intellimate.izou.sdk.resource;
002
003import org.intellimate.izou.resource.ListResourceProvider;
004import org.intellimate.izou.resource.ResourceModel;
005
006import java.util.ArrayList;
007import java.util.Arrays;
008import java.util.List;
009import java.util.stream.Collectors;
010
011/**
012 * A ResourceContainer which holds all the Resources in an List internally
013 */
014public class ListResourceProviderImpl implements ListResourceProvider {
015    final List<ResourceModel> resources = new ArrayList<>();
016
017    /**
018     * adds a Resource to the Container
019     * @param resource an instance of the resource to add
020     */
021    @Override
022    public void addResource(ResourceModel resource) {
023        resources.add(resource);
024    }
025
026    /**
027     * adds a List of Resources to the Container
028     * @param resources a List of resources to add
029     */
030    @Override
031    public void addResource(List<ResourceModel> resources) {
032        this.resources.addAll(resources);
033    }
034
035    /**
036     * checks whether it can provide the resource
037     *
038     * @param resource the resource to provide
039     * @return true if the container can provide the resource
040     */
041    @Override
042    public boolean providesResource(ResourceModel resource) {
043        return resources.stream()
044                .map(ResourceModel::getResourceID)
045                .anyMatch(resourceS -> resourceS.equals(resource.getResourceID()));
046    }
047
048    /**
049     * checks whether there are any resources registered from the source
050     *
051     * @param sourceID the ID of the source
052     * @return true if the container has resources from the source
053     */
054    @Override
055    public boolean containsResourcesFromSource(String sourceID) {
056        return resources.stream()
057                .map(ResourceModel::getResourceID)
058                .anyMatch(source -> source.equals(sourceID));
059    }
060
061    /**
062     * checks whether the ResourceContainer can provide at least ONE resource
063     *
064     * @param resourcesIDs a list containing sources
065     * @return true if the ResourceContainer can provide at least one resource
066     */
067    @Override
068    public boolean providesResource(List<String> resourcesIDs) {
069        return resources.stream()
070                .map(ResourceModel::getResourceID)
071                .anyMatch(resourcesIDs::contains);
072    }
073
074    /**
075     * returns all EXISTING resources for the ID.
076     * If there are no resources for the ID the ID will get skipped
077     *
078     * @param resourceIDs an Array containing the resources
079     * @return a list of resources found
080     */
081    @Override
082    public List<ResourceModel> provideResource(String[] resourceIDs) {
083        return resources.stream()
084                .filter(resource -> Arrays.stream(resourceIDs)
085                        .anyMatch(resourceID -> resourceID.equals(resource.getResourceID())))
086                .collect(Collectors.toList());
087    }
088
089    /**
090     * returns the FIRST resource (if existing)
091     *
092     * @param resourceID the ID of the resource
093     * @return a list of resources found
094     */
095    @Override
096    public List<ResourceModel> provideResource(String resourceID) {
097        return resources.stream()
098                .filter(resource -> resource.getResourceID().equals(resourceID))
099                .collect(Collectors.toList());
100    }
101
102    /**
103     * returns the resource (if existing) from the source
104     *
105     * @param sourceID the ID of the source
106     * @return a list containing all the found resources
107     */
108    @Override
109    public List<ResourceModel> provideResourceFromSource(String sourceID) {
110        return resources.stream()
111                .filter(resource -> resource.getProvider().getID().equals(sourceID))
112                .collect(Collectors.toList());
113    }
114
115    @Override
116    public String toString() {
117        return "ListResourceProviderImpl{" +
118                "resources=" + resources +
119                '}';
120    }
121}