This class is modified from Apache's commons-lang package. + *
Operations on Strings that contain words.
+ * + *This class tries to handle null
input gracefully.
+ * An exception will not be thrown for a null
input.
+ * Each method documents its behaviour in more detail.
Wraps a single line of text, identifying words by ' '
.
New lines will be separated by the system property line separator. + * Very long words, such as URLs will not be wrapped.
+ * + *Leading spaces on a new line are stripped. + * Trailing spaces are not stripped.
+ * + *+ * WordUtils.wrap(null, *) = null + * WordUtils.wrap("", *) = "" + *+ * + * @param str the String to be word wrapped, may be null + * @param wrapLength the column to wrap the words at, less than 1 is treated as 1 + * @return a line with newlines inserted,
null
if null input
+ */
+ public static String wrap(String str, int wrapLength) {
+ return wrap(str, wrapLength, null, false);
+ }
+
+ /**
+ * Wraps a single line of text, identifying words by ' '
.
Leading spaces on a new line are stripped. + * Trailing spaces are not stripped.
+ * + *+ * WordUtils.wrap(null, *, *, *) = null + * WordUtils.wrap("", *, *, *) = "" + *+ * + * @param str the String to be word wrapped, may be null + * @param wrapLength the column to wrap the words at, less than 1 is treated as 1 + * @param newLineStr the string to insert for a new line, + *
null
uses the system property line separator
+ * @param wrapLongWords true if long words (such as URLs) should be wrapped
+ * @return a line with newlines inserted, null
if null input
+ */
+ public static String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords) {
+ if (str == null) {
+ return null;
+ }
+ if (newLineStr == null) {
+ newLineStr = java.lang.System.lineSeparator();
+ }
+ if (wrapLength < 1) {
+ wrapLength = 1;
+ }
+ int inputLineLength = str.length();
+ int offset = 0;
+ StringBuffer wrappedLine = new StringBuffer(inputLineLength + 32);
+
+ while ((inputLineLength - offset) > wrapLength) {
+ if (str.charAt(offset) == ' ') {
+ offset++;
+ continue;
+ }
+ int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset);
+
+ if (spaceToWrapAt >= offset) {
+ // normal case
+ wrappedLine.append(str.substring(offset, spaceToWrapAt));
+ wrappedLine.append(newLineStr);
+ offset = spaceToWrapAt + 1;
+
+ } else {
+ // really long word or URL
+ if (wrapLongWords) {
+ // wrap really long word one line at a time
+ wrappedLine.append(str.substring(offset, wrapLength + offset));
+ wrappedLine.append(newLineStr);
+ offset += wrapLength;
+ } else {
+ // do not wrap really long word, just extend beyond limit
+ spaceToWrapAt = str.indexOf(' ', wrapLength + offset);
+ if (spaceToWrapAt >= 0) {
+ wrappedLine.append(str.substring(offset, spaceToWrapAt));
+ wrappedLine.append(newLineStr);
+ offset = spaceToWrapAt + 1;
+ } else {
+ wrappedLine.append(str.substring(offset));
+ offset = inputLineLength;
+ }
+ }
+ }
+ }
+
+ // Whatever is left in line is short enough to just pass through
+ wrappedLine.append(str.substring(offset));
+
+ return wrappedLine.toString();
+ }
+}
diff --git a/src/main/java/me/desht/dhutils/blocks/RelativePosition.java b/src/main/java/me/desht/dhutils/blocks/RelativePosition.java
index ce2224f0..b3bb8c9a 100644
--- a/src/main/java/me/desht/dhutils/blocks/RelativePosition.java
+++ b/src/main/java/me/desht/dhutils/blocks/RelativePosition.java
@@ -1,6 +1,6 @@
package me.desht.dhutils.blocks;
-import org.apache.commons.lang.Validate;
+import com.google.common.base.Preconditions;
import io.github.thebusybiscuit.sensibletoolbox.api.items.BaseSTBBlock;
@@ -19,7 +19,7 @@ public class RelativePosition {
private final int left;
public RelativePosition(int front, int up, int left) {
- Validate.isTrue(front != 0 || up != 0 || left != 0, "At least one of front, up, left must be non-zero");
+ Preconditions.checkArgument(front != 0 || up != 0 || left != 0, "At least one of front, up, left must be non-zero");
this.front = front;
this.up = up;
this.left = left;
diff --git a/src/main/java/me/desht/dhutils/commands/AbstractCommand.java b/src/main/java/me/desht/dhutils/commands/AbstractCommand.java
index beadd651..c2c90f57 100644
--- a/src/main/java/me/desht/dhutils/commands/AbstractCommand.java
+++ b/src/main/java/me/desht/dhutils/commands/AbstractCommand.java
@@ -12,8 +12,8 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
-import org.apache.commons.lang.StringUtils;
-import org.apache.commons.lang.Validate;
+import com.google.common.base.Strings;
+import com.google.common.base.Preconditions;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
@@ -67,7 +67,7 @@ public AbstractCommand(String label, int minArgs, int maxArgs) {
public abstract boolean execute(Plugin plugin, CommandSender sender, String[] args);
public void addAlias(@Nonnull String label) {
- Validate.notNull(label, "The alias cannot be null");
+ Preconditions.checkArgument(label != null, "The alias cannot be null");
String[] fields = label.split(" ");
cmdRecs.add(new CommandRecord(fields));
}
@@ -218,7 +218,7 @@ protected void showUsage(CommandSender sender, String alias, String prefix) {
indent = "";
} else {
int l = prefix.length();
- indent = sender instanceof Player ? StringUtils.repeat(" ", l + 2) : StringUtils.repeat(" ", l);
+ indent = sender instanceof Player ? Strings.repeat(" ", l + 2) : Strings.repeat(" ", l);
}
for (int i = 0; i < usage.length; i++) {
diff --git a/src/main/java/me/desht/dhutils/commands/CommandManager.java b/src/main/java/me/desht/dhutils/commands/CommandManager.java
index 0f3ca679..17acfb6d 100644
--- a/src/main/java/me/desht/dhutils/commands/CommandManager.java
+++ b/src/main/java/me/desht/dhutils/commands/CommandManager.java
@@ -8,7 +8,7 @@
import javax.annotation.Nonnull;
-import org.apache.commons.lang.Validate;
+import com.google.common.base.Preconditions;
import org.bukkit.Sound;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
@@ -31,7 +31,7 @@ public CommandManager(@Nonnull Plugin plugin) {
}
public void registerCommand(@Nonnull AbstractCommand cmd) {
- Validate.notNull(cmd, "Command cannot be null!");
+ Preconditions.checkArgument(cmd != null, "Command cannot be null!");
Debugger.getInstance().debug(2, "register command: " + cmd.getClass().getName());
cmdList.add(cmd);