summaryrefslogtreecommitdiffstats
path: root/Essentials2Compat/src/com/earth2me/essentials/Util.java
diff options
context:
space:
mode:
Diffstat (limited to 'Essentials2Compat/src/com/earth2me/essentials/Util.java')
-rw-r--r--Essentials2Compat/src/com/earth2me/essentials/Util.java111
1 files changed, 111 insertions, 0 deletions
diff --git a/Essentials2Compat/src/com/earth2me/essentials/Util.java b/Essentials2Compat/src/com/earth2me/essentials/Util.java
new file mode 100644
index 000000000..3a34a85fc
--- /dev/null
+++ b/Essentials2Compat/src/com/earth2me/essentials/Util.java
@@ -0,0 +1,111 @@
+package com.earth2me.essentials;
+
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.*;
+import java.util.logging.Logger;
+import java.util.regex.Pattern;
+
+
+public class Util
+{
+ private Util()
+ {
+ }
+ private final static Logger logger = Logger.getLogger("Minecraft");
+ private final static Pattern INVALIDFILECHARS = Pattern.compile("[^a-z0-9]");
+ private final static Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFC]");
+
+ //Used to clean file names before saving to disk
+ public static String sanitizeFileName(final String name)
+ {
+ return safeString(name);
+ }
+
+ //Used to clean strings/names before saving as filenames/permissions
+ public static String safeString(final String string)
+ {
+ return INVALIDFILECHARS.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_");
+ }
+
+ //Less restrictive string sanitizing, when not used as perm or filename
+ public static String sanitizeString(final String string)
+ {
+ return INVALIDCHARS.matcher(string).replaceAll("");
+ }
+
+
+ private static DecimalFormat dFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US));
+
+ public static String formatAsCurrency(final double value)
+ {
+ String str = dFormat.format(value);
+ if (str.endsWith(".00"))
+ {
+ str = str.substring(0, str.length() - 3);
+ }
+ return str;
+ }
+
+ public static double roundDouble(final double d)
+ {
+ return Math.round(d * 100.0) / 100.0;
+ }
+
+ public static boolean isInt(final String sInt)
+ {
+ try
+ {
+ Integer.parseInt(sInt);
+ }
+ catch (NumberFormatException e)
+ {
+ return false;
+ }
+ return true;
+ }
+
+ public static String joinList(Object... list)
+ {
+ return joinList(", ", list);
+ }
+
+ public static String joinList(String seperator, Object... list)
+ {
+ StringBuilder buf = new StringBuilder();
+ for (Object each : list)
+ {
+ if (buf.length() > 0)
+ {
+ buf.append(seperator);
+ }
+
+ if (each instanceof Collection)
+ {
+ buf.append(joinList(seperator, ((Collection)each).toArray()));
+ }
+ else
+ {
+ try
+ {
+ buf.append(each.toString());
+ }
+ catch (Exception e)
+ {
+ buf.append(each.toString());
+ }
+ }
+ }
+ return buf.toString();
+ }
+
+ public static String lastCode(final String input)
+ {
+ int pos = input.lastIndexOf("ยง");
+ if (pos == -1 || (pos + 1) == input.length())
+ {
+ return "";
+ }
+ return input.substring(pos, pos + 2);
+ }
+} \ No newline at end of file