package com.codegym.task.task31.task3111;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
public class SearchFileVisitor extends SimpleFileVisitor<Path> {
private String partOfName = null;
private String partOfContent = null;
private int minSize = -1;
private int maxSize = -1;
private List<Path> foundFiles = new ArrayList<>();
public void setPartOfName(String partOfName) {
this.partOfName = partOfName;
}
public void setPartOfContent(String partOfContent) {
this.partOfContent = partOfContent;
}
public void setMinSize(int minSize) {
this.minSize = minSize;
}
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
public List<Path> getFoundFiles() {
return foundFiles;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
byte[] content = Files.readAllBytes(file); // File size: content.length
//0 0 0 1
if (partOfName == null && partOfContent == null && maxSize == -1 && minSize >= 0 &&
isAboveMinSize(content)) {
foundFiles.add(file);
}
//0 0 1 0
else if (partOfName == null && partOfContent == null && maxSize >= 0 && minSize == -1 &&
isBelowMaxSize(content)) {
foundFiles.add(file);
}
//0 0 1 1
else if (partOfName == null && partOfContent == null && maxSize >= 0 && minSize >= 0 &&
isBelowMaxSize(content) && isAboveMinSize(content)) {
foundFiles.add(file);
}
//0 1 0 0
else if (partOfName == null && partOfContent != null && maxSize == -1 && minSize == -1 &&
containsPartOfContent(content)){
foundFiles.add(file);
}
//0 1 0 1
else if (partOfName == null && partOfContent != null && maxSize == -1 && minSize >= 0 &&
containsPartOfContent(content) && isAboveMinSize(content)){
foundFiles.add(file);
}
//0 1 1 0
else if (partOfName == null && partOfContent != null && maxSize >= 0 && minSize == -1 &&
containsPartOfContent(content) && isBelowMaxSize(content)){
foundFiles.add(file);
}
//0 1 1 1
else if (partOfName == null && partOfContent != null && maxSize >= 0 && minSize >= 0 &&
containsPartOfContent(content) && isBelowMaxSize(content) && isAboveMinSize(content)){
foundFiles.add(file);
}
//1 0 0 0
else if (partOfName != null && partOfContent == null && maxSize == -1 && minSize == -1 &&
containsPartOfName(file)) {
foundFiles.add(file);
}
//1 0 0 1
else if (partOfName != null && partOfContent == null && maxSize == -1 && minSize >= 0 &&
containsPartOfName(file) && isAboveMinSize(content)) {
foundFiles.add(file);
}
//1 0 1 0
else if (partOfName != null && partOfContent == null && maxSize >= 0 && minSize == -1 &&
containsPartOfName(file) && isBelowMaxSize(content)) {
foundFiles.add(file);
}
//1 0 1 1
else if (partOfName != null && partOfContent == null && maxSize >= 0 && minSize >= 0 &&
containsPartOfName(file) && isBelowMaxSize(content) && isAboveMinSize(content)) {
foundFiles.add(file);
}
//1 1 0 0
else if (partOfName != null && partOfContent != null && maxSize == -1 && minSize == -1 &&
containsPartOfName(file) && containsPartOfContent(content)) {
foundFiles.add(file);
}
//1 1 0 1
else if (partOfName != null && partOfContent != null && maxSize == -1 && minSize >= 0 &&
containsPartOfName(file) && containsPartOfContent(content) && isAboveMinSize(content)) {
foundFiles.add(file);
}
//1 1 1 0
else if (partOfName != null && partOfContent != null && maxSize >= 0 && minSize == -1 &&
containsPartOfName(file) && containsPartOfContent(content) && isBelowMaxSize(content)) {
foundFiles.add(file);
}
//1 1 1 1
else if (partOfName != null && partOfContent != null && maxSize >= 0 && minSize >= 0 &&
containsPartOfName(file) && containsPartOfContent(content) && isBelowMaxSize(content) && isAboveMinSize(content)) {
foundFiles.add(file);
}
return super.visitFile(file, attrs);
}
private boolean containsPartOfName(Path file){
return file.getFileName().toString().contains(partOfName);
}
private boolean containsPartOfContent(byte[] content){
return new String(content).contains(partOfContent);
}
private boolean isBelowMaxSize(byte[] content){
return content.length < maxSize;
}
private boolean isAboveMinSize(byte[] content){
return content.length > minSize;
}
}
I managed to solve this task using this abomination...
My question is: Is there a better way of making the code accept any combination of those parameters without doing it the way i did it?I solved it but i'm looking for better solutions
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Murat Can Akpınar
23 July 2020, 16:01
by using boolean variables, i ascertained whether the default values were changed. If they were, then checked the condition. The only way these boolean variables will be false is when the relevant criteria has been specified, and is not satisfied. This way, i was able to formulate it down to a single if statement
+2
Vahan
14 May 2020, 13:24
String strContent = new String(content);
boolean containsName = true;
if (partOfName != null)
containsName = file.getFileName().toString().contains(partOfName);
if (partOfContent != null && containsName)
containsName = strContent.contains(partOfContent);
if (maxSize != 0 && minSize != 0 && containsName)
containsName = (long) content.length < maxSize && (long) content.length > minSize;
else if (maxSize != 0 && containsName)
containsName = (long) content.length < maxSize;
else if (minSize != 0 && containsName)
containsName = (long) content.length > minSize;
if (containsName)
foundFiles.add(file);
+5