1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.qualitytest.blueprint;
17
18 import java.util.Collections;
19 import java.util.HashSet;
20 import java.util.Set;
21 import java.util.Stack;
22
23 import javax.annotation.Nonnull;
24 import javax.annotation.concurrent.NotThreadSafe;
25
26 import net.sf.qualitycheck.ArgumentsChecked;
27 import net.sf.qualitycheck.Check;
28 import net.sf.qualitycheck.Throws;
29 import net.sf.qualitycheck.exception.IllegalEmptyArgumentException;
30 import net.sf.qualitycheck.exception.IllegalNullArgumentException;
31
32
33
34
35
36
37
38 @NotThreadSafe
39 public final class BlueprintSession {
40
41 private static final String SEPARATOR = "->";
42
43 private final Stack<Class<?>> stack = new Stack<Class<?>>();
44 private final Set<Class<?>> classes = new HashSet<Class<?>>();
45 private int blueprintCount = 0;
46
47 private String lastAction = "";
48
49
50
51
52
53
54
55
56
57 private boolean detectCycles(@Nonnull final Class<?> clazz) {
58 return stack.contains(clazz);
59 }
60
61
62
63
64
65
66 public Set<Class<?>> getBlueprintClasses() {
67 return Collections.unmodifiableSet(classes);
68 }
69
70
71
72
73
74
75 public int getBlueprintCount() {
76 return blueprintCount;
77 }
78
79
80
81
82
83
84
85 public String getContext() {
86 final StringBuffer buffer = new StringBuffer();
87 for (int i = 0; i < stack.size(); i++) {
88 buffer.append(stack.get(i).getName());
89 if (i < stack.size() - 1) {
90 buffer.append(SEPARATOR);
91 }
92 }
93
94 if (!lastAction.isEmpty()) {
95 buffer.append(" {");
96 buffer.append(lastAction);
97 buffer.append('}');
98 }
99
100 return buffer.toString();
101 }
102
103
104
105
106 public void pop() {
107 stack.pop();
108
109 blueprintCount++;
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123 @ArgumentsChecked
124 @Throws(IllegalNullArgumentException.class)
125 public boolean push(@Nonnull final Class<?> clazz) {
126 Check.notNull(clazz, "clazz");
127
128 final boolean cycle = detectCycles(clazz);
129
130 stack.push(clazz);
131 classes.add(clazz);
132
133 return cycle;
134 }
135
136
137
138
139
140
141
142
143 @ArgumentsChecked
144 @Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class })
145 public void setLastAction(@Nonnull final String lastAction) {
146 this.lastAction = Check.notEmpty(lastAction, "lastAction");
147 }
148 }