java.lang.Object
com.erudika.para.core.utils.Utils
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
abbreviate
(String str, int max) Abbreviates a string.static String
abbreviateInt
(Number number, int decPlaces) Abbreviates an integer by adding a letter suffix at the end.static String
Joins a list of strings to String using a separator.static String
Decodes a string from Base64.static String
base64enc
(byte[] str) Encodes a byte array to Base64.static String
base64encURL
(byte[] str) Encodes a byte array to Base64.static String
bcrypt hash function implemented by Spring Security.static boolean
bcryptMatches
(String plain, String storedHash) Checks if a hash matches a string.static String
compileMustache
(Map<String, Object> context, String template) static String
compileMustache
(Map<String, Object> context, String template, boolean escapeHtml) Compiles a mustache template with a given scope (map of fields and values).static String
formatDate
(Long timestamp, String format, Locale loc) Formats a date in a specific format.static String
formatDate
(String format, Locale loc) Formats the date for today, in a specific format.static String
formatMessage
(String msg, Object... params) Formats a messages containing {0}, {1}...static String
formatPrice
(double price) Returns the price with two fractional digits at the end.static String
Generates an authentication token - a random 32 byte string encoded in Base64.static String
generateSecurityToken
(int length) Generates an authentication token - a random string encoded in Base64.static String
generateSecurityToken
(int length, boolean urlSafe) Generates an authentication token - a random string encoded in Base64.getAllDeclaredFields
(Class<? extends ParaObject> clazz) Returns a list of all declared fields in a class.static String
getBaseURL
(String url) The basic URL without any parameters: >scheme<:>authority<.static int
Returns the current year.static String
getHostFromURL
(String url) Returns the host part of the URL.static HumanTime
HumanTime - a relative time formatter.static Utils
Returns an instance of this class.static Locale
static String[]
Returns an array of the months in the Gregorian calendar.static String
getNewId()
Distributed id generator.static String
getObjectURI
(ParaObject obj, boolean includeName, boolean includeId) Returns the default URL for a given domain object.static String
getSystemProperty
(String name) Same asSystem.getProperty(java.lang.String)
.static String
hmacSHA256
(String message, String secret) Computes the HmacSHA256 hash of a message.static boolean
isBasicType
(Class<?> clazz) Checks if a class is primitive, String or a primitive wrapper.static boolean
isJsonType
(String contentType) Checks if a response is of type JSON.static boolean
isValidEmail
(String email) Email validation.static boolean
isValidURL
(String url) URL validation.static String
markdownToHtml
(String markdownString) Converts Markdown to HTML.static String
markdownToHtml
(String markdownString, boolean htmlTagsRenderingEnabled) Converts Markdown to HTML.static String
md5 hash function.static String
Converts spaces to dashes.static int
round
(float d) Rounds a float to an int.static double
roundHalfUp
(double d) Round up a double using the "half up" method.static double
roundHalfUp
(double d, int scale) Round up a double using the "half up" method.static String
singularToPlural
(String singul) Quick and dirty singular to plural conversion.static String
stripAndTrim
(String str) Strips all symbols, punctuation, whitespace and control chars from a string.static String
stripAndTrim
(String str, String replaceWith) Strips all symbols, punctuation, whitespace and control chars from a string.static String
stripAndTrim
(String str, String replaceWith, boolean asciiOnly) Strips all symbols, punctuation, whitespace and control chars from a string.static String
Strips all HTML tags from a string.static long
Java timestamp.static String
type
(Class<? extends ParaObject> clazz) Returns the simple name of a class in lowercase (AKA the type).static String
Decodes a URL-encoded string.static String
URL-encodes a string.
-
Method Details
-
getInstance
-
getHumanTime
HumanTime - a relative time formatter.- Returns:
- humantime instance
-
md5
-
hmacSHA256
-
bcrypt
-
bcryptMatches
-
generateSecurityToken
Generates an authentication token - a random string encoded in Base64.- Parameters:
length
- the length of the generated tokenurlSafe
- switches to a URL safe encoding- Returns:
- a random string
-
generateSecurityToken
Generates an authentication token - a random string encoded in Base64.- Parameters:
length
- the length of the generated token- Returns:
- a random string
-
generateSecurityToken
Generates an authentication token - a random 32 byte string encoded in Base64.- Returns:
- a random string
-
stripHtml
-
markdownToHtml
-
markdownToHtml
-
compileMustache
public static String compileMustache(Map<String, Object> context, String template, boolean escapeHtml) Compiles a mustache template with a given scope (map of fields and values).- Parameters:
context
- a map of fields and valuestemplate
- a Mustache templateescapeHtml
- enable/disable escaping of HTML- Returns:
- the compiled template string
-
compileMustache
-
abbreviate
-
arrayJoin
-
stripAndTrim
-
stripAndTrim
-
stripAndTrim
Strips all symbols, punctuation, whitespace and control chars from a string.- Parameters:
str
- a dirty stringreplaceWith
- a string to replace spaces withasciiOnly
- if true, all non-ASCII characters will be stripped- Returns:
- a clean string
-
noSpaces
-
formatMessage
-
base64enc
Encodes a byte array to Base64.- Parameters:
str
- the byte array- Returns:
- an encoded string
-
base64encURL
Encodes a byte array to Base64. URL safe.- Parameters:
str
- the byte array- Returns:
- an encoded string
-
base64dec
-
formatDate
-
formatDate
-
getCurrentYear
public static int getCurrentYear()Returns the current year.- Returns:
- this year
-
timestamp
-
getMonths
-
getLocale
-
round
public static int round(float d) Rounds a float to an int.- Parameters:
d
- a float- Returns:
- a rounded int
-
formatPrice
Returns the price with two fractional digits at the end.- Parameters:
price
- a price- Returns:
- $###.##
-
roundHalfUp
public static double roundHalfUp(double d) Round up a double using the "half up" method.- Parameters:
d
- a double- Returns:
- a double
-
roundHalfUp
public static double roundHalfUp(double d, int scale) Round up a double using the "half up" method.- Parameters:
d
- a doublescale
- the scale- Returns:
- a double
-
abbreviateInt
-
urlDecode
-
urlEncode
-
isValidURL
URL validation.- Parameters:
url
- a URL- Returns:
- true if the URL is valid
-
isValidEmail
Email validation.- Parameters:
email
- email address- Returns:
- true if the URL is valid
-
getHostFromURL
-
getBaseURL
-
getObjectURI
Returns the default URL for a given domain object.- Parameters:
obj
- the domain objectincludeName
- true if we want to include the name of the object in the URLincludeId
- true if we want to include the ID of the object in the URL- Returns:
- the object's URL - e.g. /users/123-name, /users/, /users/123
-
getSystemProperty
Same asSystem.getProperty(java.lang.String)
.- Parameters:
name
- the name of the property- Returns:
- the property value
-
isJsonType
Checks if a response is of type JSON.- Parameters:
contentType
- the value of "Content-Type" header- Returns:
- true if JSON
-
singularToPlural
-
isBasicType
Checks if a class is primitive, String or a primitive wrapper.- Parameters:
clazz
- a class- Returns:
- true if primitive or wrapper
-
type
Returns the simple name of a class in lowercase (AKA the type).- Parameters:
clazz
- a core class- Returns:
- just the name in lowercase or an empty string if clazz is null
-
getAllDeclaredFields
Returns a list of all declared fields in a class. Transient and serialVersionUID fields are skipped. This method scans parent classes as well.- Parameters:
clazz
- a class to scan- Returns:
- a list of fields including those of the parent classes excluding the Object class.
-
getNewId
Distributed id generator. Relies on node/worker ids and datacenter ids to prevent collisions.- Returns:
- a long unique ID string of digits
-