1 /*
2  * Copyright (c) 2015, 2020, 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  */
25 package jdk.tools.jlink.plugin;
26 
27 import java.nio.ByteOrder;
28 import java.util.Optional;
29 import java.util.function.Function;
30 import java.util.stream.Stream;
31 
32 /**
33  * A Pool of Java resources.
34  */
35 public interface ResourcePool {
36     /**
37      * Return the module view of this resource pool.
38      *
39      * @return a module based view of this resource pool.
40      */
moduleView()41     public ResourcePoolModuleView moduleView();
42 
43     /**
44      * Get all ResourcePoolEntry contained in this ResourcePool instance.
45      *
46      * @return The stream of ResourcePoolEntries.
47      */
entries()48     public Stream<ResourcePoolEntry> entries();
49 
50     /**
51      * Return the number of ResourcePoolEntry count in this ResourcePool.
52      *
53      * @return the entry count.
54      */
entryCount()55     public int entryCount();
56 
57     /**
58      * Get the ResourcePoolEntry for the passed path.
59      *
60      * @param path A data path
61      * @return A ResourcePoolEntry instance or null if the data is not found
62      */
findEntry(String path)63     public Optional<ResourcePoolEntry> findEntry(String path);
64 
65     /**
66      * Get the ModuleEntry for the passed path restricted to supplied context.
67      *
68      * @param path A data path
69      * @param context A context of the search
70      * @return A ModuleEntry instance or null if the data is not found
71      */
findEntryInContext(String path, ResourcePoolEntry context)72     public Optional<ResourcePoolEntry> findEntryInContext(String path, ResourcePoolEntry context);
73 
74     /**
75      * Check if the ResourcePool contains the given ResourcePoolEntry.
76      *
77      * @param data The module data to check existence for.
78      * @return The module data or null if not found.
79      */
contains(ResourcePoolEntry data)80     public boolean contains(ResourcePoolEntry data);
81 
82     /**
83      * Check if the ResourcePool contains some content at all.
84      *
85      * @return True, no content, false otherwise.
86      */
isEmpty()87     public boolean isEmpty();
88 
89     /**
90      * The ByteOrder currently in use when generating the jimage file.
91      *
92      * @return The ByteOrder.
93      */
byteOrder()94     public ByteOrder byteOrder();
95 
96     /**
97      * Visit each ResourcePoolEntry in this ResourcePool to transform it and copy
98      * the transformed ResourcePoolEntry to the output ResourcePoolBuilder.
99      *
100      * @param transform The function called for each ResourcePoolEntry found in the
101      * ResourcePool. The transform function should return a ResourcePoolEntry
102      * instance which will be added to the output or it should return null if
103      * the passed ResourcePoolEntry is to be ignored for the output.
104      *
105      * @param outBuilder The ResourcePoolBuilder to be filled with Visitor returned
106      * ResourcePoolEntries.
107      */
transformAndCopy( Function<ResourcePoolEntry, ResourcePoolEntry> transform, ResourcePoolBuilder outBuilder)108     public default void transformAndCopy(
109             Function<ResourcePoolEntry, ResourcePoolEntry> transform,
110             ResourcePoolBuilder outBuilder) {
111         entries().forEach(resource -> {
112             ResourcePoolEntry res = transform.apply(resource);
113             if (res != null) {
114                 outBuilder.add(res);
115             }
116         });
117     }
118 }
119