View Javadoc
1   /*-
2    * #%L
3    * io.earcam.instrumental.lade.jpms
4    * %%
5    * Copyright (C) 2018 earcam
6    * %%
7    * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
8    * 
9    * You <b>must</b> choose to accept, in full - any individual or combination of 
10   * the following licenses:
11   * <ul>
12   * 	<li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li>
13   * 	<li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li>
14   * 	<li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li>
15   * 	<li><a href="https://opensource.org/licenses/MIT">MIT</a></li>
16   * </ul>
17   * #L%
18   */
19  package io.earcam.instrumental.lade.jpms;
20  
21  import java.io.IOException;
22  import java.lang.module.ModuleDescriptor;
23  import java.lang.module.ModuleReader;
24  import java.lang.module.ModuleReference;
25  import java.net.URI;
26  import java.util.Optional;
27  import java.util.Set;
28  import java.util.stream.Stream;
29  
30  import io.earcam.unexceptional.Exceptional;
31  
32  final class InMemoryModuleReference extends ModuleReference {
33  
34  	private class InMemoryModuleReader implements ModuleReader {
35  		@Override
36  		public Stream<String> list() throws IOException
37  		{
38  			return list.stream();
39  		}
40  
41  
42  		@Override
43  		public Optional<URI> find(String name) throws IOException
44  		{
45  			return list()
46  					.filter(r -> r.startsWith(location().get().toString()))
47  					.map(Exceptional::uri)
48  					.filter(r -> r.toString().endsWith(name))
49  					.findAny();
50  		}
51  
52  
53  		@Override
54  		public void close()
55  		{
56  			/* NoOp */
57  		}
58  	}
59  
60  	private final Set<String> list;
61  
62  
63  	InMemoryModuleReference(ModuleDescriptor descriptor, URI location, Set<String> list)
64  	{
65  		super(descriptor, location);
66  		this.list = list;
67  	}
68  
69  
70  	@Override
71  	public ModuleReader open() throws IOException
72  	{
73  		return new InMemoryModuleReader();
74  	}
75  }