Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CheckManager |
|
| 3.0;3 |
1 | /* $Id: CheckManager.java 17814 2010-01-12 18:33:50Z 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 | * thn | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2006 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.cognitive.checklist; | |
40 | ||
41 | import java.io.Serializable; | |
42 | import java.util.Hashtable; | |
43 | import java.util.Enumeration; | |
44 | ||
45 | /** | |
46 | * The CheckManager keeps track of which Checklists should be | |
47 | * presented for a given design material. CheckManager also keeps | |
48 | * track of which CheckItem's are checked off for a given design | |
49 | * element. | |
50 | * | |
51 | * @author Jason Robbins | |
52 | */ | |
53 | public class CheckManager implements Serializable { | |
54 | ||
55 | //////////////////////////////////////////////////////////////// | |
56 | // static variables | |
57 | ||
58 | /** | |
59 | * List of checklists. | |
60 | * | |
61 | * Indexed on the object type of the element that this checklist is | |
62 | * appropriate for. | |
63 | */ | |
64 | 900 | private static Hashtable lists = new Hashtable(); |
65 | ||
66 | /** | |
67 | * List of ChecklistStatus:es. | |
68 | * | |
69 | * Indexed on the model element itself. | |
70 | * TODO: Should use weak references so that this is forgotten about | |
71 | * when the object is removed. | |
72 | */ | |
73 | 900 | private static Hashtable statuses = new Hashtable(); |
74 | ||
75 | /** | |
76 | * Constructor. | |
77 | */ | |
78 | 0 | public CheckManager() { } |
79 | ||
80 | //////////////////////////////////////////////////////////////// | |
81 | // static accessors | |
82 | ||
83 | /** | |
84 | * Gets the checklist for an element. | |
85 | * | |
86 | * @param dm is the element | |
87 | * @return a checklist | |
88 | */ | |
89 | public static Checklist getChecklistFor(Object dm) { | |
90 | Checklist cl; | |
91 | ||
92 | 1201 | java.lang.Class cls = dm.getClass(); |
93 | 7206 | while (cls != null) { |
94 | 6005 | cl = lookupChecklist(cls); |
95 | 6005 | if (cl != null) { |
96 | 0 | return cl; |
97 | } | |
98 | 6005 | cls = cls.getSuperclass(); |
99 | } | |
100 | 1201 | return null; |
101 | } | |
102 | ||
103 | /** | |
104 | * Find an element in the list. | |
105 | * | |
106 | * This is a little more complex than the simple lookup since it might be | |
107 | * that we are indexing with a class and the list contains interfaces. | |
108 | * | |
109 | * Since the hashtable lookup is a lot faster than the linear search we | |
110 | * add the result of the linear search to the hashtable so that the next | |
111 | * time we need not do it. | |
112 | * | |
113 | * @return Checklist or null if noone exist. | |
114 | * @param cls the class to lookup. | |
115 | */ | |
116 | private static Checklist lookupChecklist(Class cls) { | |
117 | 6005 | if (lists.contains(cls)) { |
118 | 0 | return (Checklist) lists.get(cls); |
119 | } | |
120 | ||
121 | // Now lets search | |
122 | 6005 | Enumeration enumeration = lists.keys(); |
123 | ||
124 | 22560 | while (enumeration.hasMoreElements()) { |
125 | 16555 | Object clazz = enumeration.nextElement(); |
126 | ||
127 | 16555 | Class[] intfs = cls.getInterfaces(); |
128 | 49665 | for (int i = 0; i < intfs.length; i++) { |
129 | 33110 | if (intfs[i].equals(clazz)) { |
130 | // We found it! | |
131 | 0 | Checklist chlist = (Checklist) lists.get(clazz); |
132 | ||
133 | // Enter the class to speed up the next search. | |
134 | 0 | lists.put(cls, chlist); |
135 | 0 | return chlist; |
136 | } | |
137 | } | |
138 | 16555 | } |
139 | ||
140 | 6005 | return null; |
141 | } | |
142 | ||
143 | ||
144 | /** | |
145 | * Registers a new list. Used when setting up the checklist stuff. | |
146 | * | |
147 | * @param dm the class for which the Checklist holds | |
148 | * @param cl the Checklist | |
149 | */ | |
150 | public static void register(Object dm, Checklist cl) { | |
151 | 9900 | lists.put(dm, cl); |
152 | 9900 | } |
153 | ||
154 | /** | |
155 | * Get the ChecklistStatus for some object. | |
156 | * | |
157 | * If there is none, then create one. | |
158 | * | |
159 | * @return ChecklistStatus, a half filled list. | |
160 | * @param dm is the object that we retrieve the checklist for | |
161 | */ | |
162 | public static ChecklistStatus getStatusFor(Object dm) { | |
163 | 0 | ChecklistStatus cls = (ChecklistStatus) statuses.get(dm); |
164 | 0 | if (cls == null) { |
165 | 0 | cls = new ChecklistStatus(); |
166 | 0 | statuses.put(dm, cls); |
167 | } | |
168 | 0 | return cls; |
169 | } | |
170 | } /* end class CheckManager */ | |
171 |