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 String
blanks
protected static int
DEFAULT_DISPLAY_WIDTH
static String
ELLIPSIS
static boolean
JUSTIFY_LEFT
static boolean
JUSTIFY_RIGHT
protected static String
STARS
static String
tableFieldDelimiter
-
Constructor Summary
Constructors Constructor Description StringUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
center(String text, int width)
Centers the string within the width.static boolean
equals(String x, String y)
static boolean
equals(String x, String y, boolean ignoreCase, boolean trimEnds)
Checks if towo strings have equal content.static String
formatByteCount(long count)
Make E.g.static String
formatCount(long count, String unit)
Formats the count and units with the correct unit prefix, formatting the count.static String
formatElapsedTime(long elapsedTime)
Makes elapsed times in milliseconds human readable.static String
formatHerz(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)
Format a map of objects as easily readable key value pairs.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 String
formatMapEntry(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 String
fromList(List<String> listOfStrings)
Convert a list of strings into a single string with linefeeds.static String
getBlanks(int width)
Get a string of blanks for the given width.static String
getNBSpaces(int width)
static boolean
isTrivial(String x)
If the string is either null or empty.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.static List<String>
justify(int leftMargin, List<String> source, int rightMargin)
static String
justify(String x, int width, boolean rightJustify)
Right or right justify a snippet of text to the given width, e.g.static String
listToString(List<String> list)
Converts a list of strings to a single string with embedded linefeeds.static String
LJustify(String x, int width)
Left justify, padding the string on the right.static void
main(String[] args)
Some quick tests for this class.static String
pad(String s, int commandBufferMaxWidth)
Pad a string with blanks as needed.static String
pad2(int value, int commandBufferMaxWidth)
static String
pad2(String s, boolean isTruncate, int commandBufferMaxWidth)
static String
pad2(String s, int commandBufferMaxWidth)
Pad the string to the given length with blanks.static String
pad2(Date value, boolean isISO, int commandBufferMaxWidth)
static String
pad2(Date value, int commandBufferMaxWidth)
Default is ISO 8601 datesstatic String
RJustify(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 void
testWrap()
static List<String>
toList(String x)
Convert a string with embedded line feeds to a list os strings.static String
toUnicode(char ch)
Returns the unicode for a single characterstatic String
toUnicode(String x)
static String
truncate(String x)
Truncate with all the defaults.static String
truncate(String x, int width)
Truncate with the defaultELLIPSIS
.static String
truncate(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 String
wrap(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
-
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:
-
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:
-
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 ryf
This 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)
Format a map of objects as easily readable key value pairs.- Parameters:
map
- The map to be displayedkeySubset
- An optional (may be null or empty) subset of keys. Only these will be used id presentsortKeys
- if true, the keys in the map will be sorted.multiLine
- Split the formatting of the values in the map over several lines (true) or truncate with ellipses (false)indent
- The amount for the whole thing to be indented from the leftdisplayWidth
- The total width that this must fit in.- 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:
-
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 entryvalue
- 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:
-
-