diff --git a/src/main/java/edu/unl/cse/bohn/StringBox.java b/src/main/java/edu/unl/cse/bohn/StringBox.java index 820441e62948e3c3ea892125f2bcc29531ea0c62..9da516da72dc29106185093f59e3c04886bf447f 100644 --- a/src/main/java/edu/unl/cse/bohn/StringBox.java +++ b/src/main/java/edu/unl/cse/bohn/StringBox.java @@ -90,13 +90,13 @@ public class StringBox { * and the lines will be left-justified. Any portions of the string that would be placed outside the StringBox's * defined boundaries will be silently truncated.</p> * - * <p>Equivalent to <code>placeStringAlignTopLeft(string, topRow, leftColumn)</code>.</p> + * <p>Equivalent to <code>placeString(string, Vertical.TOP, topRow, Horizontal.LEFT, leftColumn)</code>.</p> * * @param string the string to be placed in the StringBox * @param topRow the row on which the first line of the string should be placed * @param leftColumn the column in which the first character of each row should be placed * @return the current StringBox object, suitable for chained calls - * @see #placeStringAlignTopLeft(String, int, int) + * @see #placeString(String, Vertical, int, Horizontal, int) */ public StringBox placeString(String string, int topRow, int leftColumn) { return placeString(string, Vertical.TOP, topRow, Horizontal.LEFT, leftColumn); @@ -120,29 +120,17 @@ public class StringBox { */ public StringBox placeString(String string, Vertical verticalAlignment, int verticalPosition, Horizontal horizontalAlignment, int horizontalPosition) { - String[] strings = string.split(System.lineSeparator()); + String[] oneEmptyString = {""}; + String[] strings = string != null ? string.split(System.lineSeparator()) : oneEmptyString; // This would benefit from the switch expression in JDK 12/13 int topRow = verticalAlignment == Vertical.TOP ? verticalPosition : - verticalAlignment == Vertical.BOTTOM ? verticalPosition - strings.length + 1 : - 0; + verticalAlignment == Vertical.BOTTOM ? verticalPosition - strings.length + 1 : + 0; int firstRow = Math.max(topRow, 0); int lastRow = Math.min(topRow + strings.length, maximumHeight); int offset = -topRow; for (int i = firstRow; i < lastRow; i++) { - switch (horizontalAlignment) { - case LEFT: - rows[i].placeSubstringAlignLeft(strings[i + offset], horizontalPosition); - break; - case RIGHT: - rows[i].placeSubstringAlignRight(strings[i + offset], horizontalPosition); - break; - default: - System.err.println("Unreachable code was reached in StringBox.placeString()!"); - System.err.println("\thorizontalAlignment == " + horizontalAlignment); - Horizontal recoveryAlignment = Horizontal.LEFT; - System.err.println("\tAttempting to recover with horizontalAlignment = " + recoveryAlignment); - rows[i].placeSubstringAlignLeft(strings[i + offset], horizontalPosition); - } + rows[i].placeSubstring(strings[i+offset], horizontalAlignment, horizontalPosition); } logicalHeight = Math.max(logicalHeight, lastRow); return this; @@ -162,7 +150,9 @@ public class StringBox { * @param topRow the row on which the first line of the string should be placed * @param leftColumn the column in which the first character of each row should be placed * @return the current StringBox object, suitable for chained calls + * @deprecated Replaced by {@link #placeString(String, Vertical, int, Horizontal, int) */ + @Deprecated public StringBox placeStringAlignTopLeft(String string, int topRow, int leftColumn) { return placeString(string, Vertical.TOP, topRow, Horizontal.LEFT, leftColumn); } @@ -177,7 +167,9 @@ public class StringBox { * @param bottomRow the row on which the last line of the string should be placed * @param leftColumn the column in which the first character of each row should be placed * @return the current StringBox object, suitable for chained calls + * @deprecated Replaced by {@link #placeString(String, Vertical, int, Horizontal, int) */ + @Deprecated public StringBox placeStringAlignBottomLeft(String string, int bottomRow, int leftColumn) { return placeString(string, Vertical.BOTTOM, bottomRow, Horizontal.LEFT, leftColumn); } @@ -192,7 +184,9 @@ public class StringBox { * @param topRow the row on which the first line of the string should be placed * @param rightColumn the column in which the last character of each row should be placed * @return the current StringBox object, suitable for chained calls + * @deprecated Replaced by {@link #placeString(String, Vertical, int, Horizontal, int) */ + @Deprecated public StringBox placeStringAlignTopRight(String string, int topRow, int rightColumn) { return placeString(string, Vertical.TOP, topRow, Horizontal.RIGHT, rightColumn); } @@ -207,7 +201,9 @@ public class StringBox { * @param bottomRow the row on which the last line of the string should be placed * @param rightColumn the column in which the first character of each row should be placed * @return the current StringBox object, suitable for chained calls + * @deprecated Replaced by {@link #placeString(String, Vertical, int, Horizontal, int) */ + @Deprecated public StringBox placeStringAlignBottomRight(String string, int bottomRow, int rightColumn) { return placeString(string, Vertical.BOTTOM, bottomRow, Horizontal.RIGHT, rightColumn); } @@ -261,8 +257,8 @@ public class StringBox { * A helper inner class. Client code should not directly access StringRow objects. */ static class StringRow { - private StringBuilder stringBuilder; - private int maximumWidth; + private final StringBuilder stringBuilder; + private final int maximumWidth; private int rightEdge; public StringRow(int maximumWidth) { @@ -271,31 +267,33 @@ public class StringBox { } public StringRow placeSubstring(String string, int leftColumn) { - return placeSubstringAlignLeft(string, leftColumn); + return placeSubstring(string, Horizontal.LEFT, leftColumn); } - public StringRow placeSubstringAlignLeft(String string, int leftColumn) { + public StringRow placeSubstring(String string, Horizontal horizontalAlignment, int horizontalPosition) { String modifiedString = string .replace("\t", " ") .replace("\r", "") .replace("\n", "\\"); - int actualLeftColumn = leftColumn; - if (actualLeftColumn > rightEdge) { - int paddingSize = actualLeftColumn - rightEdge; + int leftColumn = horizontalAlignment == Horizontal.LEFT ? horizontalPosition : + horizontalAlignment == Horizontal.RIGHT ? horizontalPosition - modifiedString.length() : + 0; + if (leftColumn > rightEdge) { + int paddingSize = leftColumn - rightEdge; StringBuilder padding = new StringBuilder(paddingSize); for (int i = 0; i < paddingSize; i++) { // This is a job made for String.repeat() in Java 11 padding.append(" "); } modifiedString = padding.toString() + modifiedString; - actualLeftColumn -= paddingSize; + leftColumn -= paddingSize; } - if (actualLeftColumn < 0) { - modifiedString = modifiedString.substring(-actualLeftColumn); - actualLeftColumn = 0; + if (leftColumn < 0) { + modifiedString = modifiedString.substring(-leftColumn); + leftColumn = 0; } int stringLength = modifiedString.length(); - stringBuilder.replace(actualLeftColumn, actualLeftColumn + stringLength, modifiedString); - rightEdge = Math.max(rightEdge, actualLeftColumn + stringLength); + stringBuilder.replace(leftColumn, leftColumn + stringLength, modifiedString); + rightEdge = Math.max(rightEdge, leftColumn + stringLength); int overshoot = rightEdge - maximumWidth; if (overshoot > 0) { rightEdge -= overshoot; @@ -304,15 +302,24 @@ public class StringBox { return this; } + /* ****************** + * LEGACY METHODS * + ********************/ + + @Deprecated + public StringRow placeSubstringAlignLeft(String string, int leftColumn) { + return placeSubstring(string, Horizontal.LEFT, leftColumn); + } + + @Deprecated public StringRow placeSubstringAlignRight(String string, int rightColumn) { - String modifiedString = string - .replace("\t", " ") - .replace("\r", "") - .replace("\n", "\\"); - int leftColumn = rightColumn - modifiedString.length(); - return placeSubstringAlignLeft(modifiedString, leftColumn); + return placeSubstring(string, Horizontal.RIGHT, rightColumn); } + /* ************************* + * END OF LEGACY METHODS * + ***************************/ + @Override public String toString() { return stringBuilder.toString(); diff --git a/src/test/java/edu/unl/cse/bohn/StringBoxTest.java b/src/test/java/edu/unl/cse/bohn/StringBoxTest.java index 5b68205b93b1ec594730cfeaa98f17d3e2dbec49..db1a318a02cfa831bdb05baf00f3b342980f6899 100644 --- a/src/test/java/edu/unl/cse/bohn/StringBoxTest.java +++ b/src/test/java/edu/unl/cse/bohn/StringBoxTest.java @@ -11,6 +11,7 @@ import org.junit.Test; import static org.junit.Assert.*; +@SuppressWarnings("deprecation") public class StringBoxTest { private StringBox stringBox;