๐ย ์ค๋ ์ฝ์ ๋ฒ์ : 5. ํ์ ๋ง์ถ๊ธฐ (96~116p)
package fitnesse.wikitext.widgets;
import java.util.regex.*;
public class BoldWidget extends ParentWidget {
public static final String REGEXP = "'''.+?'''";
private static final Pattern pattern = Pattern.compile("'''(.+?)'''",
Pattern.MULTILINE + Pattern.DOTALL);
public BoldWidget(ParentWidget parent, String text) throws Exception {
super(parent);
Matcher match = pattern.matcher(text);
match.find();
addChildWidgets(match.group(1));
}
public String render() throws Exception {
StringBuffer html = new StringBuffer("<b>");
html.append(childHtml()).append("</b>");
return html.toString();
}
}
๋น ํ์ ์๋ก์ด ๊ฐ๋
์ ์์ํ๋ค๋ ์๊ฐ์ ๋จ์public class ReporterConfig {
/**
* ๋ฆฌํฌํฐ ๋ฆฌ์ค๋์ ํด๋์ค ์ด๋ฆ
*/
private String m_className;
/**
* ๋ฆฌํฌํฐ ๋ฆฌ์ค๋์ ์์ฑ */
private List < Property > m_properties = new ArrayList < Property > ();
public void addProperty(Property property) {
m_properties.add(property);
}
}
์ ์ฒ๋ผ ์ฃผ์์ผ๋ก ๋ ์ธ์คํด์ค ๋ณ์๋ฅผ ๋จ์ด๋จ๋ ค ๋์์๋ ์๋๋ค.private static void readPreferences() {
InputStream is = null;
try {
is = new FileInputStream(getPreferencesFile());
setPreferences(new Properties(getPreferences()));
getPreferences().load(is);
} catch (IOException e) {
try {
if (is != null) is.close();
} catch (IOException e1) {}
}
}
public int countTestCases() {
int count = 0;
for (Test each: tests)
count += each.countTestCases();
return count;
}
public class TestSuite implements Test {
static public Test createTest(Class << ? extends TestCase > theClass,
...
}
public static Constructor << ? extends TestCase > getTestConstructor(Class << ? extends TestCase > theClass) throws NoSuchMethodException {
...
}
public static Test warning(final String message) { ...
}
private static String exceptionToString(Throwable t) { ...
}
private String fName;
private Vector < Test > fTests = new Vector < Test > (10);
public TestSuite() {}
public TestSuite(final Class << ? extends TestCase > theClass) { ...
}
public TestSuite(Class << ? extends TestCase > theClass, String name) { ...
}
...............
}
โ ์ค๊ฐ์ ์ธ์คํด์ค ๋ณ์ ์จ๊ฒจ๋์ผ๋ฉด ์ฐพ๊ธฐ ํ๋ค๋คpublic class Assert {
static public void assertTrue(String message, boolean condition) {
if (!condition) fail(message);
}
static public void assertTrue(boolean condition) {
assertTrue(null, condition);
}
static public void assertFalse(String message, boolean condition) {
assertTrue(message, !condition);
}
static public void assertFalse(boolean condition) {
assertFalse(null, condition);
}
...
}
private void measureLine(String line) {
lineCount++;
int lineSize = line.length();
totalChars += lineSize;
lineWidthHistogram.addLine(lineSize, lineCount);
recordWidestLine(lineSize);
}
ํ ๋น(=) ์ฐ์ฐ์ ๊ฐ์กฐํ๊ธฐ ์ํด ์๋ค์ ๊ณต๋ฐฑ ํจ์์ด๋ฆ๊ณผ ์ด์ด์ง๋ ๊ดํธ์๋ ๊ณต๋ฐฑ ์ฃผ์ง ์์ โ ๋ฐ์ ํจ ์ผํ๋ฅผ ๊ฐ์กฐํด ์ธ์๊ฐ ๋ณ๊ฐ๋ผ๋ ์ฌ์ค์ ๋ณด์ฌ์คpublic class Quadratic {
public static double root1(double a, double b, double c) {
double determinant = determinant(a, b, c);
return (-b + Math.sqrt(determinant)) / (2 * a);
}
public static double root2(int a, int b, int c) {
double determinant = determinant(a, b, c);
return (-b - Math.sqrt(determinant)) / (2 * a);
}
private static double determinant(double a, double b, double c) {
return b * b - 4 * a * c;
}
}
๊ณฑ์
์ ์ฐ์ ์์๊ฐ ๊ฐ์ฅ ๋์ โ ์น์ ์ฌ์ด๋ ๊ณต๋ฐฑ ์์ ๋ง์
๊ณผ ๋บ์
์ ์ฐ์ ์์ ๋ฎ์ โ ๊ณต๋ฐฑpublic class FitNesseExpediter implements ResponseSender
{
private Socket socket;
protected long requestParsingTimeLimit;
private long requestProgress;
...
public class CommentWidget extends TextWidget {
public static final String REGEXP = "^#[^\r\n]*(?:(?:\r\n)|\n|\r)?";
public CommentWidget(ParentWidget parent, String text){super(parent, text);}
public String render() throws Exception {return ""; } }
while (dis.read(buf, 0, readBufferSize) != -1)
{};
public class CodeAnalyzer implements JavaFileAnalysis {
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;
}
private 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 void 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 / 2)
return width;
}
throw new Error("Cannot get here");
}
private int lineCountForWidth(int width) {
return lineWidthHistogram.getLinesforWidth(width).size();
}
private Integer[] getSortedWidths() {
Set < Integer > widths = lineWidthHistogram.getWidths();
Integer[] sortedWidths = (widths.toArray(new Integer[0]));
Arrays.sort(sortedWidths);
return sortedWidths;
}
}