View Javadoc

1   /*
2    JavaRawDependency.java
3    Creation date : 19/06/2010
4    Copyright © Benjamin Croizet (graffity2199@yahoo.fr)
5   
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    or GNU Lesser General Public License as published by the
9    Free Software Foundation; either version 3 of the License,
10   or (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received copies of the GNU General Public License
18   and GNU Lesser General Public License along with this program;
19   if not, write to the Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21   http://www.fsf.org/licensing/licenses/gpl.html
22   http://www.gnu.org/licenses/lgpl.html
23   */
24  
25  package net.sourceforge.plantumldependency.cli.main.option.programminglanguage.argument.java;
26  
27  import static java.util.logging.Level.SEVERE;
28  import static java.util.logging.Logger.getLogger;
29  import static net.sourceforge.plantumldependency.cli.generic.type.impl.DependencyTypeImpl.generateDependencyFullName;
30  import static net.sourceforge.plantumldependency.common.constants.log.ErrorConstants.UNEXPECTED_ERROR;
31  import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.EQUAL;
32  
33  import java.io.Serializable;
34  import java.util.Set;
35  import java.util.TreeSet;
36  import java.util.logging.Logger;
37  
38  import net.sourceforge.plantumldependency.cli.main.option.programminglanguage.argument.java.type.JavaType;
39  import net.sourceforge.plantumldependency.common.clone.DeepCloneable;
40  
41  /**
42   * The Java raw dependency, extracted from a Java source file, without any business control nor
43   * checking.
44   *
45   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
46   * @since 1.0.0
47   * @version 1.3.0
48   */
49  public class JavaRawDependency implements Comparable < JavaRawDependency >, Serializable,
50          DeepCloneable < JavaRawDependency > {
51  
52      /** Serial version UID. */
53      private static final long serialVersionUID = -8047630217535429852L;
54  
55      /** The class logger. */
56      private static final transient Logger LOGGER = getLogger(JavaRawDependency.class.getName());
57  
58      /** The boolean telling if the java dependency is abstract or not. */
59      private boolean isAbstract;
60  
61      /** The dependency package name, such as "java.lang". */
62      private String packageName;
63  
64      /** The dependency java type, such as "class" or "interface". */
65      private JavaType type;
66  
67      /** The dependency name, such as "String". */
68      private String name;
69  
70      /** The boolean indicating if the dependency has native methods inside. */
71      private boolean nativeMethods;
72  
73      /**
74       * The {@link Set} of all dependency extensions full names (package + class), i.e. classes or
75       * interfaces the dependency "extends".
76       */
77      private Set < String > parentExtensions;
78  
79      /**
80       * The {@link Set} dependency implementations full names (package + class), i.e. interfaces the
81       * dependency "implements".
82       */
83      private Set < String > parentImplementations;
84  
85      /**
86       * Empty constructor.
87       *
88       * @since 1.0.0
89       */
90      public JavaRawDependency() {
91          super();
92      }
93  
94      /**
95       * Default constructor.
96       *
97       * @param isAbs
98       *            the boolean telling if the java dependency is abstract or not.
99       * @param dependencyPackageName
100      *            the dependency package name, such as "java.lang".
101      * @param javaType
102      *            the dependency java type, such as "class" or "interface".
103      * @param dependencyName
104      *            the dependency name, such as "String".
105      * @param extensions
106      *            the {@link Set} of all dependency extensions full names (package + class), i.e.
107      *            classes or interfaces the dependency "extends".
108      * @param implementations
109      *            the {@link Set} dependency implementations full names (package + class), i.e.
110      *            interfaces the dependency "implements".
111      * @param nativeMth
112      *            the boolean indicating if the dependency has native methods inside.
113      * @since 1.0.0
114      */
115     public JavaRawDependency(final boolean isAbs, final String dependencyPackageName, final JavaType javaType,
116             final String dependencyName, final Set < String > extensions, final Set < String > implementations,
117             final boolean nativeMth) {
118         setAbstract(isAbs);
119         setPackageName(dependencyPackageName);
120         setType(javaType);
121         setName(dependencyName);
122         setParentExtensions(extensions);
123         setParentImplementations(implementations);
124         setNativeMethods(nativeMth);
125     }
126 
127     /**
128      * {@inheritDoc}
129      *
130      * @since 1.0.0
131      */
132     @Override
133     public int compareTo(final JavaRawDependency j) {
134         int comparison = EQUAL.getResult();
135         if (this != j) {
136             comparison = packageName.compareTo(j.packageName);
137             if (comparison == EQUAL.getResult()) {
138                 comparison = name.compareTo(j.name);
139             }
140         }
141         return comparison;
142     }
143 
144     /**
145      * {@inheritDoc}
146      *
147      * @since 1.0.0
148      */
149     @Override
150     public JavaRawDependency deepClone() {
151         JavaRawDependency j = null;
152 
153         try {
154             j = (JavaRawDependency) super.clone();
155             j.parentExtensions = new TreeSet < String >(getParentExtensions());
156             j.parentImplementations = new TreeSet < String >(getParentImplementations());
157         } catch (final CloneNotSupportedException cnse) {
158             LOGGER.log(SEVERE, UNEXPECTED_ERROR, cnse);
159         }
160 
161         return j;
162     }
163 
164     /**
165      * {@inheritDoc}
166      *
167      * @since 1.0.0
168      */
169     @Override
170     public boolean equals(final Object obj) {
171         if (this == obj) {
172             return true;
173         }
174         if (obj == null) {
175             return false;
176         }
177         if (getClass() != obj.getClass()) {
178             return false;
179         }
180         final JavaRawDependency other = (JavaRawDependency) obj;
181         if (name == null) {
182             if (other.name != null) {
183                 return false;
184             }
185         } else if (!name.equals(other.name)) {
186             return false;
187         }
188         if (packageName == null) {
189             if (other.packageName != null) {
190                 return false;
191             }
192         } else if (!packageName.equals(other.packageName)) {
193             return false;
194         }
195         return true;
196     }
197 
198     /**
199      * Gets the full name of the dependency, i.e. the package name + the class name.
200      *
201      * @return the the full name of the dependency.
202      * @since 1.0.0
203      */
204     public String getFullName() {
205         return generateDependencyFullName(getPackageName(), getName());
206     }
207 
208     /**
209      * Gets the value of <code>name</code>.
210      *
211      * @return the value of <code>name</code>.
212      * @see #setName(String)
213      * @since 1.0.0
214      */
215     public String getName() {
216         return name;
217     }
218 
219     /**
220      * Gets the value of <code>packageName</code>.
221      *
222      * @return the value of <code>packageName</code>.
223      * @see #setPackageName(String)
224      * @since 1.0.0
225      */
226     public String getPackageName() {
227         return packageName;
228     }
229 
230     /**
231      * Gets the value of <code>parentExtensions</code>.
232      *
233      * @return the value of <code>parentExtensions</code>.
234      * @see #setParentExtensions(Set)
235      * @since 1.0.0
236      */
237     public Set < String > getParentExtensions() {
238         return parentExtensions;
239     }
240 
241     /**
242      * Gets the value of <code>parentImplementations</code>.
243      *
244      * @return the value of <code>parentImplementations</code>.
245      * @see #setParentImplementations(Set)
246      * @since 1.0.0
247      */
248     public Set < String > getParentImplementations() {
249         return parentImplementations;
250     }
251 
252     /**
253      * Gets the value of <code>type</code>.
254      *
255      * @return the value of <code>type</code>.
256      * @see #setType(JavaType)
257      * @since 1.0.0
258      */
259     public JavaType getType() {
260         return type;
261     }
262 
263     /**
264      * {@inheritDoc}
265      *
266      * @since 1.0.0
267      */
268     @Override
269     public int hashCode() {
270         final int prime = 31;
271         int result = 1;
272         result = prime * result + ((name == null) ? 0 : name.hashCode());
273         result = prime * result + ((packageName == null) ? 0 : packageName.hashCode());
274         return result;
275     }
276 
277     /**
278      * Gets the value of <code>nativeMethods</code>.
279      *
280      * @return the value of <code>nativeMethods</code>.
281      * @see #setNativeMethods(boolean)
282      * @since 1.0.0
283      */
284     public boolean hasNativeMethods() {
285         return nativeMethods;
286     }
287 
288     /**
289      * Gets the value of <code>isAbstract</code>.
290      *
291      * @return the value of <code>isAbstract</code>.
292      * @see #setAbstract(boolean)
293      * @since 1.0.0
294      */
295     public boolean isAbstract() {
296         return isAbstract;
297     }
298 
299     /**
300      * Sets the value of <code>isAbstract</code>.
301      *
302      * @param value
303      *            the <code>isAbstract</code> to set, can be <code>null</code>.
304      * @see #isAbstract()
305      * @since 1.0.0
306      */
307     public void setAbstract(final boolean value) {
308         isAbstract = value;
309     }
310 
311     /**
312      * Sets the value of <code>name</code>.
313      *
314      * @param value
315      *            the <code>name</code> to set, can be <code>null</code>.
316      * @see #getName()
317      * @since 1.0.0
318      */
319     public void setName(final String value) {
320         name = value;
321     }
322 
323     /**
324      * Sets the value of <code>nativeMethods</code>.
325      *
326      * @param value
327      *            the <code>nativeMethods</code> to set, can be <code>null</code>.
328      * @see #hasNativeMethods()
329      * @since 1.0.0
330      */
331     public void setNativeMethods(final boolean value) {
332         nativeMethods = value;
333     }
334 
335     /**
336      * Sets the value of <code>packageName</code>.
337      *
338      * @param value
339      *            the <code>packageName</code> to set, can be <code>null</code>.
340      * @see #getPackageName()
341      * @since 1.0.0
342      */
343     public void setPackageName(final String value) {
344         packageName = value;
345     }
346 
347     /**
348      * Sets the value of <code>parentExtensions</code>.
349      *
350      * @param value
351      *            the <code>parentExtensions</code> to set, can be <code>null</code>.
352      * @see #getParentExtensions()
353      * @since 1.0.0
354      */
355     public void setParentExtensions(final Set < String > value) {
356         parentExtensions = value;
357     }
358 
359     /**
360      * Sets the value of <code>parentImplementations</code>.
361      *
362      * @param value
363      *            the <code>parentImplementations</code> to set, can be <code>null</code>.
364      * @see #getParentImplementations()
365      * @since 1.0.0
366      */
367     public void setParentImplementations(final Set < String > value) {
368         parentImplementations = value;
369     }
370 
371     /**
372      * Sets the value of <code>type</code>.
373      *
374      * @param value
375      *            the <code>type</code> to set, can be <code>null</code>.
376      * @see #getType()
377      * @since 1.0.0
378      */
379     public void setType(final JavaType value) {
380         type = value;
381     }
382 
383     /**
384      * {@inheritDoc}
385      *
386      * @since 1.0.0
387      */
388     @Override
389     public String toString() {
390         return getClass().getSimpleName() + " [isAbstract=" + isAbstract + ", packageName=" + packageName + ", type="
391                 + type + ", name=" + name + ", nativeMethods=" + nativeMethods + ", parentExtensions="
392                 + parentExtensions + ", parentImplementations=" + parentImplementations + "]";
393     }
394 }