KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > dependency > CheckCyclesStatement


1 /*
2  * Copyright (c) 2003-2006, Franz-Josef Elmer, All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */

25 /*
26  * Created on 07.04.2006
27  */

28 package classycle.dependency;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32
33 import classycle.PackageProcessor;
34 import classycle.graph.AtomicVertex;
35 import classycle.graph.NameAttributes;
36 import classycle.graph.StrongComponent;
37 import classycle.graph.StrongComponentAnalyser;
38 import classycle.graph.Vertex;
39 import classycle.util.StringPattern;
40
41 public class CheckCyclesStatement implements Statement
42 {
43   private final StringPattern _set;
44   private final int _maximumSize;
45   private final boolean _packageCycles;
46   private final SetDefinitionRepository _repository;
47   
48   public CheckCyclesStatement(StringPattern set, int size, boolean cycles,
49                               SetDefinitionRepository repository)
50   {
51     _set = set;
52     _maximumSize = size;
53     _packageCycles = cycles;
54     _repository = repository;
55   }
56
57   public Result execute(AtomicVertex[] graph)
58   {
59     List JavaDoc filteredGraph = new ArrayList JavaDoc();
60     for (int i = 0; i < graph.length; i++)
61     {
62       if (_set.matches(((NameAttributes) graph[i].getAttributes()).getName()))
63       {
64         filteredGraph.add(graph[i]);
65       }
66     }
67     graph = (AtomicVertex[]) filteredGraph.toArray(new AtomicVertex[0]);
68     if (_packageCycles)
69     {
70       PackageProcessor processor = new PackageProcessor();
71       processor.deepSearchFirst(graph);
72       graph = processor.getGraph();
73     }
74     StrongComponentAnalyser analyser = new StrongComponentAnalyser(graph);
75     Vertex[] condensedGraph = analyser.getCondensedGraph();
76     CyclesResult result = new CyclesResult(createStatement(), _packageCycles);
77     for (int i = 0; i < condensedGraph.length; i++)
78     {
79       StrongComponent strongComponent = (StrongComponent) condensedGraph[i];
80       if (strongComponent.getNumberOfVertices() > _maximumSize)
81       {
82         result.addCycle(strongComponent);
83       }
84     }
85     return result;
86   }
87
88   public String JavaDoc toString()
89   {
90     return createStatement();
91   }
92   
93   private String JavaDoc createStatement()
94   {
95     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
96     buffer.append(DependencyDefinitionParser.CHECK_KEY_WORD).append(' ');
97     if (_packageCycles)
98     {
99       buffer.append(DependencyDefinitionParser.PACKAGE_CYCLES_KEY_WORD);
100     } else
101     {
102       buffer.append(DependencyDefinitionParser.CLASS_CYCLES_KEY_WORD);
103     }
104     buffer.append(" > ").append(_maximumSize).append(' ');
105     buffer.append(DependencyDefinitionParser.IN_KEY_WORD).append(' ');
106     buffer.append(_repository.toString(_set));
107     return new String JavaDoc(buffer);
108   }
109
110   
111 }
112
Popular Tags