클린코드
카테고리 미분류
지은이 로버트 C. 마틴 (케이앤피IT, 2010년)
상세보기


import java.io.BufferedReader;
import java.io.File;
import java.util.List;
import java.util.Set;

public class CodeAnalyzer {
	private int lineCount;
	private int maxLineWidth;
	private int widestLineNumber;
	private LineWidthHistogram lineWidthHistogram;
	private int totalChars;

	public CodeAnalyzer() {
		lineWidthHistogram = new LineWidthHistogram();
	}
	
	public static List<File> findJavaFiles(File parentDirectory) {
		List<File> files = new ArrayList<File>();
		findJavaFiles(parentDirectory, files);
		return files;
	}
	
	public static void findJavaFiles(File parentDirectory, List<File> files) {
		for(File file : parentDirectory.listFiles()) {
			if (file.getName().endsWith(".java"))
				files.add(file);
			else if (file.isDirectory())
				findJavaFiles(file, files);
		}
	}
	
	public vvoid analyzeFile(File javaFile) throws Exception {
		BufferedReader br = new BufferedReader(new FileReader(javaFile));
		String line;
		while ((line = br.readLine()) != null)
			measureLine(line);
	}
	
	private void measureLine(String line) {
		lineCount++;
		int lineSize = line.length();
		totalChars += lineSize;
		lineWidthHistogram.addLine(lineSize, lineCount);
		recordWidestLine(lineSize);
	}
	
	private void recordWidestLine(int lineSize) {
		if (lineSize > maxLineWidth) {
			maxLineWidth = lineSize;
			widestLineNumber = lineCount;
		}
	}
	
	public int getLineCount() {
		return lineCount;
	}
	
	public int getMaxLineWidth() {
		return maxLineWidth;
	}
	
	public int getWidestLineNumber() {
		return widestLineNumber;
	}
	
	public LineWidthHistogram getLineWidthHistogram() {
		return lineWidthHistogram;
	}
	
	public double getMeanLineWidth() {
		return (double)totalChars/lineCount;
	}
	
	public int getMedianLineWidth() {
		Integer[] sortedWidths = getSortedWidths();
		int cumulativeLineCount = 0;
		for (int width : sortedWidths) {
			cumulativeLineCount += lineCountForWidth(width);
			if (cumulativeLineCount > lineCount)
				return width;			
		}
		throws new Error("Cannot get here");
	}
	
	private int lineCountForWidth(int width) {
		return lineWidthHistogram.getLinesforWidth(width).size();
	}
	
	private Integer[] getSortedWidth() {
		Set<Integer> widths = lineWidthHistogram.getWidths();
		Integer[] sortedWidths = (widths.toArray(new Integer[0]));
		Arrays.sort(sortedWidths);
		return sortedWidths;
	}
	
}


클린코드
카테고리 미분류
지은이 로버트 C. 마틴 (케이앤피IT, 2010년)
상세보기


public class SetupTeardownIncluder {

	private PageData pageData;
	private boolean isSuite;
	private WikiPage testPage;
	private StringBuffer	newPageContent;
	private PageCrawler pageCrawler;
	
	public static String render(PageData pageData) throws Exception {
		return render(pageData, false);
	}
	
	public static String render(PageData pageData, boolean isSuite) throws Exception {
		return new SetupTeardownIncluder(pageData).render(isSuite);
	}
	
	private SetupTeardownIncluder(PageData pageData) {
		this.pageData = pageData;
		testPage = pageData.getWikiPage();
		pageCrawler = testPage.getPageCrawler();
		newPageContent = new StringBuffer();
	}
	
	private String render(boolean isSuite) throws Exception {
		this.isSuite = isSuite;
		if (isTestPage())
			includeSetupAndTeardownPages();
		return pageData.getHtml();
	}
	
	private boolean isTestPage() throws Exception {
		return pageData.hasAttribute("Test");
	}
	
	private void includeSetupAndTeardownPages() throws Exception {
		includeSetupPages();
		includePageContent();
		includeTeardownPages();
		updatePageContent();
	}
	
	private void includeSuiteSetupPage() throws Exception {
		include(SuiteResponder.SUITE_SETUP_NAME, "-setup");		
	}
	
	private void includeSetupPage() throws Exception {
		include("Setup", "-setup");		
	}
	
	private void includePageContent() throws Exception {
		newPageContent.append(pageData.getContent());
	}
	
	private void includeTeardownPages() throws Excepton {
		includeTeardownPage();
		if (isSuite)
			includeSuiteTeardownPage();
	}
	
	private void includeTeardownPage() throws Exception {
		include("TearDown", "-teardown");
	}
	
	private void includeSuiteTeardownPage() throws Exception {
		include(SuiteResponder.SUITE_TEARDOWN_NAME, "-teardown");
	}
	
	private void updatePageContent() throws Exception {
		pageData.setContent(newPageContent.toString());
	}
	
	private void include(String pageName, String arg) throws Exception {
		WikiPage inheritedPage = findInheritedPage(pageName);
		if ( inheritedPage != null ) {
			String pagePathName = getPathNameForPage(inheritedPage);
			buildIncludeDirective(pagePathName, arg);
		}
	}
	
	private WikiPage findInheritedPage(String pageName) throws Exception {
		return PageCrawlerImpl.getInheritedPage(pageName, testPage);
	}
	
	private String getPathNameForPage(WikiPage page) throws Exception {
		WikiPagePath pagePath = pageCrawler.getFullPath(page);
		return PathParser.render(pagePath);
	}
	
	private void buildIncludeDirective(String pagePathName, String arg) {
		newPageContent
			.append("\n!include ")
			.append(arg)
			.append(" .")
			.append(pagePathName)
			.append("\n");
	}
}

+ Recent posts