Package refactoring
This commit is contained in:
parent
6b74f1711f
commit
ce184a16bf
22 changed files with 285 additions and 193 deletions
|
|
@ -1,4 +1,4 @@
|
|||
package org.zhdev;
|
||||
package org.zhdev.varioutil;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package org.zhdev.language;
|
||||
package org.zhdev.varioutil.language;
|
||||
|
||||
import org.zhdev.util.StringUtils;
|
||||
import org.zhdev.varioutil.util.StringUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
|
|
@ -15,7 +15,7 @@ public class Language {
|
|||
return sectionKey + ':' + phraseKey;
|
||||
}
|
||||
|
||||
String phrase = section.getOrDefault(phraseKey, phraseKey);
|
||||
String phrase = section.get(phraseKey);
|
||||
if (phrase == null) {
|
||||
Map<String, Object> paramsMap = new LinkedHashMap<>(params.length / 2);
|
||||
Object replacement = null;
|
||||
|
|
@ -23,7 +23,7 @@ public class Language {
|
|||
if (i % 2 == 0) {
|
||||
replacement = params[i];
|
||||
} else {
|
||||
paramsMap.put(String.valueOf(params[i]), replacement);
|
||||
paramsMap.put(String.valueOf(replacement), params[i]);
|
||||
}
|
||||
}
|
||||
return sectionKey + ':' + phraseKey + ' ' + paramsMap;
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package org.zhdev.reflection;
|
||||
package org.zhdev.varioutil.reflection;
|
||||
|
||||
import org.zhdev.util.ReflectionUtils;
|
||||
import org.zhdev.varioutil.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package org.zhdev.reflection;
|
||||
package org.zhdev.varioutil.reflection;
|
||||
|
||||
import org.zhdev.util.ReflectionUtils;
|
||||
import org.zhdev.varioutil.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.zhdev.util;
|
||||
package org.zhdev.varioutil.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.Function;
|
||||
|
|
@ -33,27 +33,27 @@ public class ArrayUtils {
|
|||
}
|
||||
|
||||
public static <T> Byte[] mapToByte(T[] array, IntFunction<Byte[]> arrayConstructor) {
|
||||
return map(array, arrayConstructor, NumberUtils::parseByte);
|
||||
return map(array, arrayConstructor, StringUtils::parseByte);
|
||||
}
|
||||
|
||||
public static <T> Short[] mapToShort(T[] array, IntFunction<Short[]> arrayConstructor) {
|
||||
return map(array, arrayConstructor, NumberUtils::parseShort);
|
||||
return map(array, arrayConstructor, StringUtils::parseShort);
|
||||
}
|
||||
|
||||
public static <T> Integer[] mapToInteger(T[] array, IntFunction<Integer[]> arrayConstructor) {
|
||||
return map(array, arrayConstructor, NumberUtils::parseInt);
|
||||
return map(array, arrayConstructor, StringUtils::parseInt);
|
||||
}
|
||||
|
||||
public static <T> Long[] mapToLong(T[] array, IntFunction<Long[]> arrayConstructor) {
|
||||
return map(array, arrayConstructor, NumberUtils::parseLong);
|
||||
return map(array, arrayConstructor, StringUtils::parseLong);
|
||||
}
|
||||
|
||||
public static <T> Float[] mapToFloat(T[] array, IntFunction<Float[]> arrayConstructor) {
|
||||
return map(array, arrayConstructor, NumberUtils::parseFloat);
|
||||
return map(array, arrayConstructor, StringUtils::parseFloat);
|
||||
}
|
||||
|
||||
public static <T> Double[] mapToDouble(T[] array, IntFunction<Double[]> arrayConstructor) {
|
||||
return map(array, arrayConstructor, NumberUtils::parseDouble);
|
||||
return map(array, arrayConstructor, StringUtils::parseDouble);
|
||||
}
|
||||
|
||||
public static <C extends Collection<R>, T, R> C mapAndToCollection(T[] array, IntFunction<C> collectionConstructor, Function<T, R> function) {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.zhdev.util;
|
||||
package org.zhdev.varioutil.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
|
@ -34,27 +34,27 @@ public class CollectionUtils {
|
|||
}
|
||||
|
||||
public static <C extends Collection<Byte>, T> C mapToByte(Collection<T> collection, IntFunction<C> collectionConstructor) {
|
||||
return map(collection, collectionConstructor, NumberUtils::parseByte);
|
||||
return map(collection, collectionConstructor, StringUtils::parseByte);
|
||||
}
|
||||
|
||||
public static <C extends Collection<Short>, T> C mapToShort(Collection<T> collection, IntFunction<C> collectionConstructor) {
|
||||
return map(collection, collectionConstructor, NumberUtils::parseShort);
|
||||
return map(collection, collectionConstructor, StringUtils::parseShort);
|
||||
}
|
||||
|
||||
public static <C extends Collection<Integer>, T> C mapToInteger(Collection<T> collection, IntFunction<C> collectionConstructor) {
|
||||
return map(collection, collectionConstructor, NumberUtils::parseInt);
|
||||
return map(collection, collectionConstructor, StringUtils::parseInt);
|
||||
}
|
||||
|
||||
public static <C extends Collection<Long>, T> C mapToLong(Collection<T> collection, IntFunction<C> collectionConstructor) {
|
||||
return map(collection, collectionConstructor, NumberUtils::parseLong);
|
||||
return map(collection, collectionConstructor, StringUtils::parseLong);
|
||||
}
|
||||
|
||||
public static <C extends Collection<Float>, T> C mapToFloat(Collection<T> collection, IntFunction<C> collectionConstructor) {
|
||||
return map(collection, collectionConstructor, NumberUtils::parseFloat);
|
||||
return map(collection, collectionConstructor, StringUtils::parseFloat);
|
||||
}
|
||||
|
||||
public static <C extends Collection<Double>, T> C mapToDouble(Collection<T> collection, IntFunction<C> collectionConstructor) {
|
||||
return map(collection, collectionConstructor, NumberUtils::parseDouble);
|
||||
return map(collection, collectionConstructor, StringUtils::parseDouble);
|
||||
}
|
||||
|
||||
public static <T, R> R[] mapAndToArray(Collection<T> collection, IntFunction<R[]> arrayConstructor, Function<T, R> function) {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.zhdev.util;
|
||||
package org.zhdev.varioutil.util;
|
||||
|
||||
import java.time.*;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.zhdev.util;
|
||||
package org.zhdev.varioutil.util;
|
||||
|
||||
public class EnumUtils {
|
||||
private static <T extends Enum<T>> T[] getValues(T eNum) {
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
package org.zhdev.util;
|
||||
package org.zhdev.varioutil.util;
|
||||
|
||||
import org.zhdev.reflection.FieldSearcher;
|
||||
import org.zhdev.reflection.MethodSearcher;
|
||||
import org.zhdev.varioutil.reflection.FieldSearcher;
|
||||
import org.zhdev.varioutil.reflection.MethodSearcher;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
package org.zhdev.util;
|
||||
package org.zhdev.varioutil.util;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class ResourceUtils {
|
||||
public static InputStream getResource(String path, ClassLoader loader) {
|
||||
|
|
@ -25,7 +26,7 @@ public class ResourceUtils {
|
|||
return getResource(path, ResourceUtils.class.getClassLoader());
|
||||
}
|
||||
|
||||
public static boolean saveResource(String resourcePath, File outFile, ClassLoader loader) throws IllegalStateException {
|
||||
public static boolean saveResource(String resourcePath, Path outPath, ClassLoader loader) throws IllegalStateException {
|
||||
if (resourcePath == null || resourcePath.equals("")) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -36,14 +37,14 @@ public class ResourceUtils {
|
|||
return false;
|
||||
}
|
||||
|
||||
File parent = outFile.getParentFile();
|
||||
if (parent != null && !parent.exists()) {
|
||||
parent.mkdirs();
|
||||
}
|
||||
|
||||
try {
|
||||
if (!outFile.exists() || outFile.length() == 0) {
|
||||
OutputStream stream = Files.newOutputStream(outFile.toPath());
|
||||
Path parent = outPath.getParent();
|
||||
if (parent != null && Files.notExists(parent)) {
|
||||
Files.createDirectories(parent);
|
||||
}
|
||||
|
||||
if (Files.notExists(outPath) || Files.size(outPath) == 0) {
|
||||
OutputStream stream = Files.newOutputStream(outPath);
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = in.read(buf)) > 0) {
|
||||
|
|
@ -54,14 +55,14 @@ public class ResourceUtils {
|
|||
return true;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Could not save resource " + resourcePath + " to " + outFile, e);
|
||||
throw new IllegalStateException("Could not save resource " + resourcePath + " to " + outPath, e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static boolean saveResource(String resourcePath, File outFile) throws IllegalStateException {
|
||||
return saveResource(resourcePath, outFile);
|
||||
public static boolean saveResource(String resourcePath, Path outPath) throws IllegalStateException {
|
||||
return saveResource(resourcePath, outPath, ResourceUtils.class.getClassLoader());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package org.zhdev.util;
|
||||
package org.zhdev.varioutil.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
|
@ -8,10 +8,12 @@ public class StringUtils {
|
|||
public static String join(int beginIndex, int lastIndex, String delimiter, String lastDelimiter, String... elements) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (; beginIndex < elements.length; beginIndex++) {
|
||||
if (beginIndex == lastIndex) {
|
||||
builder.append(lastDelimiter);
|
||||
} else {
|
||||
builder.append(delimiter);
|
||||
if (builder.length() > 0) {
|
||||
if (beginIndex == lastIndex) {
|
||||
builder.append(lastDelimiter);
|
||||
} else {
|
||||
builder.append(delimiter);
|
||||
}
|
||||
}
|
||||
builder.append(elements[beginIndex]);
|
||||
}
|
||||
|
|
@ -262,4 +264,156 @@ public class StringUtils {
|
|||
chars[0] = Character.toUpperCase(chars[0]);
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
public static boolean isDigit(String input) {
|
||||
for (int i = 0; i < input.length(); i++){
|
||||
if (!Character.isDigit(input.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static byte parseByte(String str, byte def) {
|
||||
try {
|
||||
return Byte.parseByte(str);
|
||||
} catch (NumberFormatException e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public static short parseShort(String str, short def) {
|
||||
try {
|
||||
return Short.parseShort(str);
|
||||
} catch (NumberFormatException e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public static int parseInt(String str, int def) {
|
||||
try {
|
||||
return Integer.parseInt(str);
|
||||
} catch (NumberFormatException e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public static long parseLong(String str, long def) {
|
||||
try {
|
||||
return Long.parseLong(str);
|
||||
} catch (NumberFormatException e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public static double parseFloat(String str, float def) {
|
||||
try {
|
||||
return Float.parseFloat(str);
|
||||
} catch (NumberFormatException e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public static double parseDouble(String str, double def) {
|
||||
try {
|
||||
return Double.parseDouble(str);
|
||||
} catch (NumberFormatException e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public static Byte parseByte(Object obj, Byte def) {
|
||||
try {
|
||||
return Byte.parseByte(String.valueOf(obj));
|
||||
} catch (NumberFormatException e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public static Byte parseByte(Object obj) {
|
||||
return parseByte(obj, null);
|
||||
}
|
||||
|
||||
public static Short parseShort(Object obj, Short def) {
|
||||
try {
|
||||
return Short.parseShort(String.valueOf(obj));
|
||||
} catch (NumberFormatException e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public static Short parseShort(Object obj) {
|
||||
return parseShort(obj, null);
|
||||
}
|
||||
|
||||
public static Integer parseInt(Object obj, Integer def) {
|
||||
try {
|
||||
return Integer.parseInt(String.valueOf(obj));
|
||||
} catch (NumberFormatException e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer parseInt(Object obj) {
|
||||
return parseInt(obj, null);
|
||||
}
|
||||
|
||||
public static Long parseLong(Object obj, Long def) {
|
||||
try {
|
||||
return Long.parseLong(String.valueOf(obj));
|
||||
} catch (NumberFormatException e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public static Long parseLong(Object obj) {
|
||||
return parseLong(obj, null);
|
||||
}
|
||||
|
||||
public static Float parseFloat(Object obj, Float def) {
|
||||
try {
|
||||
return Float.parseFloat(String.valueOf(obj));
|
||||
} catch (NumberFormatException e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public static Float parseFloat(Object obj) {
|
||||
return parseFloat(obj, null);
|
||||
}
|
||||
|
||||
public static Double parseDouble(Object obj, Double def) {
|
||||
try {
|
||||
return Double.parseDouble(String.valueOf(obj));
|
||||
} catch (NumberFormatException e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
public static Double parseDouble(Object obj) {
|
||||
return parseDouble(obj, null);
|
||||
}
|
||||
|
||||
public static String inflect(long value, String var1, String var2, String var3, String var4) {
|
||||
if (value == 0) {
|
||||
return var4;
|
||||
}
|
||||
|
||||
long r1;
|
||||
if (value % 10 == 0 || (value / 10) % 10 == 1 || (r1 = value % 10) > 4) {
|
||||
return var3;
|
||||
} else if (r1 != 1) {
|
||||
return var2;
|
||||
} else {
|
||||
return var1;
|
||||
}
|
||||
}
|
||||
|
||||
public static String inflect(long value, String var1, String var2, String var3) {
|
||||
return inflect(value, var1, var2, var3, var3);
|
||||
}
|
||||
|
||||
public static String inflect(long value, String var1, String var2) {
|
||||
return inflect(value, var1, var2, var2);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue