-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleLocator.java
More file actions
89 lines (74 loc) · 2.3 KB
/
ModuleLocator.java
File metadata and controls
89 lines (74 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright (c) 2024 Christian Stein
* Licensed under the Universal Permissive License v 1.0 -> https://opensource.org/license/upl
*/
package run.bach;
import java.net.URI;
import java.util.List;
import java.util.Set;
/** Connects zero or more module names to their locations, usually uniform resource identifiers. */
@FunctionalInterface
public interface ModuleLocator {
/**
* Finds a location for a module of a given name.
*
* @param name The name of the module to locate
* @return A location a module with the given name
*/
Location locate(String name);
/** {@return the names of modules this locator can locate } */
default Set<String> names() {
return Set.of();
}
/** A nominal reference to the location of a module. */
sealed interface Location extends ModuleLocator {
static Location of(String name, String location) {
return of(name, URI.create(location));
}
static Location of(String name, URI location) {
return new Uniform(name, location);
}
static Location unknown(String name) {
return new Unknown(name);
}
record Unknown(String name) implements Location {
@Override
public Location locate(String name) {
return this;
}
}
record Uniform(String name, URI uri) implements Location {
@Override
public Set<String> names() {
return Set.of(name);
}
@Override
public Location locate(String name) {
return this.name.equals(name) ? this : new Unknown(name);
}
}
}
static ModuleLocator of(String name, String location) {
return Location.of(name, location);
}
static ModuleLocator compose(ModuleLocator... lookups) {
return new CompositeLocator(List.of(lookups));
}
record CompositeLocator(List<ModuleLocator> locators) implements ModuleLocator {
public CompositeLocator {
locators = List.copyOf(locators);
}
@Override
public Set<String> names() {
return Set.copyOf(locators.stream().flatMap(locator -> locator.names().stream()).toList());
}
@Override
public Location locate(String name) {
for (var locator : locators) {
var location = locator.locate(name);
if (!(location instanceof Location.Unknown)) return location;
}
return new Location.Unknown(name);
}
}
}