View Javadoc

1   /*
2    AbstractPlantUMLClassesDiagramElement.java
3    Creation date : 8/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.element;
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.tag.PlantUMLClassesDiagramElementTag;
35  
36  /**
37   * The abstract implementation of the {@link PlantUMLClassesDiagramElement} 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 AbstractPlantUMLClassesDiagramElement implements PlantUMLClassesDiagramElement {
44  
45      /** Serial version UID. */
46      private static final long serialVersionUID = -698646710338667065L;
47  
48      /** The class logger. */
49      private static final transient Logger LOGGER = getLogger(AbstractPlantUMLClassesDiagramElement.class.getName());
50  
51      /** The plantUML element full name. */
52      private final String name;
53  
54      /** The plantUML element tag. */
55      private final PlantUMLClassesDiagramElementTag elementTag;
56  
57      /**
58       * Full constructor.
59       *
60       * @param fullName
61       *            the element full name, mustn't be <code>null</code>.
62       * @param tag
63       *            plantUML element tag available in the plantUML classes diagram language, mustn't
64       *            be <code>null</code>.
65       * @since 1.1.1
66       */
67      protected AbstractPlantUMLClassesDiagramElement(final String fullName, final PlantUMLClassesDiagramElementTag tag) {
68          name = fullName;
69          elementTag = tag;
70      }
71  
72      /**
73       * {@inheritDoc}
74       *
75       * @since 1.1.1
76       */
77      @Override
78      public int compareTo(final PlantUMLClassesDiagramElement o) {
79          int comparison;
80  
81          if (this == o) {
82              comparison = EQUAL.getResult();
83          } else {
84              if (getElementTag().equals(o.getElementTag())) {
85                  comparison = getName().compareTo(o.getName());
86              } else {
87                  comparison = getElementTag().toString().compareTo(o.getElementTag().toString());
88              }
89          }
90  
91          return comparison;
92      }
93  
94      /**
95       * {@inheritDoc}
96       *
97       * @since 1.1.1
98       */
99      @Override
100     public PlantUMLClassesDiagramElement deepClone() {
101         AbstractPlantUMLClassesDiagramElement a = null;
102 
103         try {
104             a = (AbstractPlantUMLClassesDiagramElement) super.clone();
105         } catch (final CloneNotSupportedException cnse) {
106             LOGGER.log(SEVERE, UNEXPECTED_ERROR, cnse);
107         }
108 
109         return a;
110     }
111 
112     /**
113      * {@inheritDoc}
114      *
115      * @since 1.1.1
116      */
117     @Override
118     public boolean equals(final Object obj) {
119         if (this == obj) {
120             return true;
121         }
122         if (obj == null) {
123             return false;
124         }
125         if (getClass() != obj.getClass()) {
126             return false;
127         }
128         final AbstractPlantUMLClassesDiagramElement other = (AbstractPlantUMLClassesDiagramElement) obj;
129         if (elementTag == null) {
130             if (other.elementTag != null) {
131                 return false;
132             }
133         } else if (!elementTag.equals(other.elementTag)) {
134             return false;
135         }
136         if (name == null) {
137             if (other.name != null) {
138                 return false;
139             }
140         } else if (!name.equals(other.name)) {
141             return false;
142         }
143         return true;
144     }
145 
146     /**
147      * Gets an additional plantUML description, which may be necessary for some element.
148      *
149      * @return an additional plantUML description.
150      * @since 1.1.1
151      */
152     protected abstract String getAdditionalPlantUMLTextDescription();
153 
154     /**
155      * {@inheritDoc}
156      *
157      * @since 1.1.1
158      */
159     @Override
160     public PlantUMLClassesDiagramElementTag getElementTag() {
161         return elementTag;
162     }
163 
164     /**
165      * {@inheritDoc}
166      *
167      * @since 1.1.1
168      */
169     @Override
170     public String getName() {
171         return name;
172     }
173 
174     /**
175      * {@inheritDoc}
176      *
177      * @since 1.1.1
178      */
179     @Override
180     public String getPlantUMLTextDescription() {
181         return getElementTag().getTagName() + getName() + getAdditionalPlantUMLTextDescription();
182     }
183 
184     /**
185      * {@inheritDoc}
186      *
187      * @since 1.1.1
188      */
189     @Override
190     public int hashCode() {
191         final int prime = 31;
192         int result = 1;
193         result = prime * result + ((elementTag == null) ? 0 : elementTag.hashCode());
194         result = prime * result + ((name == null) ? 0 : name.hashCode());
195         return result;
196     }
197 
198     /**
199      * {@inheritDoc}
200      *
201      * @since 1.1.1
202      */
203     @Override
204     public String toString() {
205         return "elementTag=" + elementTag + ", name=" + name;
206     }
207 }