Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CrConsiderSingleton |
|
| 12.0;12 |
1 | /* $Id: CrConsiderSingleton.java 17831 2010-01-12 18:56:55Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * tfmorris | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2007 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.pattern.cognitive.critics; | |
40 | ||
41 | import java.util.Iterator; | |
42 | ||
43 | import org.argouml.cognitive.Designer; | |
44 | import org.argouml.cognitive.ToDoItem; | |
45 | import org.argouml.model.Model; | |
46 | import org.argouml.uml.cognitive.UMLDecision; | |
47 | import org.argouml.uml.cognitive.critics.CrUML; | |
48 | ||
49 | /** | |
50 | * A critic to detect when a class can never have more than one instance (of | |
51 | * itself of any subclasses), and thus whether it is suitable for declaration | |
52 | * as a Singleton (with stereotype «Singleton».<p> | |
53 | * <p> | |
54 | * see the ArgoUML User Manual: Consider Using Singleton Pattern for Class | |
55 | * | |
56 | * @author jrobbins | |
57 | */ | |
58 | public class CrConsiderSingleton extends CrUML { | |
59 | ||
60 | /** | |
61 | * Constructor for the critic.<p> | |
62 | * | |
63 | * Sets up the resource name, which will allow headline and description | |
64 | * to be found for the current locale. Provides a design issue category | |
65 | * (PATTERNS), sets a priority for any to-do items (LOW) and adds triggers | |
66 | * for metaclasses "stereotype", "structuralFeature" and | |
67 | * "associationEnd". | |
68 | */ | |
69 | 900 | public CrConsiderSingleton() { |
70 | 900 | setupHeadAndDesc(); |
71 | 900 | addSupportedDecision(UMLDecision.PATTERNS); |
72 | 900 | setPriority(ToDoItem.LOW_PRIORITY); |
73 | ||
74 | // These may not actually make any difference at present (the code | |
75 | // behind addTrigger needs more work). | |
76 | ||
77 | 900 | addTrigger("stereotype"); |
78 | 900 | addTrigger("structuralFeature"); |
79 | 900 | addTrigger("associationEnd"); |
80 | 900 | } |
81 | ||
82 | ||
83 | /** | |
84 | * The trigger for the critic.<p> | |
85 | * | |
86 | * First check we are already a Singleton.<p> | |
87 | * | |
88 | * Otherwise plausible candidates for the Singleton design pattern are | |
89 | * classes with no instance variables (i.e. non-static attributes) and no | |
90 | * outgoing associations.<p> | |
91 | * | |
92 | * @param dm the {@link java.lang.Object Object} to be checked against | |
93 | * the critic. | |
94 | * | |
95 | * @param dsgr the {@link org.argouml.cognitive.Designer Designer} | |
96 | * creating the model. Not used, this is for future | |
97 | * development of ArgoUML. | |
98 | * | |
99 | * @return {@link #PROBLEM_FOUND PROBLEM_FOUND} if the critic is | |
100 | * triggered, otherwise {@link #NO_PROBLEM NO_PROBLEM}. | |
101 | */ | |
102 | @Override | |
103 | public boolean predicate2(Object dm, Designer dsgr) { | |
104 | ||
105 | // Only look at classes... | |
106 | ||
107 | 0 | if (!(Model.getFacade().isAClass(dm))) { |
108 | 0 | return NO_PROBLEM; |
109 | } | |
110 | ||
111 | // and not association classes | |
112 | 0 | if (Model.getFacade().isAAssociationClass(dm)) { |
113 | 0 | return NO_PROBLEM; |
114 | } | |
115 | ||
116 | // with a name... | |
117 | 0 | if (Model.getFacade().getName(dm) == null |
118 | || "".equals(Model.getFacade().getName(dm))) { | |
119 | 0 | return NO_PROBLEM; |
120 | } | |
121 | ||
122 | // ... and not incompletely imported | |
123 | 0 | if (!(Model.getFacade().isPrimaryObject(dm))) { |
124 | 0 | return NO_PROBLEM; |
125 | } | |
126 | ||
127 | // abstract classes are hardly ever singletons | |
128 | 0 | if (Model.getFacade().isAbstract(dm)) { |
129 | 0 | return NO_PROBLEM; |
130 | } | |
131 | ||
132 | // Check for Singleton stereotype, uninitialised instance variables and | |
133 | // outgoing associations, as per JavaDoc above. | |
134 | ||
135 | 0 | if (Model.getFacade().isSingleton(dm)) { |
136 | 0 | return NO_PROBLEM; |
137 | } | |
138 | ||
139 | 0 | if (Model.getFacade().isUtility(dm)) { |
140 | 0 | return NO_PROBLEM; |
141 | } | |
142 | ||
143 | // If there is an attribute which is not static => no problem | |
144 | 0 | Iterator iter = Model.getFacade().getAttributes(dm).iterator(); |
145 | ||
146 | 0 | while (iter.hasNext()) { |
147 | 0 | if (!Model.getFacade().isStatic(iter.next())) { |
148 | 0 | return NO_PROBLEM; |
149 | } | |
150 | } | |
151 | ||
152 | ||
153 | // If there is an outgoing association => no problem | |
154 | 0 | Iterator ends = Model.getFacade().getAssociationEnds(dm).iterator(); |
155 | ||
156 | 0 | while (ends.hasNext()) { |
157 | 0 | Iterator otherends = |
158 | Model.getFacade() | |
159 | .getOtherAssociationEnds(ends.next()).iterator(); | |
160 | ||
161 | 0 | while (otherends.hasNext()) { |
162 | 0 | if (Model.getFacade().isNavigable(otherends.next())) { |
163 | 0 | return NO_PROBLEM; |
164 | } | |
165 | } | |
166 | 0 | } |
167 | ||
168 | 0 | return PROBLEM_FOUND; |
169 | } | |
170 | ||
171 | ||
172 | /** | |
173 | * The UID. | |
174 | */ | |
175 | private static final long serialVersionUID = -178026888698499288L; | |
176 | } | |
177 |