7
7
import java .io .File ;
8
8
import java .io .IOException ;
9
9
import java .util .HashMap ;
10
- import java .util .HashSet ;
10
+ import java .util .Map ;
11
+ import java .util .Objects ;
12
+ import java .util .Optional ;
11
13
import java .util .Set ;
12
14
13
15
/**
14
- * Structure for loading and serving the CDS structure definitions
16
+ * Handler for loading and serving structure definitions.
15
17
*/
16
-
17
18
@ Component
18
19
public class StructureDefinitionHandler {
19
20
20
-
21
- private final HashMap <String , StructureDefinition > definitionsMap = new HashMap <>();
22
- protected ResourceReader resourceReader ;
21
+ private final Map <String , StructureDefinition > definitions = new HashMap <>();
22
+ private final ResourceReader resourceReader ;
23
23
24
24
public StructureDefinitionHandler (String fileDirectory , ResourceReader resourceReader ) {
25
25
try {
@@ -30,27 +30,20 @@ public StructureDefinitionHandler(String fileDirectory, ResourceReader resourceR
30
30
}
31
31
}
32
32
33
- /**
34
- * @return Keyset of the underlying Map managing profiles and their Structure Definitions.
35
- */
36
- public Set <String > knownProfiles () {
37
- return Set .copyOf (definitionsMap .keySet ());
38
- }
39
-
40
33
/**
41
34
* @param profile to check if known
42
35
* @return returns if profile is known.
43
36
*/
44
37
public Boolean known (String profile ) {
45
- return definitionsMap .containsKey (profile );
38
+ return definitions .containsKey (profile );
46
39
}
47
40
48
41
/**
49
42
* Reads a StructureDefinition from a file and stores it in the definitionsMap
50
43
*/
51
44
public void readStructureDefinition (String filePath ) throws IOException {
52
45
StructureDefinition structureDefinition = (StructureDefinition ) resourceReader .readResource (filePath );
53
- definitionsMap .put (structureDefinition .getUrl (), structureDefinition );
46
+ definitions .put (structureDefinition .getUrl (), structureDefinition );
54
47
}
55
48
56
49
/**
@@ -62,61 +55,37 @@ public void readStructureDefinition(String filePath) throws IOException {
62
55
*/
63
56
public StructureDefinition getDefinition (String url ) {
64
57
String [] versionSplit = url .split ("\\ |" );
65
- return definitionsMap .get (versionSplit [0 ]);
58
+ return definitions .get (versionSplit [0 ]);
66
59
}
67
60
68
61
/**
69
62
* Returns the first non-null StructureDefinition from a list of URLs.
63
+ * <p>
70
64
* Iterates over the list of URLs, returning the first valid StructureDefinition.
71
65
*
72
- * @param urls A list of URLs for which to find the corresponding StructureDefinition.
66
+ * @param urls a list of URLs for which to find the corresponding StructureDefinition.
73
67
* @return The first non-null StructureDefinition found, or empty if none are found.
74
68
*/
75
- public Set <StructureDefinition > getDefinitions (Set <String > urls ) {
76
- Set <StructureDefinition > definitions = new HashSet <>();
77
- urls .forEach (url -> {
78
- StructureDefinition def = definitionsMap .get (url );
79
- if (def != null ) {
80
- definitions .add (def );
81
- }
82
- });
83
- return definitions ;
69
+ public Optional <StructureDefinition > getDefinition (Set <String > urls ) {
70
+ return urls .stream ().map (definitions ::get ).filter (Objects ::nonNull ).findFirst ();
84
71
}
85
72
86
- /**
87
- * Returns the first non-null StructureDefinition from a list of URLs.
88
- * Iterates over the list of URLs, returning the first valid StructureDefinition.
89
- *
90
- * @param urls A list of URLs for which to find the corresponding StructureDefinition.
91
- * @return The first non-null StructureDefinition found, or null if none are found.
92
- */
93
- public Set <StructureDefinition .StructureDefinitionSnapshotComponent > getSnapshots (Set <String > urls ) {
94
- Set <StructureDefinition .StructureDefinitionSnapshotComponent > definitions = new HashSet <>();
95
- urls .forEach (url -> definitions .add (getSnapshot (url )));
96
- return definitions ;
97
- }
98
-
99
-
100
73
public StructureDefinition .StructureDefinitionSnapshotComponent getSnapshot (String url ) {
101
- if (definitionsMap .get (url ) != null ) {
102
- return (definitionsMap .get (url )).getSnapshot ();
74
+ if (definitions .get (url ) != null ) {
75
+ return (definitions .get (url )).getSnapshot ();
103
76
} else {
104
77
throw new IllegalArgumentException ("Unknown Profile: " + url );
105
78
}
106
-
107
-
108
79
}
109
80
110
81
public String getResourceType (String url ) {
111
- if (definitionsMap .get (url ) != null ) {
112
- return (definitionsMap .get (url )).getType ();
82
+ if (definitions .get (url ) != null ) {
83
+ return (definitions .get (url )).getType ();
113
84
} else {
114
85
throw new IllegalArgumentException ("Unknown Profile: " + url );
115
86
}
116
-
117
87
}
118
88
119
-
120
89
/**
121
90
* Reads all JSON files in a directory and stores their StructureDefinitions in the definitionsMap
122
91
*/
0 commit comments