< prev index next >

src/java.base/share/classes/jdk/internal/module/ModuleHashes.java

Print this page
rev 16336 : 8171373: Reduce copying during initialization of ModuleHashes
Reviewed-by: alanb, mchung, chegar


 132      * path to the entry (module artifact).
 133      *
 134      * @return ModuleHashes that encapsulates the hashes
 135      */
 136     public static ModuleHashes generate(Map<String, Path> map, String algorithm) {
 137         Map<String, byte[]> nameToHash = new HashMap<>();
 138         for (Map.Entry<String, Path> entry: map.entrySet()) {
 139             String name = entry.getKey();
 140             Path path = entry.getValue();
 141             nameToHash.put(name, computeHash(path, algorithm));
 142         }
 143         return new ModuleHashes(algorithm, nameToHash);
 144     }
 145 
 146     /**
 147      * This is used by jdk.internal.module.SystemModules class
 148      * generated at link time.
 149      */
 150     public static class Builder {
 151         final String algorithm;
 152         Map<String, byte[]> nameToHash;
 153 
 154         Builder(String algorithm) {

 155             this.algorithm =  Objects.requireNonNull(algorithm);
 156         }
 157 
 158         /**
 159          * Sets the module hash for the given module name
 160          */
 161         public Builder hashForModule(String mn, byte[] hash) {
 162             if (nameToHash == null)
 163                 nameToHash = new HashMap<>();
 164 
 165             nameToHash.put(mn, hash);
 166             return this;
 167         }
 168 
 169         /**
 170          * Builds a {@code ModuleHashes}.
 171          */
 172         public ModuleHashes build() {
 173             if (nameToHash != null) {
 174                 return new ModuleHashes(algorithm, nameToHash);
 175             } else {
 176                 return null;
 177             }
 178         }
 179     }
 180 }


 132      * path to the entry (module artifact).
 133      *
 134      * @return ModuleHashes that encapsulates the hashes
 135      */
 136     public static ModuleHashes generate(Map<String, Path> map, String algorithm) {
 137         Map<String, byte[]> nameToHash = new HashMap<>();
 138         for (Map.Entry<String, Path> entry: map.entrySet()) {
 139             String name = entry.getKey();
 140             Path path = entry.getValue();
 141             nameToHash.put(name, computeHash(path, algorithm));
 142         }
 143         return new ModuleHashes(algorithm, nameToHash);
 144     }
 145 
 146     /**
 147      * This is used by jdk.internal.module.SystemModules class
 148      * generated at link time.
 149      */
 150     public static class Builder {
 151         final String algorithm;
 152         final Map<String, byte[]> nameToHash;
 153 
 154         Builder(String algorithm, int initialCapacity) {
 155             this.nameToHash = new HashMap<>(initialCapacity);
 156             this.algorithm =  Objects.requireNonNull(algorithm);
 157         }
 158 
 159         /**
 160          * Sets the module hash for the given module name
 161          */
 162         public Builder hashForModule(String mn, byte[] hash) {



 163             nameToHash.put(mn, hash);
 164             return this;
 165         }
 166 
 167         /**
 168          * Builds a {@code ModuleHashes}.
 169          */
 170         public ModuleHashes build() {
 171             if (!nameToHash.isEmpty()) {
 172                 return new ModuleHashes(algorithm, nameToHash);
 173             } else {
 174                 return null;
 175             }
 176         }
 177     }
 178 }
< prev index next >