Class Utils
java.lang.Object
ru.bgcrm.util.Utils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault delimiter: ", "static final char[]static final char[] -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final voidaddCommaSeparated(StringBuilder result, String value) Appends a new value to the end of the string, separating it with a comma and a space if it is not the first onestatic final voidaddSeparated(StringBuilder result, String separator, String value) Appends a new value to the end of the string, separating it with the given separator if it is not the first onestatic final voidaddSetupPair(StringBuilder data, String prefix, String param, String value) Appends a "prefix param=value" line to the datastatic final StringbooleanToStringInt(boolean value) Converts a boolean value to a string "1" or "0"static final intcountLines(String value) Counts the number of lines in stringstatic final FilecreateDirectoryIfNoExistInWorkDir(String dirName) Creates a directory with a given name in working directory if it does not existstatic final <T, C extends Collection<T>>
CemptyToNull(C value) Converts tonullan empty collectionstatic voiderrorAndExit(int code, String message) Writes error message and exits the running applicationstatic StringReplaces XML markup symbols with special codesstatic Stringformat(BigDecimal value) Formats a decimal value to a regular string without trailing zerosstatic final Stringstatic StringgetDigest(byte[] value) HEX representation of MD5 digeststatic StringComputes the MD5 digest HEX string of the input string, using UTF-8 encodingstatic StringReturns the HEX string representation of the MD5 hash of the input stringstatic <T> TgetFirst(Collection<T> collection) getObjectIds(Collection<T> values) Comma separated object IDsgetObjectIds(Collection<T> values, String startValues) Comma separated object IDsgetObjectIds(Collection<T> values, String startValues, String delim) Separated object IDsgetObjectIdsList(Collection<T> list) Returns a list of object IDsgetObjectIdsSet(Collection<T> list) Returns a set of object IDsgetObjectList(List<T> fullList, Collection<Integer> selectedIds) Selects objects sub-list from a given full list with IDs presented in a collectiongetObjectList(Map<Integer, T> fullMap, List<Integer> selectedIds) Selects objects list from a full map by a given IDs list in the same ordergetObjectTitles(Collection<T> list) Concatenates object titles to a comma separated stringgetObjectTitles(List<T> fullList, Set<Integer> selectedIds) Concatenates object titles to a comma separated stringgetObjectTitles(Map<Integer, T> fullMap, List<Integer> selectedIds) Concatenates object titles to a comma separated stringstatic intExtracts entity ID from URLstatic StringgetSystemProperty(String key, String defaultValue) Retrieves a property value fromSystem.getProperty(String)static StringSystem temp directorystatic booleanChecks if object is instance one of classesstatic StringhtmlEncode(String value) Replaces only HTML tags with escaped codesstatic final booleanisBlankString(String value) Checks whether the string is empty, consists only of whitespace chars, or isnullstatic final booleanisEmptyString(String value) Checks whether the string is empty ornullstatic final booleanisPositive(Integer value) Is the integer value greater than zero.static booleanisStringNumber(String str) Checks whether the string contains only digits, i.e.static final booleanisValidEmail(String value) Is the string a valid Email addressstatic final StringConverts the input string to a default value if it isnullor emptystatic final StringConverts the input string to an empty one if it isnullstatic final <T> TmaskNull(T value, T defaultValue) Returns the value, or a default value if the value isnullstatic final BigDecimalmaskNullDecimal(BigDecimal value) Returns the input value if it is notnull, otherwiseBigDecimal.ZEROstatic ObjectnewInstance(String className, Object... args) Deprecated.static final booleannotBlankString(String value) Checks if string value is blankstatic final booleannotBlankStrings(String... value) Checks all the passed values withnotBlankString(String)static final booleannotEmptyString(String value) Checks whether the string is not empty and notnullstatic BigDecimalparseBigDecimal(Object obj) Converts an object's string representation to a decimal objectstatic BigDecimalparseBigDecimal(Object obj, BigDecimal defaultValue) Converts an object's string representation to a decimal objectstatic booleanparseBoolean(String str) Converts a string to a boolean, returnfalsein case of errorstatic BooleanparseBoolean(String str, Boolean defaultValue) Converts a string to a booleanparseIdTitleList(String value) Parses a list of objects from a string like "id:title; id:title"parseIdTitleList(String value, String noPairValue) Parses a list of objects from a string like "id:title; id:title".static intConverts a string to an int, return0in case of errorstatic intConverts a string to an int, return a default value in case of errorstatic longConverts a string to a long, return0in case of errorstatic longConverts a string to a long, return a default value in case of errorstatic voidsetFileNameHeaders(javax.servlet.http.HttpServletResponse response, String fileName) Sets HTTP headers for downloaded filestatic final StringsubstringAfter(String value, String token, int num) Returns the remainder of the string after thenum-th occurrence of the tokentoIntegerList(String valuesStr) Converts a string, delimited by commas or semicolons, to a list ofIntegertoIntegerList(String valuesStr, String delims) Converts a string, delimited by the given chars, to a list ofIntegertoIntegerSet(String valuesStr) Converts a string, delimited by commas, to a set ofIntegerConverts a string, delimited by commas or semicolons, to a list of stringsConverts a string with arbitrary delimiter chars to list of stringsConverts a string, delimited by commas or semicolons, to a set of stringsConverts a string, delimited by the given chars, to a set of stringsstatic final StringtoString(Collection<?> valuesList) Converts a collection to a string joined withDEFAULT_DELIMstatic final StringtoString(Collection<?> valuesList, String emptyValue, String delim) Converts a collection to a string joined with the delimiterstatic final StringJoins configuration lines to a single string with a separator
-
Field Details
-
DEFAULT_DELIM
-
HEX
public static final char[] HEX -
HEX_LOWERCASE
public static final char[] HEX_LOWERCASE
-
-
Constructor Details
-
Utils
public Utils()
-
-
Method Details
-
parseInt
Converts a string to an int, return0in case of error- Parameters:
str- the input string- Returns:
- the number converted from the string, or
0in case of error
-
parseInt
Converts a string to an int, return a default value in case of error- Parameters:
str- the input stringdefaultValue- the default value in case of error- Returns:
- the number converted from the string, or
defaultValuein case of error
-
parseLong
Converts a string to a long, return0in case of error- Parameters:
str- the input string- Returns:
- the number converted from the string, or
0in case of error
-
parseLong
Converts a string to a long, return a default value in case of error- Parameters:
str- the input stringdefaultValue- the default value in case of error- Returns:
- the number converted from the string, or
defaultValuein case of error
-
parseBigDecimal
Converts an object's string representation to a decimal object- Parameters:
obj- the objectdefaultValue- the default value in case of parsing exception- Returns:
- decimal object,
defaultValuefor any unparsable value
-
parseBigDecimal
Converts an object's string representation to a decimal object- Parameters:
obj- the object- Returns:
- decimal object,
nullwhenobjwasnull,BigDecimal.ZEROfor any unparsable value
-
parseBoolean
Converts a string to a boolean, returnfalsein case of error- Parameters:
str- the input string- Returns:
- the boolean value converted from the string, or
falsein case of error
-
isStringNumber
Checks whether the string contains only digits, i.e. no letters- Parameters:
str- the input string- Returns:
trueif the string doesn't contain letters
-
parseBoolean
Converts a string to a boolean- Parameters:
str- the stringdefaultValue- the default value- Returns:
true- forstr"1", "TRUE", "YES", "ON" case insensitive;false- forstr"0", "FALSE", "NO" case insensitive; all other cases -defaultValue
-
booleanToStringInt
Converts a boolean value to a string "1" or "0"- Parameters:
value- the boolean value- Returns:
- "1" for
true, "0" forfalse
-
maskNull
public static final <T> T maskNull(T value, T defaultValue) Returns the value, or a default value if the value isnull- Parameters:
value- the valuedefaultValue- the default value- Returns:
value, ordefaultValueifvalueisnull
-
maskNull
-
maskNullDecimal
Returns the input value if it is notnull, otherwiseBigDecimal.ZERO- Parameters:
value- the input value- Returns:
value, orBigDecimal.ZEROifvalueisnull
-
emptyToNull
Converts tonullan empty collection- Parameters:
value- the input collection- Returns:
- the input collection, if it is not empty, or
null
-
maskEmpty
-
isEmptyString
Checks whether the string is empty ornull- Parameters:
value- the checked string- Returns:
trueif empty ornull
-
isBlankString
Checks whether the string is empty, consists only of whitespace chars, or isnull- Parameters:
value- the checked string- Returns:
trueif empty, blank, ornull
-
notEmptyString
Checks whether the string is not empty and notnull- Parameters:
value- the checked string- Returns:
falseif empty ornull
-
notBlankString
Checks if string value is blank- Parameters:
value- checked value- Returns:
falsewhenvalueisnull, empty or has only whitespace chars
-
notBlankStrings
Checks all the passed values withnotBlankString(String)- Parameters:
value- values- Returns:
valueis not null, all the values aren't blank
-
countLines
Counts the number of lines in string- Parameters:
value- the string- Returns:
- the count of
\nchars
-
isPositive
Is the integer value greater than zero. To do not create lambda functions.- Parameters:
value- the value- Returns:
trueifvalueis greater than zero
-
isValidEmail
Is the string a valid Email address- Parameters:
value- the checked string- Returns:
trueif the string is a valid email address
-
toString
Converts a collection to a string joined withDEFAULT_DELIM- Parameters:
valuesList- the collection- Returns:
- the resulting string, or an empty string if the collection is empty
-
toString
Converts a collection to a string joined with the delimiter- Parameters:
valuesList- the collectionemptyValue- the value to return for an empty listdelim- the delimiter- Returns:
- the resulting string
-
toIntegerList
-
toIntegerList
-
toIntegerSet
-
toSet
-
toSet
-
toList
-
toList
-
toText
-
getObjectIdsList
Returns a list of object IDs- Parameters:
list- the objects- Returns:
- the resulting list
-
getObjectIdsSet
Returns a set of object IDs- Parameters:
list- the objects- Returns:
- the resulting set
-
getObjectIds
Comma separated object IDs- Parameters:
values- the objects- Returns:
- the resulting string
-
getObjectIds
public static final <T extends Id<Integer>> String getObjectIds(Collection<T> values, String startValues) Comma separated object IDs- Parameters:
values- the objectsstartValues- beginning of the resulting string- Returns:
- the resulting string
-
getObjectIds
-
getObjectTitles
Concatenates object titles to a comma separated string- Parameters:
list- the list of titled objects- Returns:
- comma separated string
-
getObjectTitles
-
getObjectTitles
public static final <T extends IdTitle> String getObjectTitles(Map<Integer, T> fullMap, List<Integer> selectedIds) Concatenates object titles to a comma separated string- Parameters:
fullMap- the full object mapselectedIds- the selected IDs, defines the resulting order- Returns:
- comma separated string
-
getObjectList
public static final <T extends IdTitle> List<T> getObjectList(List<T> fullList, Collection<Integer> selectedIds) Selects objects sub-list from a given full list with IDs presented in a collection- Parameters:
fullList- the full listselectedIds- the IDs collection- Returns:
- the resulting sub-list
-
getObjectList
-
parseIdTitleList
-
parseIdTitleList
Parses a list of objects from a string like "id:title; id:title". If the title part of a pair is missing,noPairValueis used instead.- Parameters:
value- the input stringnoPairValue- the default title value for entries without one- Returns:
- the resulting list
-
addSetupPair
public static final void addSetupPair(StringBuilder data, String prefix, String param, String value) Appends a "prefix param=value" line to the data- Parameters:
data- the target bufferprefix- the line prefixparam- the parameter namevalue- the parameter value
-
addCommaSeparated
Appends a new value to the end of the string, separating it with a comma and a space if it is not the first one- Parameters:
result- the target buffervalue- the value to append
-
addSeparated
Appends a new value to the end of the string, separating it with the given separator if it is not the first one- Parameters:
result- the target bufferseparator- the separatorvalue- the value to append
-
getDigest
-
getDigest
-
getDigest
HEX representation of MD5 digest- Parameters:
value- digest basic- Returns:
- digest HEX string or
nullin case of any error
-
getTmpDir
System temp directory- Returns:
- value of system property 'java.io.tmpdir', or '/tmp' if it is missing
-
createDirectoryIfNoExistInWorkDir
-
substringAfter
Returns the remainder of the string after thenum-th occurrence of the token- Parameters:
value- the input stringtoken- the token to search fornum- the occurrence number- Returns:
- the remaining substring, or an empty string if not found
-
generateSecret
- Returns:
- generated random string with 32 ASCII chars
-
getFirst
- Parameters:
collection- collection of elements- Returns:
- the first element from
collection, ornullif collection isnullor empty
-
escapeXml
-
htmlEncode
-
getOpenId
Extracts entity ID from URL- Parameters:
url- URL- Returns:
- extracted positive ID or
0if couldn't extract
-
setFileNameHeaders
public static void setFileNameHeaders(javax.servlet.http.HttpServletResponse response, String fileName) Sets HTTP headers for downloaded file- Parameters:
response- the HTTP responsefileName- the file name
-
errorAndExit
Writes error message and exits the running application- Parameters:
code- exit codemessage- message
-
newInstance
-
hasClass
-
getSystemProperty
Retrieves a property value fromSystem.getProperty(String)- Parameters:
key- the key is prepended bybgerp.defaultValue- the default value if no property found- Returns:
- the property value, or
defaultValueif not found
-
format
Formats a decimal value to a regular string without trailing zeros- Parameters:
value- the decimal value- Returns:
- empty string for
nullvalue, or formatted string
-