1 | /* |
2 | * Copyright (c) 2001-2009, Jean Tessier |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * |
12 | * * Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * |
16 | * * Neither the name of Jean Tessier nor the names of his contributors |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR |
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | */ |
32 | |
33 | package com.jeantessier.dependency; |
34 | |
35 | import java.util.*; |
36 | |
37 | import org.apache.oro.text.perl.*; |
38 | |
39 | public class LCOM4Gatherer implements Visitor { |
40 | private static final Perl5Util perl = new Perl5Util(); |
41 | |
42 | private Map<ClassNode, Collection<Collection<FeatureNode>>> results = new HashMap<ClassNode, Collection<Collection<FeatureNode>>>(); |
43 | private Collection<FeatureNode> currentComponent; |
44 | private ClassNode currentClass; |
45 | private LinkedList<FeatureNode> unvisitedNodes; |
46 | |
47 | private HashSet<Collection<FeatureNode>> currentComponents; |
48 | |
49 | public Map<ClassNode, Collection<Collection<FeatureNode>>> getResults() { |
50 | return results; |
51 | } |
52 | |
53 | public void traverseNodes(Collection<? extends Node> nodes) { |
54 | for (Node node : nodes) { |
55 | if (node.isConfirmed()) { |
56 | node.accept(this); |
57 | } |
58 | } |
59 | } |
60 | |
61 | public void visitPackageNode(PackageNode node) { |
62 | traverseNodes(node.getClasses()); |
63 | } |
64 | |
65 | public void visitInboundPackageNode(PackageNode node) { |
66 | // Do nothing |
67 | } |
68 | |
69 | public void visitOutboundPackageNode(PackageNode node) { |
70 | // Do nothing |
71 | } |
72 | |
73 | public void visitClassNode(ClassNode node) { |
74 | currentClass = node; |
75 | |
76 | currentComponents = new HashSet<Collection<FeatureNode>>(); |
77 | results.put(currentClass, currentComponents); |
78 | |
79 | unvisitedNodes = filterOutConstructors(currentClass.getFeatures()); |
80 | while (!unvisitedNodes.isEmpty()) { |
81 | unvisitedNodes.removeFirst().accept(this); |
82 | } |
83 | } |
84 | |
85 | public void visitInboundClassNode(ClassNode node) { |
86 | // Do nothing |
87 | } |
88 | |
89 | public void visitOutboundClassNode(ClassNode node) { |
90 | // Do nothing |
91 | } |
92 | |
93 | public void visitFeatureNode(FeatureNode node) { |
94 | currentComponent = new HashSet<FeatureNode>(); |
95 | currentComponents.add(currentComponent); |
96 | currentComponent.add(node); |
97 | |
98 | traverseInbound(node.getInboundDependencies()); |
99 | traverseOutbound(node.getOutboundDependencies()); |
100 | } |
101 | |
102 | public void traverseInbound(Collection<? extends Node> inboundDependencies) { |
103 | for (Node inboundDependency : inboundDependencies) { |
104 | inboundDependency.acceptInbound(this); |
105 | } |
106 | } |
107 | |
108 | public void traverseOutbound(Collection<? extends Node> outboundDependencies) { |
109 | for (Node outboundDependency : outboundDependencies) { |
110 | outboundDependency.acceptOutbound(this); |
111 | } |
112 | } |
113 | |
114 | public void visitInboundFeatureNode(FeatureNode node) { |
115 | visitFeatureDependency(node); |
116 | } |
117 | |
118 | public void visitOutboundFeatureNode(FeatureNode node) { |
119 | visitFeatureDependency(node); |
120 | } |
121 | |
122 | private void visitFeatureDependency(FeatureNode node) { |
123 | if (currentClass.equals(node.getClassNode()) && unvisitedNodes.contains(node)) { |
124 | unvisitedNodes.remove(node); |
125 | currentComponent.add(node); |
126 | traverseInbound(node.getInboundDependencies()); |
127 | traverseOutbound(node.getOutboundDependencies()); |
128 | } |
129 | } |
130 | |
131 | private LinkedList<FeatureNode> filterOutConstructors(Collection<FeatureNode> featureNodes) { |
132 | LinkedList<FeatureNode> result = new LinkedList<FeatureNode>(); |
133 | |
134 | for (FeatureNode featureNode : featureNodes) { |
135 | if (featureNode.isConfirmed() && !isConstructor(featureNode)) { |
136 | result.add(featureNode); |
137 | } |
138 | } |
139 | |
140 | return result; |
141 | } |
142 | |
143 | private boolean isConstructor(FeatureNode node) { |
144 | return perl.match("/(\\w+)\\.\\1\\(/", node.getName()); |
145 | } |
146 | } |