StringMan instead of StringUtils

This commit is contained in:
boy0001
2015-07-27 04:38:08 +10:00
parent 042a295a55
commit 393a5bffd5
24 changed files with 277 additions and 182 deletions

View File

@ -43,9 +43,7 @@ import com.intellectualcrafters.plot.database.SQLite;
import com.intellectualcrafters.plot.flag.AbstractFlag;
import com.intellectualcrafters.plot.flag.FlagManager;
import com.intellectualcrafters.plot.flag.FlagValue;
import com.plotsquared.bukkit.generator.AugmentedPopulator;
import com.intellectualcrafters.plot.generator.ClassicPlotWorld;
import com.plotsquared.bukkit.generator.HybridGen;
import com.intellectualcrafters.plot.generator.HybridPlotWorld;
import com.intellectualcrafters.plot.generator.HybridUtils;
import com.intellectualcrafters.plot.generator.PlotGenerator2;
@ -70,10 +68,8 @@ import com.intellectualcrafters.plot.util.EconHandler;
import com.intellectualcrafters.plot.util.EventUtil;
import com.intellectualcrafters.plot.util.ExpireManager;
import com.intellectualcrafters.plot.util.InventoryUtil;
import com.intellectualcrafters.plot.util.Logger;
import com.intellectualcrafters.plot.util.MathMan;
import com.intellectualcrafters.plot.util.StringMan;
import com.intellectualcrafters.plot.util.Logger.LogLevel;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.PlayerManager;
import com.plotsquared.bukkit.util.SetupUtils;
@ -1549,7 +1545,6 @@ public class PS {
setupStyle();
} catch (final Exception err) {
err.printStackTrace();
Logger.add(LogLevel.DANGER, "Failed to save style.yml");
log("failed to save style.yml");
}
try {
@ -1562,7 +1557,6 @@ public class PS {
config = YamlConfiguration.loadConfiguration(configFile);
setupConfig();
} catch (final Exception err_trans) {
Logger.add(LogLevel.DANGER, "Failed to save settings.yml");
log("Failed to save settings.yml");
}
try {
@ -1575,7 +1569,6 @@ public class PS {
storage = YamlConfiguration.loadConfiguration(storageFile);
setupStorage();
} catch (final Exception err_trans) {
Logger.add(LogLevel.DANGER, "Failed to save storage.yml");
log("Failed to save storage.yml");
}
try {
@ -1583,7 +1576,7 @@ public class PS {
config.save(configFile);
storage.save(storageFile);
} catch (final IOException e) {
Logger.add(LogLevel.DANGER, "Configuration file saving failed");
log("Configuration file saving failed");
e.printStackTrace();
}
}

View File

@ -24,10 +24,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.intellectualcrafters.plot.config.Configuration.SettingValue;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.util.StringMan;
/**
* Configuration Node
@ -91,7 +90,7 @@ public class ConfigurationNode {
public Object getDefaultValue() {
if (this.default_value instanceof Object[]) {
return StringUtils.join((Object[]) this.default_value, ",");
return StringMan.join((Object[]) this.default_value, ",");
}
return this.default_value;
}

View File

@ -37,7 +37,6 @@ import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.bukkit.block.Biome;
import com.intellectualcrafters.plot.PS;
@ -53,6 +52,7 @@ import com.intellectualcrafters.plot.object.PlotSettings;
import com.intellectualcrafters.plot.object.RunnableVal;
import com.intellectualcrafters.plot.object.comment.PlotComment;
import com.intellectualcrafters.plot.util.ClusterManager;
import com.intellectualcrafters.plot.util.StringMan;
import com.intellectualcrafters.plot.util.TaskManager;
/**
@ -1100,7 +1100,7 @@ public class SQLManager implements AbstractDB {
}
} else {
element = element.replaceAll("\u00AF", ":").replaceAll("\u00B4", ",");
if (StringUtils.isAlpha(element.replaceAll("_", "").replaceAll("-", ""))) {
if (StringMan.isAlpha(element.replaceAll("_", "").replaceAll("-", ""))) {
Flag flag = new Flag(FlagManager.getFlag(element, true), "");
flags.put(flag.getKey(), flag);
} else {
@ -1247,7 +1247,7 @@ public class SQLManager implements AbstractDB {
}
public void setFlags(final int id, final Collection<Flag> newflags) {
final String flag_string = StringUtils.join(newflags, ",");
final String flag_string = StringMan.join(newflags, ",");
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {

View File

@ -3,7 +3,7 @@ package com.intellectualcrafters.plot.database;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.commons.lang.StringUtils;
import com.intellectualcrafters.plot.util.StringMan;
public abstract class StmtMod<T> {
public abstract String getCreateMySQL(int size);
@ -11,15 +11,15 @@ public abstract class StmtMod<T> {
public String getCreateMySQL(int size, String query, int params) {
final StringBuilder statement = new StringBuilder(query);
for (int i = 0; i < size - 1; i++) {
statement.append("(" + StringUtils.repeat(",(?)", params).substring(1) + "),");
statement.append("(" + StringMan.repeat(",(?)", params).substring(1) + "),");
}
statement.append("(" + StringUtils.repeat(",(?)", params).substring(1) + ")");
statement.append("(" + StringMan.repeat(",(?)", params).substring(1) + ")");
return statement.toString();
}
public String getCreateSQLite(int size, String query, int params) {
StringBuilder statement = new StringBuilder(query);
String modParams = StringUtils.repeat(",?", params).substring(1);
String modParams = StringMan.repeat(",?", params).substring(1);
for (int i = 0; i < (size - 1); i++) {
statement.append("UNION SELECT " + modParams + " ");
}

View File

@ -20,7 +20,7 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.flag;
import org.apache.commons.lang.StringUtils;
import com.intellectualcrafters.plot.util.StringMan;
/**
* Created 2014-09-23 for PlotSquared
@ -42,7 +42,7 @@ public class AbstractFlag {
* @param key
*/
public AbstractFlag(final String key, final FlagValue<?> value) {
if (!StringUtils.isAlpha(key.replaceAll("_", "").replaceAll("-", ""))) {
if (!StringMan.isAlpha(key.replaceAll("_", "").replaceAll("-", ""))) {
throw new IllegalArgumentException("Flag must be alphabetic characters");
}
if (key.length() > 16) {

View File

@ -20,7 +20,7 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.flag;
import org.apache.commons.lang.StringUtils;
import com.intellectualcrafters.plot.util.StringMan;
public class Flag {
private AbstractFlag key;
@ -36,7 +36,7 @@ public class Flag {
* @throws IllegalArgumentException if you provide inadequate inputs
*/
public Flag(final AbstractFlag key, final String value) {
if (!StringUtils.isAsciiPrintable(value)) {
if (!StringMan.isAsciiPrintable(value)) {
throw new IllegalArgumentException("Flag must be ascii");
}
if (value.length() > 128) {

View File

@ -5,9 +5,8 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.util.StringMan;
/**
* Created 2014-11-17 for PlotSquared
@ -298,7 +297,7 @@ public abstract class FlagValue<T> {
@SuppressWarnings("unchecked")
@Override
public String toString(final Object t) {
return StringUtils.join((HashSet<PlotBlock>) t, ",");
return StringMan.join((HashSet<PlotBlock>) t, ",");
}
@SuppressWarnings("unchecked")
@ -357,7 +356,7 @@ public abstract class FlagValue<T> {
@SuppressWarnings("unchecked")
@Override
public String toString(final Object t) {
return StringUtils.join((List<Integer>) t, ",");
return StringMan.join((List<Integer>) t, ",");
}
@SuppressWarnings("unchecked")
@ -405,7 +404,7 @@ public abstract class FlagValue<T> {
@Override
public String toString(final Object t) {
return StringUtils.join((List<String>) t, ",");
return StringMan.join((List<String>) t, ",");
}
@Override
@ -445,7 +444,7 @@ public abstract class FlagValue<T> {
@SuppressWarnings("unchecked")
@Override
public String toString(final Object t) {
return StringUtils.join((List<Double>) t, ",");
return StringMan.join((List<Double>) t, ",");
}
@SuppressWarnings("unchecked")

View File

@ -5,9 +5,10 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import com.plotsquared.bukkit.generator.AugmentedPopulator;
import org.apache.commons.lang.mutable.MutableInt;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Material;
@ -215,8 +216,8 @@ public class BukkitHybridUtils extends HybridUtils {
};
System.gc();
AugmentedPopulator.initCache();
TaskManager.index.increment();
final Integer currentIndex = TaskManager.index.toInteger();
TaskManager.index.incrementAndGet();
final Integer currentIndex = TaskManager.index.get();
final Integer task = TaskManager.runTaskRepeat(new Runnable() {
@Override
public void run() {
@ -268,7 +269,7 @@ public class BukkitHybridUtils extends HybridUtils {
}
public void checkModified(final Plot plot, final RunnableVal<Integer> whenDone) {
TaskManager.index.increment();
TaskManager.index.incrementAndGet();
final Location bot = MainUtil.getPlotBottomLoc(plot.world, plot.id).add(1, 0, 1);
final Location top = MainUtil.getPlotTopLoc(plot.world, plot.id);
int bx = bot.getX() >> 4;
@ -291,9 +292,9 @@ public class BukkitHybridUtils extends HybridUtils {
final ClassicPlotWorld cpw = (ClassicPlotWorld) plotworld;
final MutableInt count = new MutableInt(0);
final AtomicInteger count = new AtomicInteger(0);
final Integer currentIndex = TaskManager.index.toInteger();
final Integer currentIndex = TaskManager.index.get();
final Integer task = TaskManager.runTaskRepeat(new Runnable() {
@Override
public void run() {
@ -312,9 +313,9 @@ public class BukkitHybridUtils extends HybridUtils {
int ex = Math.min((chunk.getX() << 4) + 15, top.getX());
int ez = Math.min((chunk.getZ() << 4) + 15, top.getZ());
// count changes
count.add(checkModified(plot.world, bx, ex, 1, cpw.PLOT_HEIGHT - 1, bz, ez, cpw.MAIN_BLOCK));
count.add(checkModified(plot.world, bx, ex, cpw.PLOT_HEIGHT, cpw.PLOT_HEIGHT, bz, ez, cpw.TOP_BLOCK));
count.add(checkModified(plot.world, bx, ex, cpw.PLOT_HEIGHT + 1, 255, bz, ez, new PlotBlock[] { new PlotBlock((short) 0, (byte) 0) }));
count.addAndGet(checkModified(plot.world, bx, ex, 1, cpw.PLOT_HEIGHT - 1, bz, ez, cpw.MAIN_BLOCK));
count.addAndGet(checkModified(plot.world, bx, ex, cpw.PLOT_HEIGHT, cpw.PLOT_HEIGHT, bz, ez, cpw.TOP_BLOCK));
count.addAndGet(checkModified(plot.world, bx, ex, cpw.PLOT_HEIGHT + 1, 255, bz, ez, new PlotBlock[] { new PlotBlock((short) 0, (byte) 0) }));
}
}, 1);
TaskManager.tasks.put(currentIndex, task);
@ -414,11 +415,11 @@ public class BukkitHybridUtils extends HybridUtils {
BukkitHybridUtils.world = world;
chunks = new ArrayList<ChunkLoc>();
final Plugin plugin = BukkitMain.THIS;
final MutableInt count = new MutableInt(0);
final AtomicInteger count = new AtomicInteger(0);
this.task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
@Override
public void run() {
count.increment();
count.incrementAndGet();
if (count.intValue() % 20 == 0) {
PS.log("PROGRESS: " + ((100 * (2048 - chunks.size())) / 2048) + "%");
}

View File

@ -1,11 +1,10 @@
package com.intellectualcrafters.plot.generator;
import org.apache.commons.lang.StringUtils;
import com.intellectualcrafters.configuration.ConfigurationSection;
import com.intellectualcrafters.plot.config.Configuration;
import com.intellectualcrafters.plot.config.ConfigurationNode;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.util.StringMan;
public abstract class ClassicPlotWorld extends SquarePlotWorld {
public static int ROAD_HEIGHT_DEFAULT = 64;
@ -50,8 +49,8 @@ public abstract class ClassicPlotWorld extends SquarePlotWorld {
super.loadConfiguration(config);
this.PLOT_BEDROCK = config.getBoolean("plot.bedrock");
this.PLOT_HEIGHT = Math.min(255, config.getInt("plot.height"));
this.MAIN_BLOCK = (PlotBlock[]) Configuration.BLOCKLIST.parseString(StringUtils.join(config.getStringList("plot.filling"), ','));
this.TOP_BLOCK = (PlotBlock[]) Configuration.BLOCKLIST.parseString(StringUtils.join(config.getStringList("plot.floor"), ','));
this.MAIN_BLOCK = (PlotBlock[]) Configuration.BLOCKLIST.parseString(StringMan.join(config.getStringList("plot.filling"), ','));
this.TOP_BLOCK = (PlotBlock[]) Configuration.BLOCKLIST.parseString(StringMan.join(config.getStringList("plot.floor"), ','));
this.WALL_BLOCK = (PlotBlock) Configuration.BLOCK.parseString(config.getString("wall.block"));
this.ROAD_HEIGHT = Math.min(255, config.getInt("road.height"));
this.ROAD_BLOCK = (PlotBlock) Configuration.BLOCK.parseString(config.getString("road.block"));

View File

@ -6,8 +6,7 @@ import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.mutable.MutableInt;
import java.util.concurrent.atomic.AtomicInteger;
import com.intellectualcrafters.configuration.file.YamlConfiguration;
import com.intellectualcrafters.plot.PS;
@ -139,12 +138,12 @@ public class PlotAnalysis {
final int[] ratings = new int[plots.size()];
final MutableInt mi = new MutableInt(0);
final AtomicInteger mi = new AtomicInteger(0);
Thread ratingAnalysis = new Thread(new Runnable() {
@Override
public void run() {
for (;mi.intValue() < plots.size(); mi.increment()) {
for (;mi.intValue() < plots.size(); mi.incrementAndGet()) {
int i = mi.intValue();
Plot plot = plots.get(i);
ratings[i] = (int) ((plot.getAverageRating() + plot.getSettings().ratings.size()) * 100);

View File

@ -25,8 +25,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import com.intellectualcrafters.configuration.ConfigurationSection;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.Configuration;
@ -35,6 +33,7 @@ import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.flag.Flag;
import com.intellectualcrafters.plot.flag.FlagManager;
import com.intellectualcrafters.plot.util.EconHandler;
import com.intellectualcrafters.plot.util.StringMan;
/**
* @author Jesse Boyd
@ -189,7 +188,7 @@ public abstract class PlotWorld {
this.DEFAULT_FLAGS = FlagManager.parseFlags(flags);
} catch (final Exception e) {
e.printStackTrace();
PS.log("&cInvalid default flags for " + this.worldname + ": " + StringUtils.join(flags, ","));
PS.log("&cInvalid default flags for " + this.worldname + ": " + StringMan.join(flags, ","));
this.DEFAULT_FLAGS = new HashMap<>();
}
this.PVP = config.getBoolean("event.pvp");

View File

@ -1,89 +0,0 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
/**
* Logging of errors and debug messages.
*
* @author Citymonstret
*/
public class Logger {
private static ArrayList<String> entries;
private static File log;
public static void setup(final File file) {
log = file;
entries = new ArrayList<>();
try {
final BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
entries.add(line);
}
reader.close();
} catch (final IOException e) {
PS.log(C.PREFIX.s() + "File setup error Logger#setup");
}
}
public static void write() throws IOException {
final FileWriter writer = new FileWriter(log);
for (final String string : entries) {
writer.write(string + System.lineSeparator());
}
writer.close();
}
public static void add(final LogLevel level, final String string) {
append("[" + level.toString() + "] " + string);
}
private static void append(final String string) {
entries.add("[" + new Date().toString() + "]" + string);
}
public enum LogLevel {
GENERAL("General"),
WARNING("Warning"),
DANGER("Danger");
private final String name;
LogLevel(final String name) {
this.name = name;
}
@Override
public String toString() {
return this.name;
}
}
}

View File

@ -24,6 +24,30 @@ public class MathMan {
return i * 2;
}
public static boolean isInteger(String str) {
if (str == null) {
return false;
}
int length = str.length();
if (length == 0) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (length == 1) {
return false;
}
i = 1;
}
for (; i < length; i++) {
char c = str.charAt(i);
if (c <= '/' || c >= ':') {
return false;
}
}
return true;
}
public static double getSD(double[] array, double av) {
double sd = 0;
for (int i=0; i<array.length;i++)

View File

@ -53,7 +53,7 @@ public class SetBlockQueue {
runnables = new HashSet<>();
}
if (!running) {
TaskManager.index.increment();
TaskManager.index.incrementAndGet();
final int current = TaskManager.index.intValue();
int task = TaskManager.runTaskRepeat(new Runnable() {
@Override

View File

@ -23,8 +23,6 @@ package com.intellectualcrafters.plot.util;
import java.util.ArrayList;
import java.util.Collections;
import org.apache.commons.lang.StringUtils;
/**
* String comparison library
*
@ -76,7 +74,7 @@ public class StringComparison<T> {
* @return match
*/
public static int compare(final String s1, final String s2) {
int distance = StringUtils.getLevenshteinDistance(s1, s2);
int distance = StringMan.getLevenshteinDistance(s1, s2);
if (s2.contains(s1)) {
distance -= (Math.min(s1.length(), s2.length()));
}

View File

@ -1,5 +1,6 @@
package com.intellectualcrafters.plot.util;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
@ -41,8 +42,107 @@ public class StringMan {
return sb.toString();
}
public static boolean isAlphanumeric(String str) {
for (int i=0; i<str.length(); i++) {
char c = str.charAt(i);
if (c < 0x30 || (c >= 0x3a && c <= 0x40) || (c > 0x5a && c <= 0x60) || c > 0x7a) {
return false;
}
}
return true;
}
public static boolean isAlpha(String str) {
for (int i=0; i<str.length(); i++) {
char c = str.charAt(i);
if ((c <= 0x40) || (c > 0x5a && c <= 0x60) || c > 0x7a) {
return false;
}
}
return true;
}
public static String join(Collection<?> collection, String delimiter) {
return join(collection.toArray(), delimiter);
}
public static String join(Collection<?> collection, char delimiter) {
return join(collection.toArray(), delimiter + "");
}
public static boolean isAsciiPrintable(char c) {
return (c >= ' ') && (c < '');
}
public static boolean isAsciiPrintable(String s) {
for (char c: s.toCharArray()) {
if (!isAsciiPrintable(c)) {
return false;
}
}
return true;
}
public static int getLevenshteinDistance(String s, String t) {
int n = s.length();
int m = t.length();
if (n == 0) {
return m;
} else if (m == 0) {
return n;
}
if (n > m) {
String tmp = s;
s = t;
t = tmp;
n = m;
m = t.length();
}
int p[] = new int[n+1];
int d[] = new int[n+1];
int _d[];
int i;
int j;
char t_j;
int cost;
for (i = 0; i<=n; i++) {
p[i] = i;
}
for (j = 1; j<=m; j++) {
t_j = t.charAt(j-1);
d[0] = j;
for (i=1; i<=n; i++) {
cost = s.charAt(i-1)==t_j ? 0 : 1;
d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1), p[i-1]+cost);
}
_d = p;
p = d;
d = _d;
}
return p[n];
}
public static String join(Object[] array, String delimiter) {
StringBuilder result = new StringBuilder();
for (int i = 0, j = array.length; i < j; i++) {
if (i > 0) {
result.append(delimiter);
}
result.append(array[i]);
}
return result.toString();
}
public static boolean isEqual(String a, String b ) {
return (a == b || (a.length() == b.length() && a.hashCode() == b.hashCode() && a.equals(b)));
}
public static String repeat(String s, int n) {
final StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; i++) {
sb.append(s);
}
return sb.toString();
}
}

View File

@ -2,15 +2,14 @@ package com.intellectualcrafters.plot.util;
import java.util.HashMap;
import java.util.HashSet;
import org.apache.commons.lang.mutable.MutableInt;
import java.util.concurrent.atomic.AtomicInteger;
import com.intellectualcrafters.plot.PS;
public abstract class TaskManager {
public static HashSet<String> TELEPORT_QUEUE = new HashSet<>();
public static MutableInt index = new MutableInt(0);
public static AtomicInteger index = new AtomicInteger(0);
public static HashMap<Integer, Integer> tasks = new HashMap<>();
public static int runTaskRepeat(final Runnable r, final int interval) {