View Javadoc

1   /*
2    AbstractPlantUMLClassesDiagramRelation.java
3    Creation date : 9/12/2011
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.plantumldiagram.classes.relation;
26  
27  import static java.util.logging.Level.SEVERE;
28  import static java.util.logging.Logger.getLogger;
29  import static net.sourceforge.plantumldependency.common.constants.log.ErrorConstants.UNEXPECTED_ERROR;
30  import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.EQUAL;
31  
32  import java.util.logging.Logger;
33  
34  import net.sourceforge.plantumldependency.cli.plantumldiagram.classes.element.PlantUMLClassesDiagramElement;
35  
36  /**
37   * The abstract implementation of the {@link PlantUMLClassesDiagramRelation} interface.
38   *
39   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
40   * @since 1.1.1
41   * @version 1.3.0
42   */
43  public abstract class AbstractPlantUMLClassesDiagramRelation implements PlantUMLClassesDiagramRelation {
44  
45      /** Serial version UID. */
46      private static final long serialVersionUID = 2549425584926638370L;
47  
48      /** The class logger. */
49      private static final transient Logger LOGGER = getLogger(AbstractPlantUMLClassesDiagramRelation.class.getName());
50  
51      /** The first element of the relation. */
52      private final PlantUMLClassesDiagramElement firstElement;
53  
54      /** The second element of the relation. */
55      private final PlantUMLClassesDiagramElement secondElement;
56  
57      /** The relation type used in the plantUML language. */
58      private final PlantUMLClassesDiagramRelationType type;
59  
60      /**
61       * Full constructor.
62       *
63       * @param firstElt
64       *            The first element of the relation, mustn't be <code>null</code> nor empty.
65       * @param secondElt
66       *            The second element of the relation, mustn't be <code>null</code> nor empty.
67       * @param relationType
68       *            The relation type used in the plantUML language, mustn't be <code>null</code>.
69       * @since 1.1.1
70       */
71      protected AbstractPlantUMLClassesDiagramRelation(final PlantUMLClassesDiagramElement firstElt,
72              final PlantUMLClassesDiagramElement secondElt, final PlantUMLClassesDiagramRelationType relationType) {
73          firstElement = firstElt;
74          secondElement = secondElt;
75          type = relationType;
76      }
77  
78      /**
79       * {@inheritDoc}
80       *
81       * @since 1.1.1
82       */
83      @Override
84      public int compareTo(final PlantUMLClassesDiagramRelation o) {
85          int comparison;
86  
87          if (this == o) {
88              comparison = EQUAL.getResult();
89          } else {
90              comparison = getFirstElement().compareTo(o.getFirstElement());
91              if (comparison == 0) {
92                  comparison = getSecondElement().compareTo(o.getSecondElement());
93                  if (comparison == 0) {
94                      comparison = getType().compareTo(getType());
95                  }
96              }
97          }
98  
99          return comparison;
100     }
101 
102     /**
103      * {@inheritDoc}
104      *
105      * @since 1.1.1
106      */
107     @Override
108     public PlantUMLClassesDiagramRelation deepClone() {
109         AbstractPlantUMLClassesDiagramRelation a = null;
110 
111         try {
112             a = (AbstractPlantUMLClassesDiagramRelation) super.clone();
113         } catch (final CloneNotSupportedException cnse) {
114             LOGGER.log(SEVERE, UNEXPECTED_ERROR, cnse);
115         }
116 
117         return a;
118     }
119 
120     /**
121      * {@inheritDoc}
122      *
123      * @since 1.1.1
124      */
125     @Override
126     public boolean equals(final Object obj) {
127         if (this == obj) {
128             return true;
129         }
130         if (obj == null) {
131             return false;
132         }
133         if (getClass() != obj.getClass()) {
134             return false;
135         }
136         final AbstractPlantUMLClassesDiagramRelation other = (AbstractPlantUMLClassesDiagramRelation) obj;
137         if (firstElement == null) {
138             if (other.firstElement != null) {
139                 return false;
140             }
141         } else if (!firstElement.equals(other.firstElement)) {
142             return false;
143         }
144         if (secondElement == null) {
145             if (other.secondElement != null) {
146                 return false;
147             }
148         } else if (!secondElement.equals(other.secondElement)) {
149             return false;
150         }
151         if (type != other.type) {
152             return false;
153         }
154         return true;
155     }
156 
157     /**
158      * {@inheritDoc}
159      *
160      * @since 1.1.1
161      */
162     @Override
163     public PlantUMLClassesDiagramElement getFirstElement() {
164         return firstElement;
165     }
166 
167     /**
168      * {@inheritDoc}
169      *
170      * @since 1.1.1
171      */
172     @Override
173     public String getPlantUMLTextDescription() {
174         return getFirstElement().getName() + getType().getRightRelationSymbol() + getSecondElement().getName();
175     }
176 
177     /**
178      * {@inheritDoc}
179      *
180      * @since 1.1.1
181      */
182     @Override
183     public PlantUMLClassesDiagramElement getSecondElement() {
184         return secondElement;
185     }
186 
187     /**
188      * {@inheritDoc}
189      *
190      * @since 1.1.1
191      */
192     @Override
193     public PlantUMLClassesDiagramRelationType getType() {
194         return type;
195     }
196 
197     /**
198      * {@inheritDoc}
199      *
200      * @since 1.1.1
201      */
202     @Override
203     public int hashCode() {
204         final int prime = 31;
205         int result = 1;
206         result = prime * result + ((firstElement == null) ? 0 : firstElement.hashCode());
207         result = prime * result + ((secondElement == null) ? 0 : secondElement.hashCode());
208         result = prime * result + ((type == null) ? 0 : type.hashCode());
209         return result;
210     }
211 
212     /**
213      * {@inheritDoc}
214      *
215      * @since 1.1.1
216      */
217     @Override
218     public String toString() {
219         return getClass().getSimpleName() + " [firstElementName=" + firstElement + ", secondElement=" + secondElement
220                 + ", type=" + type + "]";
221     }
222 }