package com.github.cstroe.svndumpgui.internal.transform;

import com.github.cstroe.svndumpgui.api.FileContentChunk;
import com.github.cstroe.svndumpgui.api.SvnNode;
import com.github.cstroe.svndumpgui.api.SvnRevision;

public class ClearRevision extends AbstractSvnDumpMutator {

    private final int NOT_SET = -1;
    private final int fromRevision;
    private final int toRevision;

    private SvnRevision currentRevision;
    private boolean changedSomething = false;

    public ClearRevision(int revision) {
        if(revision < 0 ) {
            throw new IllegalArgumentException("Negative revision numbers not allowed");
        }
        this.fromRevision = revision;
        this.toRevision = NOT_SET;
    }

    public ClearRevision(int fromRevision, int toRevision) {
        if(fromRevision < 0 || toRevision < 0) {
            throw new IllegalArgumentException("Negative revision numbers not allowed");
        }
        if(fromRevision >= toRevision) {
            throw new IllegalArgumentException("Range must span at least 2 revisions in ascending order.");
        }
        this.fromRevision = fromRevision;
        this.toRevision = toRevision;
    }

    private boolean revisionMatches(SvnRevision revision) {
        return (toRevision == NOT_SET && revision.getNumber() == fromRevision) ||
               (revision.getNumber() >= fromRevision && revision.getNumber() <= toRevision);
    }

    @Override
    public void consume(SvnRevision revision) {
        currentRevision = revision;
        super.consume(revision);
    }

    @Override
    public void consume(SvnNode node) {
        if(revisionMatches(currentRevision)) {
            changedSomething = true; // notice we don't call super.consume(node) in this case.
        } else {
            super.consume(node);
        }
    }

    @Override
    public void consume(FileContentChunk chunk) {
        if(revisionMatches(currentRevision)) {
            changedSomething = true; // notice we don't call super.consumer(chunk) in this case.
        } else {
            super.consume(chunk);
        }
    }

    @Override
    public void finish() {
        if(!changedSomething) {
            throw new IllegalArgumentException("No revisions matched: " + toString());
        }
        super.finish();
    }

    @Override
    public String toString() {
        return "NodeClear{" +
                "fromRevision=" + fromRevision +
                ", toRevision=" + toRevision +
                '}';
    }
}

@@ -396,35 +396,40 @@
 ate 
-SvnRevision currentRevision
+boolean matchedSomething = false
 ;%0A  
@@ -446,32 +446,33 @@
 boolean 
-changedSomething
+inClearedRevision
  = false
@@ -1493,35 +1493,124 @@
     
-currentRevision = revision;
+if(revisionMatches(revision)) %7B%0A            inClearedRevision = true;%0A            matchedSomething = true;%0A        %7D
 %0A   
@@ -1711,39 +1711,26 @@
      if(
-revisionMatches(current
+!inCleared
 Revision
@@ -1722,33 +1722,32 @@
 ClearedRevision)
-)
  %7B%0A            c
@@ -1749,105 +1749,140 @@
     
-changedSomething = true; // notice we don't call super.consume(node) in this case.%0A        %7D else
+super.consume(node);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void consume(FileContentChunk chunk) %7B%0A        if(!inClearedRevision)
  %7B%0A 
@@ -1898,36 +1898,37 @@
   super.consume(
-node
+chunk
 );%0A        %7D%0A   
@@ -1965,76 +1965,53 @@
 oid 
-consume(FileContentChunk chunk) %7B%0A        if(revisionMatches(current
+endNode(SvnNode node) %7B%0A        if(!inCleared
 Revi
@@ -2007,33 +2007,32 @@
 ClearedRevision)
-)
  %7B%0A            c
@@ -2034,153 +2034,308 @@
     
-changedSomething = true; // notice we don't call super.consumer(chunk) in this case.%0A
+super.endNode(node);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void endChunks() %7B%0A        if(!inClearedRevision) %7B%0A            super.endChunks();%0A    
     
+%7D%0A
     %7D
- else %7B%0A            super.consume(chunk);%0A        %7D
+%0A%0A    @Override%0A    public void endRevision(SvnRevision revision) %7B%0A        inClearedRevision = false;%0A        super.endRevision(revision);
 %0A   
@@ -2395,13 +2395,13 @@
 if(!
-chang
+match
 edSo

Fix consumer chaining bug in ClearRevision.