1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package jdk.tools.jlink.plugin;
26
27import java.nio.ByteOrder;
28import java.util.Map;
29import java.util.Optional;
30import java.util.function.Function;
31import java.util.stream.Stream;
32
33/**
34 * A Pool of Java resources.
35 */
36public interface ResourcePool {
37    /**
38     * Return the module view of this resource pool.
39     *
40     * @return a module based view of this resource pool.
41     */
42    public ResourcePoolModuleView moduleView();
43
44    /**
45     * Get all ResourcePoolEntry contained in this ResourcePool instance.
46     *
47     * @return The stream of ResourcePoolEntries.
48     */
49    public Stream<ResourcePoolEntry> entries();
50
51    /**
52     * Return the number of ResourcePoolEntry count in this ResourcePool.
53     *
54     * @return the entry count.
55     */
56    public int entryCount();
57
58    /**
59     * Get the ResourcePoolEntry for the passed path.
60     *
61     * @param path A data path
62     * @return A ResourcePoolEntry instance or null if the data is not found
63     */
64    public Optional<ResourcePoolEntry> findEntry(String path);
65
66    /**
67     * Get the ModuleEntry for the passed path restricted to supplied context.
68     *
69     * @param path A data path
70     * @param context A context of the search
71     * @return A ModuleEntry instance or null if the data is not found
72     */
73    public Optional<ResourcePoolEntry> findEntryInContext(String path, ResourcePoolEntry context);
74
75    /**
76     * Check if the ResourcePool contains the given ResourcePoolEntry.
77     *
78     * @param data The module data to check existence for.
79     * @return The module data or null if not found.
80     */
81    public boolean contains(ResourcePoolEntry data);
82
83    /**
84     * Check if the ResourcePool contains some content at all.
85     *
86     * @return True, no content, false otherwise.
87     */
88    public boolean isEmpty();
89
90    /**
91     * The ByteOrder currently in use when generating the jimage file.
92     *
93     * @return The ByteOrder.
94     */
95    public ByteOrder byteOrder();
96
97    /**
98     * Visit each ResourcePoolEntry in this ResourcePool to transform it and copy
99     * the transformed ResourcePoolEntry to the output ResourcePoolBuilder.
100     *
101     * @param transform The function called for each ResourcePoolEntry found in the
102     * ResourcePool. The transform function should return a ResourcePoolEntry
103     * instance which will be added to the output or it should return null if
104     * the passed ResourcePoolEntry is to be ignored for the output.
105     *
106     * @param outBuilder The ResourcePoolBuilder to be filled with Visitor returned
107     * ResourcePoolEntries.
108     */
109    public default void transformAndCopy(
110            Function<ResourcePoolEntry, ResourcePoolEntry> transform,
111            ResourcePoolBuilder outBuilder) {
112        entries().forEach(resource -> {
113            ResourcePoolEntry res = transform.apply(resource);
114            if (res != null) {
115                outBuilder.add(res);
116            }
117        });
118    }
119}
120