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 Summary
Fields 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 Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All 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
-
blanks
protected static String blanks
-
DEFAULT_DISPLAY_WIDTH
protected static int DEFAULT_DISPLAY_WIDTH
-
JUSTIFY_LEFT
public static boolean JUSTIFY_LEFT
-
JUSTIFY_RIGHT
public static boolean JUSTIFY_RIGHT
-
ELLIPSIS
public static String ELLIPSIS
-
LOGICAL_TRUES
public static String[] LOGICAL_TRUES
-
LOGICAL_FALSES
public static String[] LOGICAL_FALSES
-
STARS
protected static String STARS
-
tableFieldDelimiter
public static final String tableFieldDelimiter
- See Also:
- Constant Field Values
-
-
Method Detail
-
RJustify
public 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:
-
LJustify
public static String LJustify(String x, int width)
Left justify, padding the string on the right. See note inRJustify(String, int)- Parameters:
x-width-- Returns:
-
getBlanks
public 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:
-
repeatString
public static String repeatString(String s, int width)
Repeat the given string within the given width. E.g.repeatString("*", 1) **********- Parameters:
s-width-- Returns:
-
getNBSpaces
public static String getNBSpaces(int width)
-
justify
public 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:
-
hLine
public static String hLine(String s, int length)
Create a horizontal "line" of characters, repeating, for the given length.- Parameters:
s-length-- Returns:
-
isTrivial
public 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:
-
main
public 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-
-
testWrap
protected static void testWrap()
-
truncate
public static String truncate(String x, int width)
Truncate with the defaultELLIPSIS.- Parameters:
x-width-- Returns:
-
truncate
public static String truncate(String x)
Truncate with all the defaults.- Parameters:
x-- Returns:
-
truncate
public 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:
-
justify
public 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:
-
wrap
public 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 blocksource-rightMargin-- Returns:
-
wrap
public static String wrap(String source, int width)
Convience to wrap a single string.- Parameters:
source-width-- Returns:
-
toList
public static List<String> toList(String x)
Convert a string with embedded line feeds to a list os strings. Inverse offromList(List)- Parameters:
x-- Returns:
-
fromList
public static String fromList(List<String> listOfStrings)
Convert a list of strings into a single string with linefeeds. Inverse oftoList(String).- Parameters:
listOfStrings-- Returns:
-
equals
public 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 checkingtrimEnds- remove any lead or trailing whitespace before checking- Returns:
-
pad
public 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:
-
pad2
public static String pad2(int value, int commandBufferMaxWidth)
-
pad2
public static String pad2(Date value, int commandBufferMaxWidth)
Default is ISO 8601 dates- Parameters:
value-commandBufferMaxWidth-- Returns:
-
pad2
public 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:
-
formatMap
public 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:
-
formatMap
public 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:
-
formatMap
public static List<String> formatMap(Map map, List<String> keySubset, boolean sortKeys, boolean multiLine, int indent, int displayWidth, boolean tryJSON, boolean omitLineNumbers)
-
formatMapEntry
public 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 usedvalue- The value of the entryindentWidth- The indent on the left of the whole entrydisplayWidth- The actual width that this is to fit inleftColumWidth- The total width of the key field.multiLine- Split the value over several lines if too long, otherwise truncate with ellipsis.- Returns:
-
listToString
public static String listToString(List<String> list)
Converts a list of strings to a single string with embedded linefeeds.- Parameters:
list-- Returns:
-
stringToList
public 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:
-
formatTable
public 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-
-
toUnicode
public static String toUnicode(char ch)
Returns the unicode for a single character- Parameters:
ch-- Returns:
-
formatByteCount
public static String formatByteCount(long count)
Make E.g. byte counts from files human readable.- Parameters:
count-- Returns:
-
formatCount
public 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:
-
formatElapsedTime
public 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:
-
formatHerz
public 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:
-
center
public 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:
-
breakAtSpaces
public 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:
-
breakAtSpacesList
public static List<String> breakAtSpacesList(String input, int width)
Same asbreakAtSpaces(String, int)but return a list.- Parameters:
input-width-- Returns:
-
toBoolean
public 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:
-
toBoolean
public 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:
-
-