Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
lib
Moduli
Commits
db0fd6cf
Commit
db0fd6cf
authored
Jul 21, 2018
by
Nicolai Ommer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add getModuleOpt to avoid dealing with exceptions
parent
4023ab1b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
4 deletions
+20
-4
src/main/java/edu/tigers/moduli/Moduli.java
src/main/java/edu/tigers/moduli/Moduli.java
+20
-4
No files found.
src/main/java/edu/tigers/moduli/Moduli.java
View file @
db0fd6cf
...
...
@@ -12,6 +12,7 @@ import java.util.HashMap;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Optional
;
import
org.apache.commons.configuration.ConfigurationException
;
import
org.apache.commons.configuration.HierarchicalConfiguration
;
...
...
@@ -382,15 +383,30 @@ public class Moduli
*/
@SuppressWarnings
(
"unchecked"
)
public
<
T
extends
AModule
>
T
getModule
(
Class
<
T
>
moduleId
)
{
return
getModuleOpt
(
moduleId
)
.
orElseThrow
(()
->
new
ModuleNotFoundException
(
moduleMessage
(
moduleId
,
"not found"
)));
}
/**
* Gets a module from current module-list.
*
* @param moduleId the type of the model
* @return the instance of the module for the id
*/
@SuppressWarnings
(
"unchecked"
)
public
<
T
extends
AModule
>
Optional
<
T
>
getModuleOpt
(
Class
<
T
>
moduleId
)
{
final
AModule
aModule
=
modules
.
get
(
moduleId
);
if
(
aModule
!=
null
)
{
return
(
T
)
aModule
;
return
Optional
.
of
(
(
T
)
aModule
)
;
}
return
(
T
)
modules
.
values
().
stream
()
.
filter
(
m
->
m
.
getClass
().
equals
(
moduleId
)).
findFirst
()
.
orElseThrow
(()
->
new
ModuleNotFoundException
(
moduleMessage
(
moduleId
,
"not found"
)));
return
modules
.
values
().
stream
()
.
filter
(
m
->
m
.
getClass
().
equals
(
moduleId
))
.
map
(
a
->
(
T
)
a
)
.
findFirst
();
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment