View Javadoc

1   /*
2    GenericDependencyImpl.java
3    Creation date : 20/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.generic.impl;
26  
27  import static java.util.logging.Level.SEVERE;
28  import static java.util.logging.Logger.getLogger;
29  import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.DEPENDENCY_TYPE_NULL_ERROR;
30  import static net.sourceforge.plantumldependency.common.constants.log.ErrorConstants.UNEXPECTED_ERROR;
31  import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
32  import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.AFTER;
33  import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.EQUAL;
34  
35  import java.util.logging.Logger;
36  
37  import net.sourceforge.plantumldependency.cli.generic.GenericDependency;
38  import net.sourceforge.plantumldependency.cli.generic.type.DependencyType;
39  import net.sourceforge.plantumldependency.cli.generic.type.impl.stubimpl.StubDependencyTypeImpl;
40  
41  /**
42   * The default implementation of the
43   * {@link net.sourceforge.plantumldependency.cli.generic.GenericDependency} interface.
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 GenericDependencyImpl implements GenericDependency {
50  
51      /** Serial version UID. */
52      private static final long serialVersionUID = 4512036274880797908L;
53  
54      /** The class logger. */
55      private static final transient Logger LOGGER = getLogger(GenericDependencyImpl.class.getName());
56  
57      /** The underlying dependency type, which determines the real nature of the dependency. */
58      private DependencyType dependencyType;
59  
60      /**
61       * Default constructor, with a dependency type. This constructor is usually used when the
62       * concrete dependency type (class, interface etc..) is already known.
63       *
64       * @param type
65       *            the underlying dependency type, which determines the real nature of the
66       *            dependency, mustn't be <code>null</code>.
67       * @since 1.0.0
68       */
69      public GenericDependencyImpl(final DependencyType type) {
70          checkNull(type, DEPENDENCY_TYPE_NULL_ERROR);
71  
72          dependencyType = type;
73      }
74  
75      /**
76       * Stub constructor. This constructor is usually used when the concrete dependency type (class,
77       * interface etc..) is not known when created the dependency. In this case, the
78       * {@link StubDependencyTypeImpl} is used instead.
79       *
80       * @param dependencyName
81       *            the dependency name, usually the class name, mustn't be <code>null</code> nor
82       *            empty.
83       * @param dependencyPackageName
84       *            the dependency package name, mustn't be <code>null</code> nor empty.
85       * @since 1.0.0
86       */
87      public GenericDependencyImpl(final String dependencyName, final String dependencyPackageName) {
88          this(new StubDependencyTypeImpl(dependencyName, dependencyPackageName));
89      }
90  
91      /**
92       * {@inheritDoc}
93       *
94       * @since 1.0.0
95       */
96      @Override
97      public int compareTo(final GenericDependency a) {
98          int comparison = AFTER.getResult();
99  
100         if (this == a) {
101             comparison = EQUAL.getResult();
102         } else {
103             comparison = getDependencyType().compareTo(a.getDependencyType());
104         }
105 
106         return comparison;
107     }
108 
109     /**
110      * {@inheritDoc}
111      *
112      * @since 1.0.0
113      */
114     @Override
115     public GenericDependency deepClone() {
116         GenericDependencyImpl g = null;
117 
118         try {
119             g = (GenericDependencyImpl) super.clone();
120             g.dependencyType = getDependencyType().deepClone();
121         } catch (final CloneNotSupportedException cnse) {
122             LOGGER.log(SEVERE, UNEXPECTED_ERROR, cnse);
123         }
124 
125         return g;
126     }
127 
128     /**
129      * {@inheritDoc}
130      *
131      * @since 1.0.0
132      */
133     @Override
134     public boolean equals(final Object obj) {
135         if (this == obj) {
136             return true;
137         }
138         if (obj == null) {
139             return false;
140         }
141         if (getClass() != obj.getClass()) {
142             return false;
143         }
144         final GenericDependencyImpl other = (GenericDependencyImpl) obj;
145         if (dependencyType == null) {
146             if (other.dependencyType != null) {
147                 return false;
148             }
149         } else if (!dependencyType.equals(other.dependencyType)) {
150             return false;
151         }
152         return true;
153     }
154 
155     /**
156      * {@inheritDoc}
157      *
158      * @since 1.0.0
159      */
160     @Override
161     public DependencyType getDependencyType() {
162         return dependencyType;
163     }
164 
165     /**
166      * {@inheritDoc}
167      *
168      * @since 1.0.0
169      */
170     @Override
171     public String getFullName() {
172         return getDependencyType().getFullName();
173     }
174 
175     /**
176      * {@inheritDoc}
177      *
178      * @since 1.0.0
179      */
180     @Override
181     public String getName() {
182         return getDependencyType().getName();
183     }
184 
185     /**
186      * {@inheritDoc}
187      *
188      * @since 1.0.0
189      */
190     @Override
191     public String getPackageName() {
192         return getDependencyType().getPackageName();
193     }
194 
195     /**
196      * {@inheritDoc}
197      *
198      * @since 1.0.0
199      */
200     @Override
201     public int hashCode() {
202         final int prime = 31;
203         int result = 1;
204         result = prime * result + ((dependencyType == null) ? 0 : dependencyType.hashCode());
205         return result;
206     }
207 
208     /**
209      * {@inheritDoc}
210      *
211      * @since 1.0.0
212      */
213     @Override
214     public void setDependencyType(final DependencyType value) {
215         dependencyType = value;
216     }
217 
218     /**
219      * {@inheritDoc}
220      *
221      * @since 1.0.0
222      */
223     @Override
224     public String toString() {
225         return getClass().getSimpleName() + " [dependencyType=" + dependencyType + "]";
226     }
227 }