91 /**
92 * Specify a dependency for a child interview.
93 * When the interview is executed, all dependencies for each child interview
94 * will be invoked before that child.
95 * @param child the interview which depends on (and will be executed after)
96 * the dependency
97 * @param dependency the interview on which the child interview depends,
98 * and which will be executed before the child interview
99 * @throws InterviewSet.CycleFault if a dependency cycle would be created
100 * @see #removeDependency
101 */
102 protected void addDependency(Interview child, Interview dependency)
103 throws CycleFault
104 {
105 if (child == null)
106 throw new NullPointerException();
107
108 if (dependency == null)
109 throw new NullPointerException();
110
111 Set allDeps = getAllDependencies(dependency);
112 if (allDeps != null && allDeps.contains(child))
113 throw new CycleFault(child, dependency);
114
115 Set<Interview> deps = getDependencies(child, true);
116 deps.add(dependency);
117
118 sortedCalls = null;
119 }
120
121 /**
122 * Remove any dependency between two interviews, and hence any ordering
123 * constraint between these two interviews.
124 * @param child the interview which depends on the dependency
125 * @param dependency the interview on which the child interview depends
126 */
127 protected void removeDependency(Interview child, Interview dependency) {
128 if (child == null)
129 throw new NullPointerException();
130
131 if (dependency == null)
132 throw new NullPointerException();
133
134 Set deps = getDependencies(child, false);
135
136 if (deps != null)
137 deps.remove(dependency);
138
139 if (deps.size() == 0)
140 dependencies.remove(child);
141
142 sortedCalls = null;
143 }
144
145 private Set<Interview> getDependencies(Interview child, boolean create) {
146 Set<Interview> deps = dependencies.get(child);
147
148 if (deps == null && create) {
149 deps = new TreeSet<Interview>(new ChildComparator());
150 dependencies.put(child, deps);
151 }
152
153 return deps;
154 }
155
156 private Set getAllDependencies(Interview child) {
157 Set<Interview> s = new HashSet<>();
158 getAllDependencies(child, s);
159 return s;
160 }
161
162 private void getAllDependencies(Interview child, Set<Interview> s) {
163 if (s.contains(child))
164 return;
165
166 Set<Interview> deps = getDependencies(child, false);
167 if (deps != null) {
168 for (Iterator<Interview> iter = deps.iterator(); iter.hasNext(); ) {
169 Interview i = iter.next();
170 getAllDependencies(i, s);
171 s.add(i);
172 }
173 }
174 }
175
176 private Interview[] sortChildren() {
229
230 return sortedCalls;
231 }
232 };
233
234 private FinalQuestion qEnd = new FinalQuestion(this);
235
236 private List<Interview> children = new ArrayList<>();
237 private Map<Interview, Set<Interview>> dependencies = new HashMap<>();
238 private Question sortedCalls;
239
240 private class ChildComparator implements Comparator<Interview>
241 {
242 public int compare(Interview o1, Interview o2) {
243 if (!children.contains(o1) || !children.contains(o2))
244 throw new IllegalArgumentException();
245
246 if (o1 == o2)
247 return 0;
248
249 for (Iterator iter = children.iterator(); iter.hasNext(); ) {
250 Object o = iter.next();
251 if (o == o1)
252 return -1;
253 if (o == o2)
254 return 1;
255 }
256
257 throw new IllegalStateException();
258 }
259 }
260 }
|
91 /**
92 * Specify a dependency for a child interview.
93 * When the interview is executed, all dependencies for each child interview
94 * will be invoked before that child.
95 * @param child the interview which depends on (and will be executed after)
96 * the dependency
97 * @param dependency the interview on which the child interview depends,
98 * and which will be executed before the child interview
99 * @throws InterviewSet.CycleFault if a dependency cycle would be created
100 * @see #removeDependency
101 */
102 protected void addDependency(Interview child, Interview dependency)
103 throws CycleFault
104 {
105 if (child == null)
106 throw new NullPointerException();
107
108 if (dependency == null)
109 throw new NullPointerException();
110
111 Set<Interview> allDeps = getAllDependencies(dependency);
112 if (allDeps != null && allDeps.contains(child))
113 throw new CycleFault(child, dependency);
114
115 Set<Interview> deps = getDependencies(child, true);
116 deps.add(dependency);
117
118 sortedCalls = null;
119 }
120
121 /**
122 * Remove any dependency between two interviews, and hence any ordering
123 * constraint between these two interviews.
124 * @param child the interview which depends on the dependency
125 * @param dependency the interview on which the child interview depends
126 */
127 protected void removeDependency(Interview child, Interview dependency) {
128 if (child == null)
129 throw new NullPointerException();
130
131 if (dependency == null)
132 throw new NullPointerException();
133
134 Set<Interview> deps = getDependencies(child, false);
135
136 if (deps != null)
137 deps.remove(dependency);
138
139 if (deps.size() == 0)
140 dependencies.remove(child);
141
142 sortedCalls = null;
143 }
144
145 private Set<Interview> getDependencies(Interview child, boolean create) {
146 Set<Interview> deps = dependencies.get(child);
147
148 if (deps == null && create) {
149 deps = new TreeSet<Interview>(new ChildComparator());
150 dependencies.put(child, deps);
151 }
152
153 return deps;
154 }
155
156 private Set<Interview> getAllDependencies(Interview child) {
157 Set<Interview> s = new HashSet<>();
158 getAllDependencies(child, s);
159 return s;
160 }
161
162 private void getAllDependencies(Interview child, Set<Interview> s) {
163 if (s.contains(child))
164 return;
165
166 Set<Interview> deps = getDependencies(child, false);
167 if (deps != null) {
168 for (Iterator<Interview> iter = deps.iterator(); iter.hasNext(); ) {
169 Interview i = iter.next();
170 getAllDependencies(i, s);
171 s.add(i);
172 }
173 }
174 }
175
176 private Interview[] sortChildren() {
229
230 return sortedCalls;
231 }
232 };
233
234 private FinalQuestion qEnd = new FinalQuestion(this);
235
236 private List<Interview> children = new ArrayList<>();
237 private Map<Interview, Set<Interview>> dependencies = new HashMap<>();
238 private Question sortedCalls;
239
240 private class ChildComparator implements Comparator<Interview>
241 {
242 public int compare(Interview o1, Interview o2) {
243 if (!children.contains(o1) || !children.contains(o2))
244 throw new IllegalArgumentException();
245
246 if (o1 == o2)
247 return 0;
248
249 for (Iterator<Interview> iter = children.iterator(); iter.hasNext(); ) {
250 Interview o = iter.next();
251 if (o == o1)
252 return -1;
253 if (o == o2)
254 return 1;
255 }
256
257 throw new IllegalStateException();
258 }
259 }
260 }
|