Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8305538

Avoid redundant HashMap.containsKey call in ModuleDescriptor.Builder

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P5 P5
    • None
    • None
    • core-libs

      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);

            aturbanov Andrey Turbanov
            aturbanov Andrey Turbanov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: