Project

General

Profile

1
package eu.dnetlib.springutils.collections;
2

    
3
import java.io.IOException;
4
import java.io.StringWriter;
5
import java.util.ArrayList;
6
import java.util.Collection;
7
import java.util.List;
8
import java.util.function.Function;
9
import java.util.stream.Collectors;
10

    
11
import org.apache.commons.io.IOUtils;
12
import org.springframework.core.io.Resource;
13

    
14

    
15
/**
16
 * very useful in unit tests when you want to read many resources obtained from the spring resource loader into a
17
 * collection of strings.
18
 * 
19
 * @author marko
20
 * 
21
 */
22
public class ResourceReaderCollectionUtils {
23

    
24
	/**
25
	 * construct a ResourceReaderCollectionUtils from a collection of resources.
26
	 * @param coll
27
	 */
28
	public static Collection<String> createCollection(Collection<Resource> coll) {
29
		return coll.stream().map(resource -> {
30
			try {
31
				return IOUtils.toString(resource.getInputStream());
32
			} catch (IOException e) {
33
				throw new IllegalArgumentException("cannot read resource", e);
34
			}
35
		}).collect(Collectors.toList());
36

    
37

    
38
	}
39

    
40
}
    (1-1/1)