package com.atlassian.bitbucket.mesh.git.gc;

import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import static java.nio.file.FileVisitResult.CONTINUE;
import static java.nio.file.FileVisitResult.TERMINATE;

public class GcLooseFileThresholdVisitor extends SimpleFileVisitor<Path> {

    private final int countThreshold;
    private final long sizeThreshold;

    private long count;
    private long sumSize;

    public GcLooseFileThresholdVisitor(int countThreshold, long sizeThreshold) {
        this.countThreshold = countThreshold;
        this.sizeThreshold = sizeThreshold;
    }

    public boolean isDirty() {
        return count > countThreshold || sumSize > sizeThreshold;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
        if (attrs.isRegularFile()) {
            ++count;
            sumSize += attrs.size();
        }

        return isDirty() ? TERMINATE : CONTINUE;
    }
}
