Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5.  
  6. public class FileCriteria {
  7.  
  8.     interface Criteria {
  9.         boolean isValid(File file, String value, String op);
  10.     }
  11.     class SizeCheck implements Criteria{
  12.         public boolean isValid(File file, String value, String op){
  13.             switch(op){
  14.                 case ">":
  15.                     return file.length() > Integer.valueOf(value) * 1024 * 1024;
  16.                 case "<":
  17.                     return file.length() < Integer.valueOf(value) * 1024 * 1024;
  18.                 case "=":
  19.                     return file.length() < Integer.valueOf(value) * 1024 * 1024;
  20.             }
  21.             return true;
  22.         }
  23.     }
  24.     class ExtensionCheck implements Criteria{
  25.         public boolean isValid(File file, String value, String op){
  26.             return file.getName().endsWith(value);
  27.         }
  28.     }
  29.     class Factory{
  30.         private Map<String, Criteria> criteria = new HashMap<>();
  31.         public Factory(){
  32.             criteria.put("size", new SizeCheck());
  33.             criteria.put("extension", new ExtensionCheck());
  34.         }
  35.         public Criteria create(String s){
  36.             return criteria.get(s);
  37.         }
  38.     }
  39.     public void listFiles(String path, List<OperationClass> ops){
  40.         Factory factory = new Factory();
  41.         File folder = new File(path);
  42.         File[] files = folder.listFiles();
  43.         for(File file : files){
  44.             if(file.isFile()){
  45.                 boolean isValid = true;
  46.                 for(OperationClass op : ops){
  47.                     if(!factory.create(op.criteria).isValid(file, op.value, op.op)){
  48.                         isValid = false;
  49.                     }
  50.                 }
  51.                 if(isValid) System.out.println(file.getName());
  52.             } else if(file.isDirectory()){
  53.                 listFiles(file.getAbsolutePath(), ops);
  54.             }
  55.         }
  56.     }
  57.  
  58.     public class OperationClass {
  59.         String criteria;
  60.         String value;
  61.         String op;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement