|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
TextCyclePrinter.java | 100% | 100% | 100% | 100% |
|
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.io.*; | |
36 | import java.util.*; | |
37 | ||
38 | public class TextCyclePrinter implements CyclePrinter { | |
39 | protected PrintWriter out; | |
40 | ||
41 | private String indentText = " "; | |
42 | private int indentLevel; | |
43 | ||
44 | 13 | public TextCyclePrinter(PrintWriter out) { |
45 | 13 | this.out = out; |
46 | } | |
47 | ||
48 | 1 | public void setIndentText(String indentText) { |
49 | 1 | this.indentText = indentText; |
50 | } | |
51 | ||
52 | 8 | public void visitCycles(Collection<Cycle> cycles) { |
53 | 8 | for (Cycle cycle : cycles) { |
54 | 8 | visitCycle(cycle); |
55 | } | |
56 | } | |
57 | ||
58 | 13 | public void visitCycle(Cycle cycle) { |
59 | 13 | Node currentNode; |
60 | 13 | Node previousNode; |
61 | ||
62 | 13 | Iterator<Node> i = cycle.getPath().iterator(); |
63 | 13 | currentNode = i.next(); |
64 | 13 | visitFirstNode(currentNode); |
65 | ||
66 | 13 | while (i.hasNext()) { |
67 | 18 | previousNode = currentNode; |
68 | 18 | currentNode = i.next(); |
69 | 18 | visitNode(previousNode, currentNode); |
70 | } | |
71 | ||
72 | 13 | previousNode = currentNode; |
73 | 13 | currentNode = cycle.getPath().iterator().next(); |
74 | 13 | visitNode(previousNode, currentNode); |
75 | } | |
76 | ||
77 | 13 | private void visitFirstNode(Node node) { |
78 | 13 | indentLevel = 0; |
79 | 13 | printFirstNode(node); |
80 | } | |
81 | ||
82 | 31 | private void visitNode(Node previousNode, Node currentNode) { |
83 | 31 | indentLevel++; |
84 | 31 | indent(); |
85 | 31 | printNode(previousNode, currentNode); |
86 | } | |
87 | ||
88 | 31 | private void indent() { |
89 | 31 | for (int i = 0; i < indentLevel; i++) { |
90 | 54 | out.print(indentText); |
91 | } | |
92 | } | |
93 | ||
94 | 9 | protected void printFirstNode(Node node) { |
95 | 9 | out.println(node); |
96 | } | |
97 | ||
98 | 22 | protected void printNode(Node previousNode, Node currentNode) { |
99 | 22 | out.print("--> "); |
100 | 22 | out.println(currentNode); |
101 | } | |
102 | } |
|