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.Set;
25  
26  /**
27   * Simple representation of a Module containing info required by this plugin.
28   * It will provide only methods matching Java 9 ModuleDescriptor, so once Java 9  is required, we can easily switch 
29   * 
30   * @author Robert Scholte
31   * @since 1.0.0
32   *
33   */
34  public class JavaModuleDescriptor
35  {
36      private String name;
37      
38      private boolean automatic;
39  
40      private Set<JavaRequires> requires = new LinkedHashSet<JavaRequires>();
41      
42      private Set<JavaExports> exports = new LinkedHashSet<JavaExports>();
43      
44      public String name()
45      {
46          return name;
47      }
48  
49      public boolean isAutomatic()
50      {
51          return automatic;
52      }
53      
54      public Set<JavaRequires> requires()
55      {
56          return Collections.unmodifiableSet( requires );
57      }
58  
59      public Set<JavaExports> exports()
60      {
61          return Collections.unmodifiableSet( exports );
62      }
63      
64      public static JavaModuleDescriptor.Builder newModule( String name )
65      {
66          return new Builder( name ).setAutomatic( false );
67      }
68      
69      public static Builder newAutomaticModule( String name )
70      {
71          return new Builder( name ).setAutomatic( true );
72      }  
73  
74      /**
75       * A JavaModuleDescriptor Builder
76       * 
77       * @author Robert Scholte
78       * @since 1.0.0
79       */
80      public static final class Builder
81      {
82          private JavaModuleDescriptor jModule;
83          
84          private Builder( String name )
85          {
86              jModule = new JavaModuleDescriptor();
87              jModule.name = name;
88          }
89          
90          private Builder setAutomatic( boolean isAutomatic )
91          {
92              jModule.automatic = isAutomatic;
93              return this;
94          }
95  
96          public Builder requires( String name )
97          {
98              JavaRequires requires = new JavaRequires( name );
99              jModule.requires.add( requires );
100             return this;
101         }
102 
103         public Builder exports( String source )
104         {
105             JavaExports exports = new JavaExports( source );
106             jModule.exports.add( exports );
107             return this;
108         }
109 
110         public Builder exports( String source, Set<String> targets )
111         {
112             JavaExports exports = new JavaExports( source, targets );
113             jModule.exports.add( exports );
114             return this;
115         }
116 
117         public JavaModuleDescriptor build()
118         {
119             return jModule;
120         }
121     }
122     
123     /**
124      * Represents Module.Requires
125      * 
126      * @author Robert Scholte
127      * @since 1.0.0
128      */
129     public static class JavaRequires
130     {
131         private final String name;
132 
133         private JavaRequires( String name )
134         {
135             this.name = name;
136         }
137 
138         public String name()
139         {
140             return name;
141         }
142     }
143     
144     /**
145      * Represents Module.Requires
146      * 
147      * @author Robert Scholte
148      * @since 1.0.0
149      */
150     public static class JavaExports
151     {
152         private final String source;
153         
154         private final Set<String> targets;
155         
156         private JavaExports( String source )
157         {
158             this.source = source;
159             this.targets = null;
160         }
161         
162         public JavaExports( String source, Set<String> targets )
163         {
164             this.source = source;
165             this.targets = targets;
166         }
167 
168         public String source()
169         {
170             return source;
171         }
172         
173         public Set<String> targets()
174         {
175             return targets;
176         }
177     }
178 }