// Path: backport/src/java/util/stream/Collectors.java
// public class Collectors {
//   public static <T, K, V> Collector<T, ?, Map<K,V>> toMap(Function<? super T, ? extends K> keyMapper,
//                                                           Function<? super T, ? extends V> valueMapper) {
//     return stream -> {
//       HashMap<K, V> map = new HashMap<>();
//       stream.forEach(element -> {
//         map.put(keyMapper.apply(element), valueMapper.apply(element));
//       });
//       return map;
//     };
//   }
// 
//   public static <T> Collector<T, ?, Set<T>> toSet() {
//     return toCollection(HashSet::new);
//   }
//   
//   public static <T> Collector<T, ?, List<T>> toList() {
//     return toCollection(ArrayList::new);
//   }
//   
//   private static <T, C extends Collection<T>> Collector<T, ?, C> toCollection(Supplier<C> collectionFactory) {
//     return stream -> {
//       C collection = collectionFactory.get();
//       stream.forEach(collection::add);
//       return collection;
//     };
//   }
// }
// 
// Path: src/jdk/internal/jimage/PackageModuleMap.java
// public final class PackageModuleMap {
//     private PackageModuleMap() {}
// 
//     public static final String MODULES_ENTRY = "module/modules.offsets";
//     public static final String PACKAGES_ENTRY = "packages.offsets";
// 
//     /*
//      * Returns a package-to-module map.
//      *
//      * The package name is in binary name format.
//      */
//     static Map<String,String> readFrom(ImageReader reader) throws IOException {
//         Map<String,String> result = new HashMap<>();
//         List<String> moduleNames = reader.getNames(MODULES_ENTRY);
// 
//         for (String moduleName : moduleNames) {
//             List<String> packageNames = reader.getNames(moduleName + "/" + PACKAGES_ENTRY);
// 
//             for (String packageName : packageNames) {
//                 result.put(packageName, moduleName);
//             }
//         }
//         return result;
//     }
// }


import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static jdk.internal.jimage.PackageModuleMap.*;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
            this.modules = Collections.unmodifiableSet(modules);
        }

        Set<String> modules() {
            return modules;
        }
        Loader loader() { return loader; }
    }

    ModuleIndex buildModuleIndex(Loader type, BasicImageWriter writer) {
        return new ModuleIndex(getModules(type), writer);
    }

    /*
     * Generate module name table and the package map as resources
     * in the modular image
     */
    public class ModuleIndex {
        final Map<String, Integer> moduleOffsets = new LinkedHashMap<>();
        final Map<String, List<Integer>> packageOffsets = new HashMap<>();
        final int size;
        public ModuleIndex(Set<String> mods, BasicImageWriter writer) {
            // module name offsets
            writer.addLocation(MODULES_ENTRY, 0, 0, mods.size() * 4);
            long offset = mods.size() * 4;
            for (String mn : mods) {
                moduleOffsets.put(mn, writer.addString(mn));
                List<Integer> poffsets = localPkgs.get(mn).stream()
                        .map(pn -> pn.replace('.', '/'))
                        .map(writer::addString)
// Path: backport/src/java/util/stream/Collectors.java
// public class Collectors {
//   public static <T, K, V> Collector<T, ?, Map<K,V>> toMap(Function<? super T, ? extends K> keyMapper,
//                                                           Function<? super T, ? extends V> valueMapper) {
//     return stream -> {
//       HashMap<K, V> map = new HashMap<>();
//       stream.forEach(element -> {
//         map.put(keyMapper.apply(element), valueMapper.apply(element));
//       });
//       return map;
//     };
//   }
// 
//   public static <T> Collector<T, ?, Set<T>> toSet() {
//     return toCollection(HashSet::new);
//   }
//   
//   public static <T> Collector<T, ?, List<T>> toList() {
//     return toCollection(ArrayList::new);
//   }
//   
//   private static <T, C extends Collection<T>> Collector<T, ?, C> toCollection(Supplier<C> collectionFactory) {
//     return stream -> {
//       C collection = collectionFactory.get();
//       stream.forEach(collection::add);
//       return collection;
//     };
//   }
// }
// 
// Path: src/jdk/internal/jimage/PackageModuleMap.java
// public final class PackageModuleMap {
//     private PackageModuleMap() {}
// 
//     public static final String MODULES_ENTRY = "module/modules.offsets";
//     public static final String PACKAGES_ENTRY = "packages.offsets";
// 
//     /*
//      * Returns a package-to-module map.
//      *
//      * The package name is in binary name format.
//      */
//     static Map<String,String> readFrom(ImageReader reader) throws IOException {
//         Map<String,String> result = new HashMap<>();
//         List<String> moduleNames = reader.getNames(MODULES_ENTRY);
// 
//         for (String moduleName : moduleNames) {
//             List<String> packageNames = reader.getNames(moduleName + "/" + PACKAGES_ENTRY);
// 
//             for (String packageName : packageNames) {
//                 result.put(packageName, moduleName);
//             }
//         }
//         return result;
//     }
// }


// Path: src/jdk/internal/jimage/ImageModules.java
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static jdk.internal.jimage.PackageModuleMap.*;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;

            this.modules = Collections.unmodifiableSet(modules);
        }

        Set<String> modules() {
            return modules;
        }
        Loader loader() { return loader; }
    }

    ModuleIndex buildModuleIndex(Loader type, BasicImageWriter writer) {
        return new ModuleIndex(getModules(type), writer);
    }

    /*
     * Generate module name table and the package map as resources
     * in the modular image
     */
    public class ModuleIndex {
        final Map<String, Integer> moduleOffsets = new LinkedHashMap<>();
        final Map<String, List<Integer>> packageOffsets = new HashMap<>();
        final int size;
        public ModuleIndex(Set<String> mods, BasicImageWriter writer) {
            // module name offsets
            writer.addLocation(MODULES_ENTRY, 0, 0, mods.size() * 4);
            long offset = mods.size() * 4;
            for (String mn : mods) {
                moduleOffsets.put(mn, writer.addString(mn));
                List<Integer> poffsets = localPkgs.get(mn).stream()
                        .map(pn -> pn.replace('.', '/'))
                        .map(writer::addString)
                        .collect(Collectors.toList());