Cleaned up the cancel process

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@808 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
David Craine
2002-08-28 15:38:14 +00:00
parent 60b7c6cf13
commit 95cb650261
2 changed files with 24 additions and 15 deletions

View File

@ -73,6 +73,7 @@ public class CPD {
if (!listener.update("Starting to process " + tokenSets.size() + " files")) return;
GST gst = new GST(tokenSets, minimumTileSize);
results = gst.crunch(listener);
if (results == null) results = new Results(); //just ot make sure we don't pass back a null Results
}
public Results getResults() {

View File

@ -1,7 +1,7 @@
/*
* User: tom
* Date: Jul 30, 2002
* Time: 10:55:08 AM
* User: tom
* Date: Jul 30, 2002
* Time: 10:55:08 AM
*/
package net.sourceforge.pmd.cpd;
@ -15,20 +15,23 @@ public class GST {
private int minimumTileSize;
private TokenSets tokenSets;
class CancelledException extends Exception {
}
public GST(TokenSets tokenSets, int minimumTileSize) {
this.minimumTileSize = minimumTileSize;
this.tokenSets = tokenSets;
}
public Results crunch(CPDListener listener) {
public Results crunch(CPDListener listener) {
Results results = new Results();
Occurrences occ =new Occurrences(tokenSets, listener);
while (!occ.isEmpty()) {
if (!listener.update("Tiles left to be crunched " + occ.size())) return null;
if (!listener.update("Tiles left to be crunched " + occ.size())) return null;
// add any tiles over the minimum size to the results
if (!listener.update("Adding large tiles to results")) return null;
if (!listener.update("Adding large tiles to results")) return null;
for (Iterator i = occ.getTiles(); i.hasNext();) {
Tile tile = (Tile)i.next();
if (tile.getTokenCount() >= minimumTileSize) {
@ -41,23 +44,28 @@ public class GST {
Occurrences newOcc = new Occurrences(listener);
int tilesSoFar = 0;
int totalTiles = occ.size();
for (Iterator i = occ.getTiles(); i.hasNext();) {
tilesSoFar++;
Tile tile = (Tile)i.next();
if (!newOcc.containsAnyTokensIn(tile)) {
expandTile(occ, newOcc, tile, listener, tilesSoFar, totalTiles);
try {
for (Iterator i = occ.getTiles(); i.hasNext();) {
tilesSoFar++;
Tile tile = (Tile)i.next();
if (!newOcc.containsAnyTokensIn(tile)) {
expandTile(occ, newOcc, tile, listener, tilesSoFar, totalTiles);
}
}
}
catch (CancelledException ce) {
return null;
}
occ = newOcc;
}
return results;
}
public Results crunch() {
return crunch(new CPDNullListener());
return crunch(new CPDNullListener());
}
private void expandTile(Occurrences oldOcc, Occurrences newOcc, Tile tile, CPDListener listener, int tilesSoFar, int totalTiles) {
private void expandTile(Occurrences oldOcc, Occurrences newOcc, Tile tile, CPDListener listener, int tilesSoFar, int totalTiles) throws CancelledException {
for (Iterator i = oldOcc.getOccurrences(tile); i.hasNext();) {
TokenEntry tok = (TokenEntry)i.next();
TokenList tokenSet = tokenSets.getTokenList(tok);
@ -68,10 +76,10 @@ public class GST {
Tile newTile = tile.copy();
newTile.add(token);
newOcc.addTile(newTile, tok);
if (!listener.addedNewTile(newTile, tilesSoFar, totalTiles)) break;
if (!listener.addedNewTile(newTile, tilesSoFar, totalTiles)) throw new CancelledException();
}
}
}
newOcc.deleteSoloTiles();
}
}
}