Skip to content

Commit

Permalink
파일을 찾아다니는 Travler 와 파일을 이용하는 Processer 로 관심에 따른 코드 분리.
Browse files Browse the repository at this point in the history
Processer 는 우선 문자열을 출력하는 FilePathPrinter 하나 만들어 둠.
그 외에 더이상 Main 으로 실행하고 있지 않아서 Main 클래스는 속을 비워 둠.
  • Loading branch information
sangpire committed May 10, 2017
1 parent 7c55b3d commit 8fc77f4
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 15 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'application'

compileJava.options.encoding = 'UTF-8'
mainClassName = "sangpire.findup.Main"

jar {
baseName = 'findup'
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/sangpire/findup/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

public class Main {
public static void main(String[] args) {
File rootDir = new File("D:\\");
Travler travler = new Travler();
travler.visit(rootDir);
throw new RuntimeException("Under Construction");
}
}
25 changes: 25 additions & 0 deletions src/main/java/sangpire/findup/Processer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sangpire.findup;

import java.io.File;

/**
* 뭔가 기가막힌 이름이 없을까?
*/
public interface Processer {

/**
* 작업 시작했어요. 이제 파일 찾으러 갈 꺼예요.
*/
void start();

/**
* 여기 파일이요.
* @param file 방금 찾은 파일.
*/
void put(File file);

/**
* 다 찾아 줬어요.
*/
void finish();
}
30 changes: 20 additions & 10 deletions src/main/java/sangpire/findup/Travler.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
package sangpire.findup;

import java.io.File;
import java.util.Collections;

/**
* 파일을 모조리 찾아내는 폴더 여행자.
*/
public class Travler {

private Processer processer;

public Processer getProcesser() {
return processer;
}

public void setProcesser(Processer processer) {
this.processer = processer;
}

public void visit(File dir) {
processer.start();
visit(0, dir);
processer.finish();
}

void visit(Integer depth, File dir) {
private void visit(Integer depth, File dir) {
if (depth > 1) {
return;
}
String margin = String.join("", Collections.nCopies(depth, " "));

File[] childFileList = dir.listFiles();
if (childFileList == null) {
String fileMetaInfos =String.format("%sEmpty!?\n", margin);
System.out.print(fileMetaInfos);
// No Regular Files.
return;
}
for(File file: childFileList) {

String fileMetaInfos =String.format("%s%s - %s\n", margin, file.isDirectory() ? "d" : "-" , file.getAbsolutePath());
System.out.print(fileMetaInfos);
if (file.exists() && file.isDirectory()) {
visit(depth + 1, file);
if (file.exists()) {
processer.put(file);
if (file.isDirectory()) {
visit(depth + 1, file);
}
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/sangpire/findup/processer/FilePathPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package sangpire.findup.processer;

import sangpire.findup.Processer;

import java.io.File;

public class FilePathPrinter implements Processer {

@Override
public void start() {
System.out.println("START File Path Printing...");
}

@Override
public void put(File file) {
String fileMetaInfos =String.format("[%s] %s\n", file.isDirectory() ? "d" : "-" , file.getAbsolutePath());
System.out.print(fileMetaInfos);
}

@Override
public void finish() {
System.out.println("FINISHED File Path Printing...");
}
}
4 changes: 4 additions & 0 deletions src/test/java/sangpire/findup/TravlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import sangpire.findup.processer.FilePathPrinter;

import java.io.File;

Expand All @@ -20,6 +21,9 @@ public void tearDown() throws Exception {
public void lookupDir() throws Exception {
File rootDir = new File("D:\\");
Travler travler = new Travler();

travler.setProcesser(new FilePathPrinter());

travler.visit(rootDir);
}

Expand Down

0 comments on commit 8fc77f4

Please sign in to comment.