-
Enhancement
-
Resolution: Unresolved
-
P5
-
None
-
None
Map.containsKey call is sometimes unnecessary, when it's known that Map doesn't contain null values.
Instead of pair containsKey+put we can use putIfAbsent and compare result with null.
I found 4 such cases in ModuleDescriptor.Builder:
1. java.lang.module.ModuleDescriptor.Builder#requires(java.lang.module.ModuleDescriptor.Requires)
String mn = req.name();
if (name.equals(mn))
throw new IllegalArgumentException("Dependence on self");
if (requires.containsKey(mn))
throw new IllegalStateException("Dependence upon " + mn
+ " already declared");
requires.put(mn, req);
2. java.lang.module.ModuleDescriptor.Builder#exports(java.lang.module.ModuleDescriptor.Exports)
String source = e.source();
if (exports.containsKey(source)) {
throw new IllegalStateException("Exported package " + source
+ " already declared");
}
exports.put(source, e);
3. java.lang.module.ModuleDescriptor.Builder#opens(java.lang.module.ModuleDescriptor.Opens)
String source = obj.source();
if (opens.containsKey(source)) {
throw new IllegalStateException("Open package " + source
+ " already declared");
}
opens.put(source, obj);
4. java.lang.module.ModuleDescriptor.Builder#provides(java.lang.module.ModuleDescriptor.Provides)
String service = p.service();
if (provides.containsKey(service))
throw new IllegalStateException("Providers of service "
+ service + " already declared");
provides.put(service, p);
Instead of pair containsKey+put we can use putIfAbsent and compare result with null.
I found 4 such cases in ModuleDescriptor.Builder:
1. java.lang.module.ModuleDescriptor.Builder#requires(java.lang.module.ModuleDescriptor.Requires)
String mn = req.name();
if (name.equals(mn))
throw new IllegalArgumentException("Dependence on self");
if (requires.containsKey(mn))
throw new IllegalStateException("Dependence upon " + mn
+ " already declared");
requires.put(mn, req);
2. java.lang.module.ModuleDescriptor.Builder#exports(java.lang.module.ModuleDescriptor.Exports)
String source = e.source();
if (exports.containsKey(source)) {
throw new IllegalStateException("Exported package " + source
+ " already declared");
}
exports.put(source, e);
3. java.lang.module.ModuleDescriptor.Builder#opens(java.lang.module.ModuleDescriptor.Opens)
String source = obj.source();
if (opens.containsKey(source)) {
throw new IllegalStateException("Open package " + source
+ " already declared");
}
opens.put(source, obj);
4. java.lang.module.ModuleDescriptor.Builder#provides(java.lang.module.ModuleDescriptor.Provides)
String service = p.service();
if (provides.containsKey(service))
throw new IllegalStateException("Providers of service "
+ service + " already declared");
provides.put(service, p);
- links to
-
Review openjdk/jdk/13288