1 /*
2  * Copyright (c) 2016, 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
26 package java.lang.module;
27
28 import java.util.Objects;
29 import java.util.Set;
30
31 /**
32  * A module in a graph of <em>resolved modules</em>.
33  *
34  * <p> {@code ResolvedModule} defines the {@link #configuration configuration}
35  * method to get the configuration that the resolved module is in. It defines
36  * the {@link #reference() reference} method to get the reference to the
37  * module's content.
38  *
39  * @since 9
40  * @spec JPMS
41  * @see Configuration#modules()
42  */

43 public final class ResolvedModule {
44
45     private final Configuration cf;
46     private final ModuleReference mref;
47
48     ResolvedModule(Configuration cf, ModuleReference mref) {
49         this.cf = Objects.requireNonNull(cf);
50         this.mref = Objects.requireNonNull(mref);
51     }
52
53     /**
54      * Returns the configuration that this resolved module is in.
55      *
56      * @return The configuration that this resolved module is in
57      */

58     public Configuration configuration() {
59         return cf;
60     }
61
62     /**
63      * Returns the reference to the module's content.
64      *
65      * @return The reference to the module's content
66      */

67     public ModuleReference reference() {
68         return mref;
69     }
70
71     /**
72      * Returns the module descriptor.
73      *
74      * This convenience method is the equivalent to invoking:
75      * <pre> {@code
76      *     reference().descriptor()
77      * }</pre>
78      *
79      * @return The module descriptor
80      */

81     ModuleDescriptor descriptor() {
82         return reference().descriptor();
83     }
84
85     /**
86      * Returns the module name.
87      *
88      * This convenience method is the equivalent to invoking:
89      * <pre> {@code
90      *     reference().descriptor().name()
91      * }</pre>
92      *
93      * @return The module name
94      */

95     public String name() {
96         return reference().descriptor().name();
97     }
98
99     /**
100      * Returns the set of resolved modules that this resolved module reads.
101      *
102      * @return A possibly-empty unmodifiable set of resolved modules that
103      *         this resolved module reads
104      */

105     public Set<ResolvedModule> reads() {
106         return cf.reads(this);
107     }
108
109     /**
110      * Computes a hash code for this resolved module.
111      *
112      * <p> The hash code is based upon the components of the resolved module
113      * and satisfies the general contract of the {@link Object#hashCode
114      * Object.hashCode} method. </p>
115      *
116      * @return The hash-code value for this resolved module
117      */

118     @Override
119     public int hashCode() {
120         return cf.hashCode() ^ mref.hashCode();
121     }
122
123     /**
124      * Tests this resolved module for equality with the given object.
125      *
126      * <p> If the given object is not a {@code ResolvedModule} then this
127      * method returns {@code false}. Two {@code ResolvedModule} objects are
128      * equal if they are in the same configuration and have equal references
129      * to the module content. </p>
130      *
131      * <p> This method satisfies the general contract of the {@link
132      * java.lang.Object#equals(Object) Object.equals} method. </p>
133      *
134      * @param   ob
135      *          the object to which this object is to be compared
136      *
137      * @return  {@code trueif, and only if, the given object is a module
138      *          reference that is equal to this module reference
139      */

140     @Override
141     public boolean equals(Object ob) {
142         if (!(ob instanceof ResolvedModule))
143             return false;
144
145         ResolvedModule that = (ResolvedModule) ob;
146         return Objects.equals(this.cf, that.cf)
147                 && Objects.equals(this.mref, that.mref);
148     }
149
150     /**
151      * Returns a string describing this resolved module.
152      *
153      * @return A string describing this resolved module
154      */

155     @Override
156     public String toString() {
157         return System.identityHashCode(cf) + "/" + name();
158     }
159
160 }
161