Package edu.uiuc.ncsa.security.core.util
Class StringUtils
- java.lang.Object
- 
- edu.uiuc.ncsa.security.core.util.StringUtils
 
- 
 public class StringUtils extends Object A very heavily used class. This centralizes many idioms about strings, plus it has a lot of simplified formatting utilities.Created by Jeff Gaynor 
 on 4/23/20 at 6:34 AM
- 
- 
Field SummaryFields Modifier and Type Field Description protected static Stringblanksprotected static intDEFAULT_DISPLAY_WIDTHstatic StringELLIPSISstatic booleanJUSTIFY_LEFTstatic booleanJUSTIFY_RIGHTstatic String[]LOGICAL_FALSESstatic String[]LOGICAL_TRUESprotected static StringSTARSstatic StringtableFieldDelimiter
 - 
Constructor SummaryConstructors Constructor Description StringUtils()
 - 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static StringbreakAtSpaces(String input, int width)Take a string and return a multiline version of it within the width, but breaking at spaces.static List<String>breakAtSpacesList(String input, int width)Same asbreakAtSpaces(String, int)but return a list.static Stringcenter(Integer value, int width)static Stringcenter(String text, int width)Centers the string within the width.static booleanequals(String x, String y)static booleanequals(String x, String y, boolean ignoreCase, boolean trimEnds)Checks if towo strings have equal content.static StringformatByteCount(long count)Make E.g.static StringformatCount(long count, String unit)Formats the count and units with the correct unit prefix, formatting the count.static StringformatElapsedTime(long elapsedTime)Makes elapsed times in milliseconds human readable.static StringformatHerz(long cycles, long startTime)Formats the number of cycles from the starting time.static List<String>formatMap(Map map, List<String> keySubset, boolean sortKeys, boolean multiLine, int indent, int displayWidth)static List<String>formatMap(Map map, List<String> keySubset, boolean sortKeys, boolean multiLine, int indent, int displayWidth, boolean tryJSON)The tryJSON flag means that if an entry might be JSON, try to interpret it and use JSON formatting guidelines.static List<String>formatMap(Map map, List<String> keySubset, boolean sortKeys, boolean multiLine, int indent, int displayWidth, boolean tryJSON, boolean omitLineNumbers)static StringformatMapEntry(String key, String value, int indentWidth, int displayWidth, int leftColumWidth, boolean multiLine)Format an entry from a map.static List<String>formatTable(List<String> headers, List<List<String>> table, int maxFieldWidth, boolean showDelimiter)Takes a table -- defined as a list of rows, each row is a separate column amd returns a list of formatted rows that can just be printed (piped through a stream or whatever) It is assumed there are no missing columns, though null columns are fine and are rendered as blank.static StringfromList(List<String> listOfStrings)Convert a list of strings into a single string with linefeeds.static StringgetBlanks(int width)Get a string of blanks for the given width.static StringgetNBSpaces(int width)static StringhLine(String s, int length)Create a horizontal "line" of characters, repeating, for the given length.static booleanisTrivial(String x)If the string is either null or empty.static Stringjustify(int leftMargin, String source, int rightMargin)Left and right margin are column numbers, so 10, string 80 means the resulting string will be padded with 10 characters on the left and 80 and the right.static List<String>justify(int leftMargin, List<String> source, int rightMargin)static Stringjustify(String x, int width, boolean rightJustify)Right or right justify a snippet of text to the given width, e.g.static StringlistToString(List<String> list)Converts a list of strings to a single string with embedded linefeeds.static StringLJustify(String x, int width)Left justify, padding the string on the right.static voidmain(String[] args)Some quick tests for this class.static Stringpad(String s, int commandBufferMaxWidth)Pad a string with blanks as needed.static Stringpad2(int value, int commandBufferMaxWidth)static Stringpad2(String s, boolean isTruncate, int commandBufferMaxWidth)static Stringpad2(String s, int commandBufferMaxWidth)Pad the string to the given length with blanks.static Stringpad2(Date value, boolean isISO, int commandBufferMaxWidth)static Stringpad2(Date value, int commandBufferMaxWidth)Default is ISO 8601 datesstatic StringrepeatString(String s, int width)Repeat the given string within the given width.static StringRJustify(String x, int width)Right justify, so for the given width, the string is padded on the right.static List<String>stringToList(String s)Converts a string with embedded linefeeds into a list of strings, one per line.protected static voidtestWrap()static BooleantoBoolean(String x)Convert a string to a boolean if it parses, null otherwise.static BooleantoBoolean(String x, boolean ignoreCase)Case insensitive conversion of a string to a boolean or null if it does not parse.static List<String>toList(String x)Convert a string with embedded line feeds to a list os strings.static StringtoUnicode(char ch)Returns the unicode for a single characterstatic StringtoUnicode(String x)static Stringtruncate(String x)Truncate with all the defaults.static Stringtruncate(String x, int width)Truncate with the defaultELLIPSIS.static Stringtruncate(String x, int width, String ellipsis)truncate the string x to the given width.static List<String>wrap(int offset, List<String> source, int rightMargin)For output likestatic Stringwrap(String source, int width)Convience to wrap a single string.
 
- 
- 
- 
Field Detail- 
blanksprotected static String blanks 
 - 
DEFAULT_DISPLAY_WIDTHprotected static int DEFAULT_DISPLAY_WIDTH 
 - 
JUSTIFY_LEFTpublic static boolean JUSTIFY_LEFT 
 - 
JUSTIFY_RIGHTpublic static boolean JUSTIFY_RIGHT 
 - 
ELLIPSISpublic static String ELLIPSIS 
 - 
LOGICAL_TRUESpublic static String[] LOGICAL_TRUES 
 - 
LOGICAL_FALSESpublic static String[] LOGICAL_FALSES 
 - 
STARSprotected static String STARS 
 - 
tableFieldDelimiterpublic static final String tableFieldDelimiter - See Also:
- Constant Field Values
 
 
- 
 - 
Method Detail- 
RJustifypublic static String RJustify(String x, int width) Right justify, so for the given width, the string is padded on the right. Note that if width < x.length, the string is returned unchanged.- Parameters:
- x-
- width-
- Returns:
 
 - 
LJustifypublic static String LJustify(String x, int width) Left justify, padding the string on the right. See note inRJustify(String, int)- Parameters:
- x-
- width-
- Returns:
 
 - 
getBlankspublic static String getBlanks(int width) Get a string of blanks for the given width. If the width is non-positive, the empty string is returned.- Parameters:
- width-
- Returns:
 
 - 
repeatStringpublic static String repeatString(String s, int width) Repeat the given string within the given width. E.g.repeatString("*", 1) **********- Parameters:
- s-
- width-
- Returns:
 
 - 
getNBSpacespublic static String getNBSpaces(int width) 
 - 
justifypublic static String justify(String x, int width, boolean rightJustify) Right or right justify a snippet of text to the given width, e.g. for right justify:x = "abc"; width = 5; output then is " abc"Left justify effectively pads it on the- Parameters:
- x-
- width-
- Returns:
 
 - 
hLinepublic static String hLine(String s, int length) Create a horizontal "line" of characters, repeating, for the given length.- Parameters:
- s-
- length-
- Returns:
 
 - 
isTrivialpublic static boolean isTrivial(String x) If the string is either null or empty. This is a very common idiom for testing strings but the built inString.isEmpty()of course cannot be used on a null object...- Parameters:
- x-
- Returns:
 
 - 
mainpublic static void main(String[] args) Some quick tests for this class. Used for development, debugging or just run it if you are curious and look at the output.- Parameters:
- args-
 
 - 
testWrapprotected static void testWrap() 
 - 
truncatepublic static String truncate(String x, int width) Truncate with the defaultELLIPSIS.- Parameters:
- x-
- width-
- Returns:
 
 - 
truncatepublic static String truncate(String x) Truncate with all the defaults.- Parameters:
- x-
- Returns:
 
 - 
truncatepublic static String truncate(String x, int width, String ellipsis) truncate the string x to the given width. If the string is too long, use the ellipsis, e.g.x = "abcdefghijklmnopqrs"; ellipsis = "..."; width = 10;Then this would yieldabcdefg...If width < 0, then do not truncate.
 If ellipsis is null or empty, use the defaultELLIPSIS- Parameters:
- x-
- width-
- ellipsis-
- Returns:
 
 - 
justifypublic static String justify(int leftMargin, String source, int rightMargin) Left and right margin are column numbers, so 10, string 80 means the resulting string will be padded with 10 characters on the left and 80 and the right. Each line will be justifid within the margins separately- Parameters:
- leftMargin-
- source-
- rightMargin-
- Returns:
 
 - 
wrappublic static List<String> wrap(int offset, List<String> source, int rightMargin) For output likeabc : foo swwe e : bar fgd ddd ryfThis outdents the first line and indents to rest for the wrap.- Parameters:
- offset- length of whole left block
- source-
- rightMargin-
- Returns:
 
 - 
wrappublic static String wrap(String source, int width) Convience to wrap a single string.- Parameters:
- source-
- width-
- Returns:
 
 - 
toListpublic static List<String> toList(String x) Convert a string with embedded line feeds to a list os strings. Inverse offromList(List)- Parameters:
- x-
- Returns:
 
 - 
fromListpublic static String fromList(List<String> listOfStrings) Convert a list of strings into a single string with linefeeds. Inverse oftoList(String).- Parameters:
- listOfStrings-
- Returns:
 
 - 
equalspublic static boolean equals(String x, String y, boolean ignoreCase, boolean trimEnds) Checks if towo strings have equal content.- Parameters:
- x-
- y-
- ignoreCase- ignore case. Everything is converted to lower case before checking
- trimEnds- remove any lead or trailing whitespace before checking
- Returns:
 
 - 
padpublic static String pad(String s, int commandBufferMaxWidth) Pad a string with blanks as needed. This does not truncate if the string is too long. If you want truncation, usepad2(String, int).- Parameters:
- s-
- commandBufferMaxWidth-
- Returns:
 
 - 
pad2public static String pad2(int value, int commandBufferMaxWidth) 
 - 
pad2public static String pad2(Date value, int commandBufferMaxWidth) Default is ISO 8601 dates- Parameters:
- value-
- commandBufferMaxWidth-
- Returns:
 
 - 
pad2public static String pad2(String s, int commandBufferMaxWidth) Pad the string to the given length with blanks. This makes sure every line is the same length. If the line is too long, it is truncated- Parameters:
- s-
- commandBufferMaxWidth-
- Returns:
 
 - 
formatMappublic static List<String> formatMap(Map map, List<String> keySubset, boolean sortKeys, boolean multiLine, int indent, int displayWidth) SeeformatMap(Map, List, boolean, boolean, int, int, boolean). This just sets tryJSON true and passes the rest of the arguments along.- Parameters:
- map-
- keySubset-
- sortKeys-
- multiLine-
- indent-
- displayWidth-
- Returns:
 
 - 
formatMappublic static List<String> formatMap(Map map, List<String> keySubset, boolean sortKeys, boolean multiLine, int indent, int displayWidth, boolean tryJSON) The tryJSON flag means that if an entry might be JSON, try to interpret it and use JSON formatting guidelines. This should be set false in cases where you know that is not the case, e.g., in calls from QDL.- Parameters:
- map-
- keySubset-
- sortKeys-
- multiLine-
- indent-
- displayWidth-
- tryJSON-
- Returns:
 
 - 
formatMappublic static List<String> formatMap(Map map, List<String> keySubset, boolean sortKeys, boolean multiLine, int indent, int displayWidth, boolean tryJSON, boolean omitLineNumbers) 
 - 
formatMapEntrypublic static String formatMapEntry(String key, String value, int indentWidth, int displayWidth, int leftColumWidth, boolean multiLine) Format an entry from a map. This will take a (key, value) pair within margins so that colons align and format it as a line of the form (multiLine false):key : value...This truncates the value to fit on a single line and within the displayWidth. If multiLine is true it will be of the form:key : value split over several lines as needed.Note that the leftColumWidth is the length of the longest key in the map, usually. The key will be right justified within this field and hence all the colons will end up in the same place, making the output extremely readable.- Parameters:
- key- The key from the entry. If null, no key is used
- value- The value of the entry
- indentWidth- The indent on the left of the whole entry
- displayWidth- The actual width that this is to fit in
- leftColumWidth- The total width of the key field.
- multiLine- Split the value over several lines if too long, otherwise truncate with ellipsis.
- Returns:
 
 - 
listToStringpublic static String listToString(List<String> list) Converts a list of strings to a single string with embedded linefeeds.- Parameters:
- list-
- Returns:
 
 - 
stringToListpublic static List<String> stringToList(String s) Converts a string with embedded linefeeds into a list of strings, one per line. The line editor needs this.- Parameters:
- s-
- Returns:
 
 - 
formatTablepublic static List<String> formatTable(List<String> headers, List<List<String>> table, int maxFieldWidth, boolean showDelimiter) Takes a table -- defined as a list of rows, each row is a separate column amd returns a list of formatted rows that can just be printed (piped through a stream or whatever) It is assumed there are no missing columns, though null columns are fine and are rendered as blank.- Parameters:
- table-
- maxFieldWidth- maximum width of a column. Output is truncated if positive.
- showDelimiter-
 
 - 
toUnicodepublic static String toUnicode(char ch) Returns the unicode for a single character- Parameters:
- ch-
- Returns:
 
 - 
formatByteCountpublic static String formatByteCount(long count) Make E.g. byte counts from files human readable.- Parameters:
- count-
- Returns:
 
 - 
formatCountpublic static String formatCount(long count, String unit) Formats the count and units with the correct unit prefix, formatting the count. So if formatCount(54321, "b") --> 54.321 Kb, here "b" is the unit- Parameters:
- count-
- unit-
- Returns:
 
 - 
formatElapsedTimepublic static String formatElapsedTime(long elapsedTime) Makes elapsed times in milliseconds human readable.- Parameters:
- elapsedTime- The actual elapsed time in ms. E.g. 1000 is 1 sec.
- Returns:
 
 - 
formatHerzpublic static String formatHerz(long cycles, long startTime) Formats the number of cycles from the starting time. This will return properly formatted results to 4 places with units e.g. 12345 Hz is returned as 12.345 KHz- Parameters:
- cycles-
- startTime-
- Returns:
 
 - 
centerpublic static String center(String text, int width) Centers the string within the width. If the string is longer than the width, nothing is done and no truncation results. The result is a string of length equal to width, with the text in centered.- Parameters:
- text-
- width-
- Returns:
 
 - 
breakAtSpacespublic static String breakAtSpaces(String input, int width) Take a string and return a multiline version of it within the width, but breaking at spaces. E.g.breakAtSpace("mairzy doats and dozey doats and liddle lambsidivy",20)returnsmairzy doats and dozey doats and liddle lambsidivy N.B. that multiple spaces are converted to single spaces.- Parameters:
- input-
- width-
- Returns:
 
 - 
breakAtSpacesListpublic static List<String> breakAtSpacesList(String input, int width) Same asbreakAtSpaces(String, int)but return a list.- Parameters:
- input-
- width-
- Returns:
 
 - 
toBooleanpublic static Boolean toBoolean(String x) Convert a string to a boolean if it parses, null otherwise. Case matters so "TRUE" will fail. UsesLOGICAL_TRUESandLOGICAL_FALSES.- Parameters:
- x-
- Returns:
 
 - 
toBooleanpublic static Boolean toBoolean(String x, boolean ignoreCase) Case insensitive conversion of a string to a boolean or null if it does not parse. Both "TRUE" and "true" work. UsesLOGICAL_TRUESandLOGICAL_FALSES.- Parameters:
- x-
- ignoreCase-
- Returns:
 
 
- 
 
-