0

「チェッカー」FolderScanのリストを取得する新しいコンストラクターを作成する必要があります。そして、これらすべての「チェッカー」は常に(真を返すだけの新しいチェッカーリストを作成する必要があります)。 しかし、問題は、これをどのように行うかがわからず、プログラムの構造を分解しないことです。return true

コード(FolderScanおよび各チェーカー):

class FolderScan implements Runnable {

    FolderScan(String path, BlockingQueue<File> queue, CountDownLatch latch,
            File endOfWorkFile) {
        this.path = path;
        this.queue = queue;
        this.latch = latch;
        this.endOfWorkFile = endOfWorkFile;

        checkers = new ArrayList<Checker>(Arrays.asList(
                new ExtentionChecking(),  new ProbeContentTypeCheking(), 
                new EncodingChecking() ));
    }

        @Override
public void run() {
    try {
        findFiles(path);
        queue.put(endOfWorkFile);
        latch.countDown();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

    private void findFiles(String path) {

    try {
        File root = new File(path);
        File[] list = root.listFiles();
        for (File currentFile : list) {
            boolean checksPassed = true;
            if (currentFile.isDirectory()) {
                findFiles(currentFile.getAbsolutePath());
            } else {
                for (Checker currentChecker : checkers) {
                    if (!currentChecker.check(currentFile)) {
                        checksPassed = false;
                        break;
                    }
                }

                if (checksPassed) {
                    queue.put(currentFile);
                }
            }
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }


    private String path;
    private BlockingQueue<File> queue;
    private CountDownLatch latch;
    private File endOfWorkFile;
    private List<Checker> checkers;
}

class ExtentionChecking implements Checker {

    @Override
    public boolean check(File currentFile) {
        fileName = currentFile.getName().toLowerCase();
        Set<String> extensions = new HashSet<String>(Arrays.asList(".txt",
                ".pdf", ".doc", ".docx", ".html", ".htm", ".xml", ".djvu",
                ".djv", ".rar", ".rtf", ".tmp"));

        if (extensions.contains(fileName.substring(fileName.lastIndexOf(".")))) {
            return true;
        }

        return false;
    }

    private String fileName;
}

class EncodingChecking implements Checker {

    @Override
    public boolean check(File currentFile) {
        return detectEncoding(currentFile);
    }

    public static boolean detectEncoding(File file) {
        detector = new CharsetDetector();

        // validate input
        if (null == file) {
            throw new IllegalArgumentException("input file can't be null");
        }
        if (file.isDirectory()) {
            throw new IllegalArgumentException(
                    "input file refers to a directory");
        }

        // read input file
        byte[] buffer;
        try {
            buffer = readUTFHeaderBytes(file);
        } catch (IOException e) {
            throw new IllegalArgumentException(
                    "Can't read input file, error = " + e.getLocalizedMessage());
        }

        if(detector.setText(buffer) != null){
            return true;
        }

        return false;
    }

    private static byte[] readUTFHeaderBytes(File input) throws IOException {
        // read data
        FileInputStream fileInputStream = new FileInputStream(input);
        try {
            byte firstBytes[] = new byte[50];
            int count = fileInputStream.read(firstBytes);
            if (count < 5) {
                throw new IOException("Poor file!");
            }
            return firstBytes;
        } finally {
            fileInputStream.close();
        }
    }


    private static CharsetDetector detector;
}



class ProbeContentTypeCheking implements Checker {

    @Override
    public boolean check(File currentFile) {
        String mimeType = null;
        try {
            Path path = Paths.get(currentFile.getAbsolutePath());
            byte[] data = Files.readAllBytes(path);
            MagicMatch match = Magic.getMagicMatch(data);
            mimeType = match.getMimeType();
        } catch (MagicParseException | MagicMatchNotFoundException
                | MagicException | IOException e) {
            e.printStackTrace();
        }

        if (null != mimeType) {
            return true;
        }

        return false;
    }
}

質問:

  • このコードをどのようにリファクタリングしますか?これが可能にnew AllwaysPassesBlocker()なり、すべてのChecersがtrueを返した後ですか?
4

1 に答える 1

1

常にtrueを返すチェッカーは

class UncriticalChecker implements Checker {
    @Override
    public boolean check(File currentFile) {
        return true;
    }
}

ただし、そのようなチェッカーをチェッカーのリストに追加しても意味がありません。リストは空のままにしておくとよいでしょう。

FolderScanのコンストラクターでチェッカーを構築する必要がある理由がよくわかりません。それらを引数としてコンストラクターに渡す方が自然なようです。

FolderScan(String path, BlockingQueue<File> queue, CountDownLatch latch,
        File endOfWorkFile, List<Checker> checkers) {
    this.path = path;
    this.queue = queue;
    this.latch = latch;
    this.endOfWorkFile = endOfWorkFile;
    this.checkers = checkers;
}

次に、FolderScanを初期化するときに、チェッカーを渡します

List<Checker> checkers = new ArrayList<Checker>(Arrays.asList(
            new ExtentionChecking(),  new ProbeContentTypeCheking(), 
            new EncodingChecking() ));
FolderScan folderScan = 
    new FolderScan(path, queue, latch, endOfWorkFile, checkers);

または、すべてのファイルを返すFolderScanを作成する場合は、空のリストを渡します。

FolderScan folderScan =
    new FolderScan(path, queue, latch, endOfWorkFile, Collections.emptyList());

編集:あなたがクラスをテストしたいと思っていることを理解しました。次に、UncriticalCheckerは理にかなっています。常にyesと表示されるチェッカーを使用してコードをテストする場合は、コンストラクターに渡します。

List<Checker> checkers = Collections.singletonList(new UncriticalChecker());
FolderScan folderScan = 
    new FolderScan(path, queue, latch, endOfWorkFile, checkers);
于 2013-03-14T21:07:26.443 に答える