Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- import java.io.*;
- public class FileCriteria {
- interface Criteria {
- boolean isValid(File file, String value, String op);
- }
- class SizeCheck implements Criteria{
- public boolean isValid(File file, String value, String op){
- switch(op){
- case ">":
- return file.length() > Integer.valueOf(value) * 1024 * 1024;
- case "<":
- return file.length() < Integer.valueOf(value) * 1024 * 1024;
- case "=":
- return file.length() < Integer.valueOf(value) * 1024 * 1024;
- }
- return true;
- }
- }
- class ExtensionCheck implements Criteria{
- public boolean isValid(File file, String value, String op){
- return file.getName().endsWith(value);
- }
- }
- class Factory{
- private Map<String, Criteria> criteria = new HashMap<>();
- public Factory(){
- criteria.put("size", new SizeCheck());
- criteria.put("extension", new ExtensionCheck());
- }
- public Criteria create(String s){
- return criteria.get(s);
- }
- }
- public void listFiles(String path, List<OperationClass> ops){
- Factory factory = new Factory();
- File folder = new File(path);
- File[] files = folder.listFiles();
- for(File file : files){
- if(file.isFile()){
- boolean isValid = true;
- for(OperationClass op : ops){
- if(!factory.create(op.criteria).isValid(file, op.value, op.op)){
- isValid = false;
- }
- }
- if(isValid) System.out.println(file.getName());
- } else if(file.isDirectory()){
- listFiles(file.getAbsolutePath(), ops);
- }
- }
- }
- public class OperationClass {
- String criteria;
- String value;
- String op;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement