Skip to content
Snippets Groups Projects
Commit 34a8c54b authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Replaced overloaded methods with parameterized method in StringRow

While StringRow's overloaded methods won't suffer from super-linear
polynomial explosion as the alignment options increase (it would be
linear), this change preserves structural similarity with StringBox. It
also eliminates a switch statement in StringBox.placeString.

Added deprecation tags to the legacy methods.
parent efe596d7
No related branches found
No related tags found
No related merge requests found
...@@ -90,13 +90,13 @@ public class StringBox { ...@@ -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 * 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> * 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 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 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 * @param leftColumn the column in which the first character of each row should be placed
* @return the current StringBox object, suitable for chained calls * @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) { public StringBox placeString(String string, int topRow, int leftColumn) {
return placeString(string, Vertical.TOP, topRow, Horizontal.LEFT, leftColumn); return placeString(string, Vertical.TOP, topRow, Horizontal.LEFT, leftColumn);
...@@ -120,7 +120,8 @@ public class StringBox { ...@@ -120,7 +120,8 @@ public class StringBox {
*/ */
public StringBox placeString(String string, Vertical verticalAlignment, int verticalPosition, public StringBox placeString(String string, Vertical verticalAlignment, int verticalPosition,
Horizontal horizontalAlignment, int horizontalPosition) { 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 // This would benefit from the switch expression in JDK 12/13
int topRow = verticalAlignment == Vertical.TOP ? verticalPosition : int topRow = verticalAlignment == Vertical.TOP ? verticalPosition :
verticalAlignment == Vertical.BOTTOM ? verticalPosition - strings.length + 1 : verticalAlignment == Vertical.BOTTOM ? verticalPosition - strings.length + 1 :
...@@ -129,20 +130,7 @@ public class StringBox { ...@@ -129,20 +130,7 @@ public class StringBox {
int lastRow = Math.min(topRow + strings.length, maximumHeight); int lastRow = Math.min(topRow + strings.length, maximumHeight);
int offset = -topRow; int offset = -topRow;
for (int i = firstRow; i < lastRow; i++) { for (int i = firstRow; i < lastRow; i++) {
switch (horizontalAlignment) { rows[i].placeSubstring(strings[i+offset], horizontalAlignment, horizontalPosition);
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);
}
} }
logicalHeight = Math.max(logicalHeight, lastRow); logicalHeight = Math.max(logicalHeight, lastRow);
return this; return this;
...@@ -162,7 +150,9 @@ public class StringBox { ...@@ -162,7 +150,9 @@ public class StringBox {
* @param topRow the row on which the first line of the string should be placed * @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 * @param leftColumn the column in which the first character of each row should be placed
* @return the current StringBox object, suitable for chained calls * @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) { public StringBox placeStringAlignTopLeft(String string, int topRow, int leftColumn) {
return placeString(string, Vertical.TOP, topRow, Horizontal.LEFT, leftColumn); return placeString(string, Vertical.TOP, topRow, Horizontal.LEFT, leftColumn);
} }
...@@ -177,7 +167,9 @@ public class StringBox { ...@@ -177,7 +167,9 @@ public class StringBox {
* @param bottomRow the row on which the last line of the string should be placed * @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 * @param leftColumn the column in which the first character of each row should be placed
* @return the current StringBox object, suitable for chained calls * @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) { public StringBox placeStringAlignBottomLeft(String string, int bottomRow, int leftColumn) {
return placeString(string, Vertical.BOTTOM, bottomRow, Horizontal.LEFT, leftColumn); return placeString(string, Vertical.BOTTOM, bottomRow, Horizontal.LEFT, leftColumn);
} }
...@@ -192,7 +184,9 @@ public class StringBox { ...@@ -192,7 +184,9 @@ public class StringBox {
* @param topRow the row on which the first line of the string should be placed * @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 * @param rightColumn the column in which the last character of each row should be placed
* @return the current StringBox object, suitable for chained calls * @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) { public StringBox placeStringAlignTopRight(String string, int topRow, int rightColumn) {
return placeString(string, Vertical.TOP, topRow, Horizontal.RIGHT, rightColumn); return placeString(string, Vertical.TOP, topRow, Horizontal.RIGHT, rightColumn);
} }
...@@ -207,7 +201,9 @@ public class StringBox { ...@@ -207,7 +201,9 @@ public class StringBox {
* @param bottomRow the row on which the last line of the string should be placed * @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 * @param rightColumn the column in which the first character of each row should be placed
* @return the current StringBox object, suitable for chained calls * @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) { public StringBox placeStringAlignBottomRight(String string, int bottomRow, int rightColumn) {
return placeString(string, Vertical.BOTTOM, bottomRow, Horizontal.RIGHT, rightColumn); return placeString(string, Vertical.BOTTOM, bottomRow, Horizontal.RIGHT, rightColumn);
} }
...@@ -261,8 +257,8 @@ public class StringBox { ...@@ -261,8 +257,8 @@ public class StringBox {
* A helper inner class. Client code should not directly access StringRow objects. * A helper inner class. Client code should not directly access StringRow objects.
*/ */
static class StringRow { static class StringRow {
private StringBuilder stringBuilder; private final StringBuilder stringBuilder;
private int maximumWidth; private final int maximumWidth;
private int rightEdge; private int rightEdge;
public StringRow(int maximumWidth) { public StringRow(int maximumWidth) {
...@@ -271,31 +267,33 @@ public class StringBox { ...@@ -271,31 +267,33 @@ public class StringBox {
} }
public StringRow placeSubstring(String string, int leftColumn) { 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 String modifiedString = string
.replace("\t", " ") .replace("\t", " ")
.replace("\r", "") .replace("\r", "")
.replace("\n", "\\"); .replace("\n", "\\");
int actualLeftColumn = leftColumn; int leftColumn = horizontalAlignment == Horizontal.LEFT ? horizontalPosition :
if (actualLeftColumn > rightEdge) { horizontalAlignment == Horizontal.RIGHT ? horizontalPosition - modifiedString.length() :
int paddingSize = actualLeftColumn - rightEdge; 0;
if (leftColumn > rightEdge) {
int paddingSize = leftColumn - rightEdge;
StringBuilder padding = new StringBuilder(paddingSize); StringBuilder padding = new StringBuilder(paddingSize);
for (int i = 0; i < paddingSize; i++) { // This is a job made for String.repeat() in Java 11 for (int i = 0; i < paddingSize; i++) { // This is a job made for String.repeat() in Java 11
padding.append(" "); padding.append(" ");
} }
modifiedString = padding.toString() + modifiedString; modifiedString = padding.toString() + modifiedString;
actualLeftColumn -= paddingSize; leftColumn -= paddingSize;
} }
if (actualLeftColumn < 0) { if (leftColumn < 0) {
modifiedString = modifiedString.substring(-actualLeftColumn); modifiedString = modifiedString.substring(-leftColumn);
actualLeftColumn = 0; leftColumn = 0;
} }
int stringLength = modifiedString.length(); int stringLength = modifiedString.length();
stringBuilder.replace(actualLeftColumn, actualLeftColumn + stringLength, modifiedString); stringBuilder.replace(leftColumn, leftColumn + stringLength, modifiedString);
rightEdge = Math.max(rightEdge, actualLeftColumn + stringLength); rightEdge = Math.max(rightEdge, leftColumn + stringLength);
int overshoot = rightEdge - maximumWidth; int overshoot = rightEdge - maximumWidth;
if (overshoot > 0) { if (overshoot > 0) {
rightEdge -= overshoot; rightEdge -= overshoot;
...@@ -304,15 +302,24 @@ public class StringBox { ...@@ -304,15 +302,24 @@ public class StringBox {
return this; 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) { public StringRow placeSubstringAlignRight(String string, int rightColumn) {
String modifiedString = string return placeSubstring(string, Horizontal.RIGHT, rightColumn);
.replace("\t", " ")
.replace("\r", "")
.replace("\n", "\\");
int leftColumn = rightColumn - modifiedString.length();
return placeSubstringAlignLeft(modifiedString, leftColumn);
} }
/* *************************
* END OF LEGACY METHODS *
***************************/
@Override @Override
public String toString() { public String toString() {
return stringBuilder.toString(); return stringBuilder.toString();
......
...@@ -11,6 +11,7 @@ import org.junit.Test; ...@@ -11,6 +11,7 @@ import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@SuppressWarnings("deprecation")
public class StringBoxTest { public class StringBoxTest {
private StringBox stringBox; private StringBox stringBox;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment