View Javadoc

1   /*
2    PlantUMLClassesDiagramImpl.java
3    Creation date : 6/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.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.PlantUMLConstants.END_PLANTUML;
30  import static net.sourceforge.plantumldependency.cli.constants.PlantUMLConstants.START_PLANTUML;
31  import static net.sourceforge.plantumldependency.common.constants.CommonConstants.LINE_SEPARATOR;
32  import static net.sourceforge.plantumldependency.common.constants.log.ErrorConstants.UNEXPECTED_ERROR;
33  
34  import java.util.Set;
35  import java.util.TreeSet;
36  import java.util.logging.Logger;
37  
38  import net.sourceforge.plantumldependency.cli.plantumldiagram.PlantUMLDiagram;
39  import net.sourceforge.plantumldependency.cli.plantumldiagram.WithDescription;
40  import net.sourceforge.plantumldependency.cli.plantumldiagram.classes.PlantUMLClassesDiagram;
41  import net.sourceforge.plantumldependency.cli.plantumldiagram.classes.element.PlantUMLClassesDiagramElement;
42  import net.sourceforge.plantumldependency.cli.plantumldiagram.classes.relation.PlantUMLClassesDiagramRelation;
43  
44  /**
45   * The default {@link PlantUMLClassesDiagram} implementation.
46   *
47   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
48   * @since 1.1.1
49   * @version 1.3.0
50   */
51  public class PlantUMLClassesDiagramImpl implements PlantUMLClassesDiagram {
52  
53      /** Serial version UID. */
54      private static final long serialVersionUID = -5603953525431054169L;
55  
56      /** The class logger. */
57      private static final transient Logger LOGGER = getLogger(PlantUMLClassesDiagramImpl.class.getName());
58  
59      /**
60       * The {@link Set} of all {@link PlantUMLClassesDiagramElement} contained in the plantUML
61       * classes diagram.
62       */
63      private Set < PlantUMLClassesDiagramElement > plantUMLClassesDiagramElements;
64  
65      /**
66       * The {@link Set} of all {@link PlantUMLClassesDiagramRelation} contained in the plantUML
67       * classes diagram.
68       */
69      private Set < PlantUMLClassesDiagramRelation > plantUMLClassesDiagramRelations;
70  
71      /**
72       * Default constructor. Elements and relations are empty.
73       *
74       * @since 1.1.1
75       */
76      public PlantUMLClassesDiagramImpl() {
77          this(new TreeSet < PlantUMLClassesDiagramElement >(), new TreeSet < PlantUMLClassesDiagramRelation >());
78      }
79  
80      /**
81       * Full constructor.
82       *
83       * @param classesDiagramElements
84       *            the {@link Set} of all {@link PlantUMLClassesDiagramElement} contained in the
85       *            plantUML classes diagram, mustn't be <code>null</code>.
86       * @param classesDiagramRelations
87       *            the {@link Set} of all {@link PlantUMLClassesDiagramRelation} contained in the
88       *            plantUML classes diagram, mustn't be <code>null</code>.
89       * @since 1.1.1
90       */
91      public PlantUMLClassesDiagramImpl(final Set < PlantUMLClassesDiagramElement > classesDiagramElements,
92              final Set < PlantUMLClassesDiagramRelation > classesDiagramRelations) {
93          plantUMLClassesDiagramElements = new TreeSet < PlantUMLClassesDiagramElement >(classesDiagramElements);
94          plantUMLClassesDiagramRelations = new TreeSet < PlantUMLClassesDiagramRelation >(classesDiagramRelations);
95      }
96  
97      /**
98       * {@inheritDoc}
99       *
100      * @since 1.1.1
101      */
102     @Override
103     public PlantUMLDiagram deepClone() {
104         PlantUMLClassesDiagramImpl p = null;
105 
106         try {
107             p = (PlantUMLClassesDiagramImpl) super.clone();
108             p.plantUMLClassesDiagramElements = new TreeSet < PlantUMLClassesDiagramElement >();
109             for (final PlantUMLClassesDiagramElement element : getPlantUMLClassesDiagramElements()) {
110                 p.plantUMLClassesDiagramElements.add(element.deepClone());
111             }
112             p.plantUMLClassesDiagramRelations = new TreeSet < PlantUMLClassesDiagramRelation >();
113             for (final PlantUMLClassesDiagramRelation relation : getPlantUMLClassesDiagramRelations()) {
114                 p.plantUMLClassesDiagramRelations.add(relation.deepClone());
115             }
116         } catch (final CloneNotSupportedException cnse) {
117             LOGGER.log(SEVERE, UNEXPECTED_ERROR, cnse);
118         }
119 
120         return p;
121     }
122 
123     /**
124      * {@inheritDoc}
125      *
126      * @since 1.1.1
127      */
128     @Override
129     public boolean equals(final Object obj) {
130         if (this == obj) {
131             return true;
132         }
133         if (obj == null) {
134             return false;
135         }
136         if (getClass() != obj.getClass()) {
137             return false;
138         }
139         final PlantUMLClassesDiagramImpl other = (PlantUMLClassesDiagramImpl) obj;
140         if (plantUMLClassesDiagramElements == null) {
141             if (other.plantUMLClassesDiagramElements != null) {
142                 return false;
143             }
144         } else if (!plantUMLClassesDiagramElements.equals(other.plantUMLClassesDiagramElements)) {
145             return false;
146         }
147         if (plantUMLClassesDiagramRelations == null) {
148             if (other.plantUMLClassesDiagramRelations != null) {
149                 return false;
150             }
151         } else if (!plantUMLClassesDiagramRelations.equals(other.plantUMLClassesDiagramRelations)) {
152             return false;
153         }
154         return true;
155     }
156 
157     /**
158      * {@inheritDoc}
159      *
160      * @since 1.1.1
161      */
162     @Override
163     public Set < PlantUMLClassesDiagramElement > getPlantUMLClassesDiagramElements() {
164         return new TreeSet < PlantUMLClassesDiagramElement >(plantUMLClassesDiagramElements);
165     }
166 
167     /**
168      * {@inheritDoc}
169      *
170      * @since 1.1.1
171      */
172     @Override
173     public Set < PlantUMLClassesDiagramRelation > getPlantUMLClassesDiagramRelations() {
174         return new TreeSet < PlantUMLClassesDiagramRelation >(plantUMLClassesDiagramRelations);
175     }
176 
177     /**
178      * {@inheritDoc}
179      *
180      * @since 1.1.1
181      */
182     @Override
183     public String getPlantUMLTextDescription() {
184         final StringBuilder plantUMLDeclaractionBuffer = new StringBuilder(START_PLANTUML);
185 
186         for (final WithDescription plantUMLElement : getPlantUMLClassesDiagramElements()) {
187             plantUMLDeclaractionBuffer.append(LINE_SEPARATOR);
188             plantUMLDeclaractionBuffer.append(plantUMLElement.getPlantUMLTextDescription());
189         }
190 
191         for (final WithDescription plantUMLClassesDiagramRelation : getPlantUMLClassesDiagramRelations()) {
192             plantUMLDeclaractionBuffer.append(LINE_SEPARATOR);
193             plantUMLDeclaractionBuffer.append(plantUMLClassesDiagramRelation.getPlantUMLTextDescription());
194         }
195 
196         plantUMLDeclaractionBuffer.append(LINE_SEPARATOR);
197         plantUMLDeclaractionBuffer.append(END_PLANTUML);
198 
199         return plantUMLDeclaractionBuffer.toString();
200     }
201 
202     /**
203      * {@inheritDoc}
204      *
205      * @since 1.1.1
206      */
207     @Override
208     public int hashCode() {
209         final int prime = 31;
210         int result = 1;
211         result = prime * result
212                 + ((plantUMLClassesDiagramElements == null) ? 0 : plantUMLClassesDiagramElements.hashCode());
213         result = prime * result
214                 + ((plantUMLClassesDiagramRelations == null) ? 0 : plantUMLClassesDiagramRelations.hashCode());
215         return result;
216     }
217 
218     /**
219      * {@inheritDoc}
220      *
221      * @since 1.1.1
222      */
223     @Override
224     public String toString() {
225         return getClass().getSimpleName() + " [plantUMLClassesDiagramElements=" + plantUMLClassesDiagramElements
226                 + ", plantUMLClassesDiagramRelations=" + plantUMLClassesDiagramRelations + "]";
227     }
228 }