1 package com.ranfa.lib.handler;
3 import java.io.FileWriter;
4 import java.io.IOException;
5 import java.nio.file.Paths;
6 import java.text.SimpleDateFormat;
7 import java.util.ArrayList;
9 import java.util.Iterator;
11 import org.slf4j.LoggerFactory;
13 public class CrashReportList <E> extends ArrayList<E> {
15 private static final String EMPTY_LINE_PLACEHOLDER = "{empty}";
17 private final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss.txt");
19 public CrashReportList() {
23 public String generateCrashReport() {
24 StringBuilder builder = new StringBuilder();
25 Iterator<E> itr = super.iterator();
27 throw new IllegalStateException("There is no lines to generate crash report.");
28 while(itr.hasNext()) {
29 String next = itr.next().toString();
30 if(next.equals(EMPTY_LINE_PLACEHOLDER))
31 next.replaceAll(EMPTY_LINE_PLACEHOLDER, "\n");
32 builder.append(next).append("\n");
34 builder.deleteCharAt(builder.length()).deleteCharAt(builder.length());
36 FileWriter writer = new FileWriter(Paths.get(FORMAT.format(new Date())).toFile());
38 } catch (IOException e) {
39 LoggerFactory.getLogger(CrashReportList.class).error("Cannot write crash report.", e);
41 return builder.toString();
44 @SuppressWarnings("unchecked")
45 public void emptyLine() {
46 super.add((E) EMPTY_LINE_PLACEHOLDER);