package com.github.ninerules.rules.fieldcount;

import java.util.function.Predicate;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.FieldDeclaration;

import com.github.ninerules.StrictLevel;
import com.github.ninerules.entities.Message;
import com.github.ninerules.parameters.FieldCount;
import com.github.ninerules.rules.jdtvisitor.FieldChecker;
import com.github.ninerules.rules.jdtvisitor.FieldCollectingValidator;

public class FieldCountValidator extends FieldCollectingValidator {
    public static final Message FIELD_COUNT = new Message("field count is more than %s.");

    private FieldChecker checker = new FieldChecker();
    private Predicate<FieldDeclaration> predicate = item -> !checker.checkStaticAndFinal(item);

    public FieldCountValidator(StrictLevel level) {
        super(level);
    }

    @Override
    public void endVisit(CompilationUnit unit){
        checkViolation();
        super.endVisit(unit);        
    }

    private void checkViolation(){
        if(computesFieldCount(item -> !checker.checkStatic(item)) > 2){
            addViolation(buildViolation(FIELD_COUNT, lineNumbers(predicate)));
        }
    }

    @Override
    public Class<FieldCount> parameterClass() {
        return FieldCount.class;
    }
}

@@ -1024,67 +1024,21 @@
  if(
-computesFieldCount(item -%3E !checker.checkStatic(item)) %3E 2)
+isViolate()) 
 %7B%0A  
@@ -1114,16 +1114,16 @@
 ate)));%0A
-
         
@@ -1123,32 +1123,236 @@
        %7D%0A    %7D%0A%0A
+    private boolean isViolate() %7B%0A        FieldCount fieldCount = new FieldCount((int)computesFieldCount(item -%3E !checker.checkStatic(item)));%0A        return fieldCount.isGreaterThan(parameter());%0A    %7D%0A%0A
     @Override%0A  

fix #32