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  * @see Configuration#modules()
41  */
42 public final class ResolvedModule {
43 
44     private final Configuration cf;
45     private final ModuleReference mref;
46 
ResolvedModule(Configuration cf, ModuleReference mref)47     ResolvedModule(Configuration cf, ModuleReference mref) {
48         this.cf = Objects.requireNonNull(cf);
49         this.mref = Objects.requireNonNull(mref);
50     }
51 
52     /**
53      * Returns the configuration that this resolved module is in.
54      *
55      * @return The configuration that this resolved module is in
56      */
configuration()57     public Configuration configuration() {
58         return cf;
59     }
60 
61     /**
62      * Returns the reference to the module's content.
63      *
64      * @return The reference to the module's content
65      */
reference()66     public ModuleReference reference() {
67         return mref;
68     }
69 
70     /**
71      * Returns the module descriptor.
72      *
73      * This convenience method is the equivalent to invoking:
74      * <pre> {@code
75      *     reference().descriptor()
76      * }</pre>
77      *
78      * @return The module descriptor
79      */
descriptor()80     ModuleDescriptor descriptor() {
81         return mref.descriptor();
82     }
83 
84     /**
85      * Returns the module name.
86      *
87      * This convenience method is the equivalent to invoking:
88      * <pre> {@code
89      *     reference().descriptor().name()
90      * }</pre>
91      *
92      * @return The module name
93      */
name()94     public String name() {
95         return mref.descriptor().name();
96     }
97 
98     /**
99      * Returns the set of resolved modules that this resolved module reads.
100      *
101      * @return A possibly-empty unmodifiable set of resolved modules that
102      *         this resolved module reads
103      */
reads()104     public Set<ResolvedModule> reads() {
105         return cf.reads(this);
106     }
107 
108     /**
109      * Computes a hash code for this resolved module.
110      *
111      * <p> The hash code is based upon the components of the resolved module
112      * and satisfies the general contract of the {@link Object#hashCode
113      * Object.hashCode} method. </p>
114      *
115      * @return The hash-code value for this resolved module
116      */
117     @Override
hashCode()118     public int hashCode() {
119         return cf.hashCode() ^ mref.hashCode();
120     }
121 
122     /**
123      * Tests this resolved module for equality with the given object.
124      *
125      * <p> If the given object is not a {@code ResolvedModule} then this
126      * method returns {@code false}. Two {@code ResolvedModule} objects are
127      * equal if they are in the same configuration and have equal references
128      * to the module content. </p>
129      *
130      * <p> This method satisfies the general contract of the {@link
131      * java.lang.Object#equals(Object) Object.equals} method. </p>
132      *
133      * @param   ob
134      *          the object to which this object is to be compared
135      *
136      * @return  {@code true} if, and only if, the given object is a module
137      *          reference that is equal to this module reference
138      */
139     @Override
equals(Object ob)140     public boolean equals(Object ob) {
141         if (!(ob instanceof ResolvedModule))
142             return false;
143 
144         ResolvedModule that = (ResolvedModule) ob;
145         return Objects.equals(this.cf, that.cf)
146                 && Objects.equals(this.mref, that.mref);
147     }
148 
149     /**
150      * Returns a string describing this resolved module.
151      *
152      * @return A string describing this resolved module
153      */
154     @Override
toString()155     public String toString() {
156         return System.identityHashCode(cf) + "/" + name();
157     }
158 
159 }
160