Created
August 29, 2012 14:30
-
-
Save aembleton/3513421 to your computer and use it in GitHub Desktop.
Provides a way to produce an information box surrounded by a char such as *.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedList; | |
import java.util.List; | |
/** | |
* Provides a way to produce an information box surrounded by a char such as *. To use, add lines by calling {@link #add(String)} and then call {@link #getStringsInBox(char)} to get a String that can | |
* be put into a System.out.println call or used elsewhere that is formatted. | |
* | |
* @author Arthur Embleton | |
* | |
*/ | |
public class InformationBox { | |
private static final String EOL = System.getProperty("line.separator"); | |
private final List<String> lines = new LinkedList<String>(); | |
/** | |
* Adds a line to the list of lines that will be printed out in a box when {@link #getStringsInBox(char)} is called. | |
* | |
* @param line | |
* The line to be added | |
*/ | |
public void add(String line) { | |
lines.add(line); | |
} | |
/** | |
* Add a blank line | |
*/ | |
public void add() { | |
lines.add(""); | |
} | |
/** | |
* Returns all of the strings inside a box made out of the border character. | |
* | |
* @param border | |
* The character to use as a border | |
* @return A {@link String} that when displayed will show all of the strings added using {@link #add(String)} inside a box | |
*/ | |
public String getStringsInBox(char border) { | |
StringBuffer sb = new StringBuffer(); | |
int width = maxLengthOfLines();// add four to give a border | |
String topAndBottom = buildStringWithChar(border, width); | |
sb.append(EOL); | |
sb.append(' ').append(topAndBottom).append(' ').append(EOL); | |
sb.append(formatLine("", border, width)).append(EOL); | |
for (String line : lines) { | |
sb.append(formatLine(line, border, width)).append(EOL); | |
} | |
sb.append(formatLine("", border, width)).append(EOL); | |
sb.append(' ').append(topAndBottom).append(' ').append(EOL); | |
return sb.toString(); | |
} | |
private String formatLine(String line, char border, int width) { | |
String paddedStr = padEndToLength(line, width); | |
return applyBorder(border, paddedStr); | |
} | |
private String applyBorder(char border, String str) { | |
StringBuffer sb = new StringBuffer(); | |
sb.append(' ').append(border).append(' ').append(str).append(' ').append(border); | |
return sb.toString(); | |
} | |
private String padEndToLength(String str, int width) { | |
StringBuffer sb = new StringBuffer(str); | |
while (sb.length() < width) { | |
sb.append(' '); | |
} | |
return sb.toString(); | |
} | |
private String buildStringWithChar(char c, int width) { | |
StringBuffer sb = new StringBuffer(); | |
// add 4 to give a border | |
for (int i = 0; i < width + 4; i++) { | |
sb.append(c); | |
} | |
return sb.toString(); | |
} | |
private int maxLengthOfLines() { | |
int maxLength = 0; | |
for (String line : lines) { | |
if (line != null && line.length() > maxLength) { | |
maxLength = line.length(); | |
} | |
} | |
return maxLength; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment