View Javadoc
1   package org.codehaus.plexus.languages.java.jpms;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Collections;
23  import java.util.LinkedHashSet;
24  import java.util.Objects;
25  import java.util.Set;
26  
27  /**
28   * Simple representation of a Module containing info required by this plugin.
29   * It will provide only methods matching Java 9 ModuleDescriptor, so once Java 9  is required, we can easily switch 
30   * 
31   * @author Robert Scholte
32   * @since 1.0.0
33   *
34   */
35  public class JavaModuleDescriptor
36  {
37      private String name;
38      
39      private boolean automatic;
40  
41      private Set<JavaRequires> requires = new LinkedHashSet<JavaRequires>();
42      
43      private Set<JavaExports> exports = new LinkedHashSet<JavaExports>();
44      
45      public String name()
46      {
47          return name;
48      }
49  
50      public boolean isAutomatic()
51      {
52          return automatic;
53      }
54      
55      public Set<JavaRequires> requires()
56      {
57          return Collections.unmodifiableSet( requires );
58      }
59  
60      public Set<JavaExports> exports()
61      {
62          return Collections.unmodifiableSet( exports );
63      }
64      
65      public static JavaModuleDescriptor.Builder newModule( String name )
66      {
67          return new Builder( name ).setAutomatic( false );
68      }
69      
70      public static Builder newAutomaticModule( String name )
71      {
72          return new Builder( name ).setAutomatic( true );
73      }  
74  
75      /**
76       * A JavaModuleDescriptor Builder
77       * 
78       * @author Robert Scholte
79       * @since 1.0.0
80       */
81      public static final class Builder
82      {
83          private JavaModuleDescriptor jModule;
84          
85          private Builder( String name )
86          {
87              jModule = new JavaModuleDescriptor();
88              jModule.name = name;
89          }
90          
91          private Builder setAutomatic( boolean isAutomatic )
92          {
93              jModule.automatic = isAutomatic;
94              return this;
95          }
96  
97          /**
98           * Adds a dependence on a module with the given (and possibly empty) set of modifiers.
99           * 
100          * @param modifiers The set of modifiers
101          * @param name The module name
102          * @return This builder
103          */
104         public Builder requires​( Set<JavaModuleDescriptor.JavaRequires.JavaModifier> modifiers, String name )
105         {
106             JavaRequires requires = new JavaRequires( modifiers, name );
107             jModule.requires.add( requires );
108             return this;
109         }
110 
111         /**
112          * Adds a dependence on a module with an empty set of modifiers.
113          * 
114          * @param name The module name
115          * @return This builder
116          */
117         public Builder requires( String name )
118         {
119             JavaRequires requires = new JavaRequires( name );
120             jModule.requires.add( requires );
121             return this;
122         }
123 
124         /**
125          * Adds an exported package. The package is exported to all modules.
126          * 
127          * @param source The package name
128          * @return This builder
129          */
130         public Builder exports( String source )
131         {
132             JavaExports exports = new JavaExports( source );
133             jModule.exports.add( exports );
134             return this;
135         }
136 
137         /**
138          * Adds an exported package. The package is exported to a set of target modules.
139          * 
140          * @param source The package name
141          * @param targets  The set of target modules names
142          * @return This builder
143          */
144         public Builder exports( String source, Set<String> targets )
145         {
146             JavaExports exports = new JavaExports( source, targets );
147             jModule.exports.add( exports );
148             return this;
149         }
150 
151         /**
152          * Builds and returns a ModuleDescriptor from its components.
153          * 
154          * @return The module descriptor
155          */
156         public JavaModuleDescriptor build()
157         {
158             return jModule;
159         }
160     }
161     
162     /**
163      * Represents Module.Requires
164      * 
165      * @author Robert Scholte
166      * @since 1.0.0
167      */
168     public static class JavaRequires
169     {
170         private final Set<JavaModifier> modifiers;
171         
172         private final String name;
173 
174         private JavaRequires( Set<JavaModifier> modifiers, String name )
175         {
176             this.modifiers = modifiers;
177             this.name = name;
178         }
179 
180         private JavaRequires( String name )
181         {
182             this.modifiers = Collections.emptySet();
183             this.name = name;
184         }
185 
186         public Set<JavaModifier> modifiers​()
187         {
188             return modifiers;
189         }
190         
191         public String name()
192         {
193             return name;
194         }
195         
196         /**
197          * Represents Module.Requires.Modifier
198          * 
199          * @author Robert Scholte
200          * @since 1.0.0
201          */
202         public static enum JavaModifier
203         {
204             STATIC
205         }
206 
207         @Override
208         public int hashCode()
209         {
210             return Objects.hash( modifiers, name );
211         }
212 
213         @Override
214         public boolean equals( Object obj )
215         {
216             if ( this == obj )
217             {
218                 return true;
219             }
220             if ( obj == null )
221             {
222                 return false;
223             }
224             if ( getClass() != obj.getClass() )
225             {
226                 return false;
227             }
228 
229             JavaRequires other = (JavaRequires) obj;
230             if ( !Objects.equals( modifiers, other.modifiers ) )
231             {
232                 return false;
233             }
234             if ( !Objects.equals( name, other.name ) )
235             {
236                 return false;
237             }
238             return true;
239         }
240     }
241     
242     /**
243      * Represents Module.Requires
244      * 
245      * @author Robert Scholte
246      * @since 1.0.0
247      */
248     public static class JavaExports
249     {
250         private final String source;
251         
252         private final Set<String> targets;
253         
254         private JavaExports( String source )
255         {
256             this.source = source;
257             this.targets = null;
258         }
259         
260         public JavaExports( String source, Set<String> targets )
261         {
262             this.source = source;
263             this.targets = targets;
264         }
265 
266         public String source()
267         {
268             return source;
269         }
270         
271         public Set<String> targets()
272         {
273             return targets;
274         }
275 
276         @Override
277         public int hashCode()
278         {
279             return Objects.hash( source, targets );
280         }
281 
282         @Override
283         public boolean equals( Object obj )
284         {
285             if ( this == obj )
286             {
287                 return true;
288             }
289             if ( obj == null )
290             {
291                 return false;
292             }
293             if ( getClass() != obj.getClass() )
294             {
295                 return false;
296             }
297             
298             JavaExports other = (JavaExports) obj;
299             if ( !Objects.equals( source, other.source ) )
300             {
301                     return false;
302             }
303             if ( !Objects.equals( targets, other.targets ) )
304             {
305                     return false;
306             }
307             return true;
308         }
309         
310         
311     }
312 }