Skip to content

Commit 0c1bb2c

Browse files
committed
- adds serialization to text file
1 parent b9d571c commit 0c1bb2c

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

typesummary/app/src/main/java/typesummary/App.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.reflect.ClassPath;
2020

2121
public class App {
22+
// -o absolutepathpath.txt to output to text file instead of console
2223
public static void main(String[] args) throws Exception {
2324
final String[] mNames = { //those are default java language methods that every object will have
2425
"equals",
@@ -30,13 +31,19 @@ public static void main(String[] args) throws Exception {
3031
"wait"
3132
};
3233
methodsNameToSkip = Arrays.asList(mNames);
33-
final ILogWriter writer = new ConsoleLogWriter();
34+
final Boolean isFileMode = args.length > 0 && args[0].equals("-o");
35+
36+
final ILogWriter writer = isFileMode ? new TextFileLogWriter(args[1]) : new ConsoleLogWriter();
3437

3538

3639
final List<String> classNames = getOrderedClassNames();
3740
serializeEnums(writer, classNames);
3841
serializeClasses(writer, classNames);
3942
serializeInterfaces(writer, classNames);
43+
44+
if(isFileMode) {
45+
((TextFileLogWriter)writer).flush();
46+
}
4047
}
4148
private static void serializeEnums(final ILogWriter writer, final List<String> classNames) throws Exception {
4249
for (String c : classNames) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package typesummary;
2+
3+
import java.lang.StringBuilder;
4+
import com.google.common.base.Charsets;
5+
6+
import com.google.common.io.Files;
7+
import java.io.File;
8+
import java.io.IOException;
9+
10+
public class TextFileLogWriter implements ILogWriter {
11+
private String filePath;
12+
private StringBuilder builder = new StringBuilder();
13+
public TextFileLogWriter(final String filePath) {
14+
this.filePath = filePath;
15+
}
16+
public void write(final String content) {
17+
write(content, 0);
18+
}
19+
public void write(final String content, final int indent) {
20+
String indentTxt = "";
21+
for(int i = 0; i <= indent; i++) {
22+
indentTxt += " ";
23+
}
24+
this.builder.append(indentTxt + content + "\r\n");
25+
}
26+
public void flush() throws IOException {
27+
File file = new File(this.filePath);
28+
Files.write(builder, file, Charsets.UTF_8);
29+
}
30+
}

0 commit comments

Comments
 (0)