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