fix: 去除重复代码

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
502647092 2016-09-19 15:49:37 +08:00
parent 42dd115075
commit 975c50a32a
6 changed files with 133 additions and 258 deletions

View File

@ -1,8 +1,5 @@
package pw.yumc.YumCore.config;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
@ -39,22 +36,6 @@ public class CommentConfig extends AbstractConfig {
return array;
}
@Override
public void load(final Reader reader) throws IOException, InvalidConfigurationException {
final BufferedReader input = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
final StringBuilder builder = new StringBuilder();
try {
String line;
while ((line = input.readLine()) != null) {
builder.append(line);
builder.append(newLine);
}
} finally {
input.close();
}
loadFromString(builder.toString());
}
@Override
public void loadFromString(final String contents) throws InvalidConfigurationException {
final String[] parts = contents.split(newLine);

View File

@ -0,0 +1,84 @@
package pw.yumc.YumCore.paste;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @since 2016919 3:40:49
* @author
*/
public class PasteContent {
private final static String errN = "异常名称: %s";
private final static String errM = "异常说明: %s";
private final static String errInfo = "简易错误信息如下:";
private final static String errStackTrace = " 位于 %s.%s(%s:%s)";
private final List<String> TEXT = new ArrayList<>();
/**
*
*
* @param file
*
* @throws IOException
* IO
*/
public void addFile(final File file) throws IOException {
if (file == null) {
throw new IllegalArgumentException("文件不得为Null!");
}
addLines(Files.readAllLines(file.toPath()));
}
/**
*
*
* @param str
*
*/
public void addLine(final String str) {
this.TEXT.add(str);
}
/**
*
*
* @param str
*
*/
public void addLines(final List<String> str) {
this.TEXT.addAll(str);
}
/**
*
*
* @param e
*
*/
public void addThrowable(final Throwable e) {
Throwable temp = e;
while (temp.getCause() != null) {
temp = temp.getCause();
}
TEXT.add(String.format(errN, e.getClass().getName()));
TEXT.add(String.format(errM, e.getMessage()));
TEXT.add(errInfo);
for (final StackTraceElement ste : e.getStackTrace()) {
TEXT.add(String.format(errStackTrace, ste.getClassName(), ste.getMethodName(), ste.getFileName(), ste.getLineNumber() < 0 ? "未知" : ste.getLineNumber()));
}
}
@Override
public String toString() {
final StringBuilder text = new StringBuilder();
for (final String str : TEXT) {
text.append(str + '\n');
}
return text.toString();
}
}

View File

@ -0,0 +1,25 @@
package pw.yumc.YumCore.paste;
/**
*
*
* @since 2016918 7:00:15
* @author
*/
public enum PasteFormat {
JAVA("java"),
JAVASCRIPT("javascript"),
HTML("html"),
YAML("yaml");
String format;
private PasteFormat(final String format) {
this.format = format;
}
@Override
public String toString() {
return format;
}
}

View File

@ -1,30 +1,25 @@
package pw.yumc.YumCore.paste;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
public class PasteXcode {
private final String POST_URL = "http://paste.xcode.ro/";
public static void main(final String[] args) {
final PasteXcode p = new PasteXcode();
final Paste paste = new Paste();
final PasteContent paste = new PasteContent();
paste.addLine("异常提交测试!");
paste.addThrowable(new Throwable());
System.out.println(p.post(paste));;
}
public String post(final PasteXcode.Paste content) {
return post("YumCore", PasteXcode.Format.JAVA, content);
public String post(final PasteContent content) {
return post("YumCore", PasteFormat.JAVA, content);
}
public String post(final String name, final PasteXcode.Format format, final PasteXcode.Paste content) {
public String post(final String name, final PasteFormat format, final PasteContent content) {
String result = "Failed to post!";
try {
final HttpURLConnection connection = (HttpURLConnection) new URL(POST_URL).openConnection();
@ -47,66 +42,4 @@ public class PasteXcode {
}
return result;
}
public enum Format {
JAVA("java"),
JAVASCRIPT("javascript"),
HTML("html");
String format;
private Format(final String format) {
this.format = format;
}
@Override
public String toString() {
return format;
}
}
public static class Paste {
private final static String errN = "异常名称: %s";
private final static String errM = "异常说明: %s";
private final static String errInfo = "简易错误信息如下:";
private final static String errStackTrace = " 位于 %s.%s(%s:%s)";
private final List<String> TEXT = new ArrayList<>();
public void addFile(final File file) throws IOException {
if (file == null) {
throw new IllegalArgumentException("文件不得为Null!");
}
addLines(Files.readAllLines(file.toPath()));
}
public void addLine(final String str) {
this.TEXT.add(str);
}
public void addLines(final List<String> str) {
this.TEXT.addAll(str);
}
public void addThrowable(final Throwable e) {
Throwable temp = e;
while (temp.getCause() != null) {
temp = temp.getCause();
}
TEXT.add(String.format(errN, e.getClass().getName()));
TEXT.add(String.format(errM, e.getMessage()));
TEXT.add(errInfo);
for (final StackTraceElement ste : e.getStackTrace()) {
TEXT.add(String.format(errStackTrace, ste.getClassName(), ste.getMethodName(), ste.getFileName(), ste.getLineNumber() < 0 ? "未知" : ste.getLineNumber()));
}
}
@Override
public String toString() {
final StringBuilder text = new StringBuilder();
for (final String str : TEXT) {
text.append(str + '\n');
}
return text.toString();
}
}
}

View File

@ -1,16 +1,11 @@
package pw.yumc.YumCore.paste;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
public class Pastebin {
private final String POST_URL = "http://pastebin.com/api/api_post.php";
@ -26,17 +21,17 @@ public class Pastebin {
public static void main(final String[] args) {
final Pastebin p = new Pastebin();
final Paste paste = new Paste();
final PasteContent paste = new PasteContent();
paste.addLine("异常提交测试!");
paste.addThrowable(new Throwable());
System.out.println(p.post(paste));;
}
public String post(final Pastebin.Paste content) {
return post("", Pastebin.Format.JAVA, Pastebin.Private.UNLISTED, content);
public String post(final PasteContent content) {
return post("", PasteFormat.JAVA, Pastebin.Private.UNLISTED, content);
}
public String post(final String name, final Pastebin.Format format, final Pastebin.Private level, final Pastebin.Paste content) {
public String post(final String name, final PasteFormat format, final Pastebin.Private level, final PasteContent content) {
String result = "Failed to post!";
try {
final HttpURLConnection connection = (HttpURLConnection) new URL(POST_URL).openConnection();
@ -82,67 +77,6 @@ public class Pastebin {
return result;
}
public enum Format {
JAVA("java"),
YAML("yaml");
String format;
private Format(final String format) {
this.format = format;
}
@Override
public String toString() {
return format;
}
}
public static class Paste {
private final static String errN = "异常名称: %s";
private final static String errM = "异常说明: %s";
private final static String errInfo = "简易错误信息如下:";
private final static String errStackTrace = " 位于 %s.%s(%s:%s)";
private final List<String> TEXT = new ArrayList<>();
public void addFile(final File file) throws IOException {
if (file == null) {
throw new IllegalArgumentException("文件不得为Null!");
}
addLines(Files.readAllLines(file.toPath()));
}
public void addLine(final String str) {
this.TEXT.add(str);
}
public void addLines(final List<String> str) {
this.TEXT.addAll(str);
}
public void addThrowable(final Throwable e) {
Throwable temp = e;
while (temp.getCause() != null) {
temp = temp.getCause();
}
TEXT.add(String.format(errN, e.getClass().getName()));
TEXT.add(String.format(errM, e.getMessage()));
TEXT.add(errInfo);
for (final StackTraceElement ste : e.getStackTrace()) {
TEXT.add(String.format(errStackTrace, ste.getClassName(), ste.getMethodName(), ste.getFileName(), ste.getLineNumber() < 0 ? "未知" : ste.getLineNumber()));
}
}
@Override
public String toString() {
final StringBuilder text = new StringBuilder();
for (final String str : TEXT) {
text.append(str + '\n');
}
return text.toString();
}
}
public enum Private {
PUBLIC(0),
UNLISTED(1),

View File

@ -1,16 +1,10 @@
package pw.yumc.YumCore.paste;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
@ -28,11 +22,21 @@ public class StickyNotes {
public static void main(final String[] args) {
final StickyNotes p = new StickyNotes();
final Paste paste = new Paste();
final PasteContent paste = new PasteContent();
paste.addLine("异常提交测试!");
paste.addThrowable(new Throwable());
System.out.println(p.post(StickyNotes.Expire.HalfHour, paste));
;
System.out.println(p.post(StickyNotes.Expire.HalfHour, paste));;
}
/**
*
*
* @param content
*
* @return
*/
public String post(final PasteContent content) {
return post("YumCore-" + System.currentTimeMillis(), PasteFormat.JAVA, StickyNotes.Expire.Never, content);
}
/**
@ -44,19 +48,8 @@ public class StickyNotes {
*
* @return
*/
public String post(final StickyNotes.Expire expire, final StickyNotes.Paste content) {
return post("YumCore-" + System.currentTimeMillis(), StickyNotes.Format.JAVA, expire, content);
}
/**
*
*
* @param content
*
* @return
*/
public String post(final StickyNotes.Paste content) {
return post("YumCore-" + System.currentTimeMillis(), StickyNotes.Format.JAVA, StickyNotes.Expire.Never, content);
public String post(final StickyNotes.Expire expire, final PasteContent content) {
return post("YumCore-" + System.currentTimeMillis(), PasteFormat.JAVA, expire, content);
}
/**
@ -72,7 +65,7 @@ public class StickyNotes {
*
* @return
*/
public String post(final String title, final StickyNotes.Format format, final StickyNotes.Expire expire, final StickyNotes.Paste content) {
public String post(final String title, final PasteFormat format, final StickyNotes.Expire expire, final PasteContent content) {
return post(title, format.toString(), expire.getExpire(), content.toString());
}
@ -149,79 +142,4 @@ public class StickyNotes {
return expire;
}
}
/**
*
*
* @since 2016918 7:00:15
* @author
*/
public enum Format {
JAVA("java"),
JAVASCRIPT("javascript"),
HTML("html"),
YAML("yaml");
String format;
private Format(final String format) {
this.format = format;
}
@Override
public String toString() {
return format;
}
}
/**
*
*
* @since 2016918 7:00:21
* @author
*/
public static class Paste {
private final static String errN = "异常名称: %s";
private final static String errM = "异常说明: %s";
private final static String errInfo = "简易错误信息如下:";
private final static String errStackTrace = " 位于 %s.%s(%s:%s)";
private final List<String> TEXT = new ArrayList<>();
public void addFile(final File file) throws IOException {
if (file == null) {
throw new IllegalArgumentException("文件不得为Null!");
}
addLines(Files.readAllLines(file.toPath(), Charset.forName("UTF-8")));
}
public void addLine(final String str) {
this.TEXT.add(str);
}
public void addLines(final List<String> str) {
this.TEXT.addAll(str);
}
public void addThrowable(final Throwable e) {
Throwable temp = e;
while (temp.getCause() != null) {
temp = temp.getCause();
}
TEXT.add(String.format(errN, e.getClass().getName()));
TEXT.add(String.format(errM, e.getMessage()));
TEXT.add(errInfo);
for (final StackTraceElement ste : e.getStackTrace()) {
TEXT.add(String.format(errStackTrace, ste.getClassName(), ste.getMethodName(), ste.getFileName(), ste.getLineNumber() < 0 ? "未知" : ste.getLineNumber()));
}
}
@Override
public String toString() {
final StringBuilder text = new StringBuilder();
for (final String str : TEXT) {
text.append(str + '\n');
}
return text.toString();
}
}
}