Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions filescanner/src/FileCounterTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.atomic.AtomicLong;

public class FileCounterTask extends RecursiveTask<Long> {

/**
* Current processed file
*/
protected final File mFile;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you define the members as protected
Also I would recommend to avoid Hungarian notation in Java.
Also that is bad practice according to Clean Code principles.
Instead try to give is a good and meaningful name consists from normal English words.

/**
* All documents in folder
*/
protected final AtomicLong mDocumentCount;
/**
* Directories count
*/
protected final AtomicLong mFolderCount;
FileCounterTask(File file, AtomicLong mDocumentCount, AtomicLong mFolderCount) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the constructor has package visibility?
How would you like to use this class?

this.mFile = file;
this.mDocumentCount = new AtomicLong(0);
this.mFolderCount = new AtomicLong(0);
}

/**
*
* @return files number
*/
public AtomicLong documentCount() {
return mDocumentCount;
};

/**
*
* @return directories number
*/
public AtomicLong folderCount() {
return mFolderCount;
}

@Override
protected Long compute() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method deserve to be split up on smaller logical chunks. So you can clearly see the logical structure.

if (mFile.isFile()) {
mDocumentCount.incrementAndGet();
} else {
mFolderCount.incrementAndGet();
}

List<ForkJoinTask<Long>> forkJoinTaskList = new ArrayList<>();
for(File file: Objects.requireNonNull(mFile.listFiles())) {
//the task for each File
forkJoinTaskList.add(
new FileCounterTask(file, mDocumentCount, mFolderCount).fork());
}

long sum = 0;

for (ForkJoinTask<Long> forkJoinTask: forkJoinTaskList) {
sum += forkJoinTask.join();
}

return sum;
}
}