1 //$Id: ReferenceProcessor.java 240 2006-05-03 22:45:16Z jg_hamburg $
2 /********************************************************************************
3 * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
4 * Copyright (c) 2004, Joerg and Kai Gellien
5 * All rights reserved.
6 *
7 * The Software is provided under the terms of the Common Public License 1.0
8 * as provided with the distribution of DDTUnit in the file cpl-v10.html.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * + Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * + Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
20 *
21 * + Neither the name of the authors or DDTUnit, nor the
22 * names of its contributors may be used to endorse or promote
23 * products derived from this software without specific prior
24 * written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 ********************************************************************************/
38 package junitx.ddtunit.data.processing;
39
40 import java.util.List;
41 import java.util.Vector;
42
43 import junitx.ddtunit.data.IDataSet;
44
45 class ReferenceProcessor implements IReferenceListener {
46 private List refList;
47
48 private IDataSet clusterDataSet;
49
50 private String groupId;
51
52 private String testId;
53
54 /**
55 * @param clusterDataSet
56 *
57 */
58 public ReferenceProcessor(IDataSet clusterDataSet, String groupId,
59 String testId) {
60 this.refList = new Vector();
61 this.clusterDataSet = clusterDataSet;
62 setGroupId(groupId);
63 setTestId(testId);
64 }
65
66 public ReferenceProcessor(IDataSet clusterDataSet) {
67 this(clusterDataSet, ParserConstants.UNKNOWN, ParserConstants.UNKNOWN);
68 }
69
70 public void add(IReferenceInfo info) {
71 this.refList.add(info);
72 }
73
74 public void resolve() {
75 checkRankingOrder();
76 sortRefList();
77 for (int pos = 0; pos < this.refList.size(); pos++) {
78 IReferenceInfo info = (IReferenceInfo) this.refList.get(pos);
79 info.resolve(this.clusterDataSet, this.getGroupId(), this
80 .getTestId());
81 }
82 clear();
83 }
84
85 private void checkRankingOrder() {
86 for (int countA = 0; countA < this.refList.size(); countA++) {
87 IReferenceInfo infoA = (IReferenceInfo) this.refList.get(countA);
88 for (int countB = countA + 1; countB < this.refList.size(); countB++) {
89 IReferenceInfo infoB = (IReferenceInfo) this.refList
90 .get(countB);
91 infoA.raiseRankOf(infoB);
92 infoB.raiseRankOf(infoA);
93 }
94 }
95 }
96
97 private void sortRefList() {
98 boolean hasChanged = true;
99 while (hasChanged) {
100 hasChanged = false;
101 for (int countA = 0; countA < this.refList.size(); countA++) {
102 IReferenceInfo infoA = (IReferenceInfo) this.refList
103 .get(countA);
104 int aPos = countA;
105 for (int countB = countA + 1; countB < this.refList.size(); countB++) {
106 IReferenceInfo infoB = (IReferenceInfo) this.refList
107 .get(countB);
108 if (infoA.getRank() < infoB.getRank()) {
109 hasChanged = true;
110 this.refList.set(countB, infoA);
111 this.refList.set(aPos, infoB);
112 aPos = countB;
113 }
114 }
115 if (hasChanged) {
116 countA = -1;
117 hasChanged = false;
118 }
119 }
120 }
121 }
122
123 private void clear() {
124 this.refList.clear();
125 }
126
127 public String getGroupId() {
128 return this.groupId;
129 }
130
131 public void setGroupId(String groupId) {
132 if (groupId == null) {
133 this.groupId = ParserConstants.UNKNOWN;
134 } else {
135 this.groupId = groupId;
136 }
137 }
138
139 public String getTestId() {
140 return this.testId;
141 }
142
143 public void setTestId(String testId) {
144 if (testId == null) {
145 this.testId = ParserConstants.UNKNOWN;
146 } else {
147 this.testId = testId;
148 }
149 }
150 }