public final class Utils extends Object
| Modifier and Type | Method and Description |
|---|---|
static 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 |
arrayJoin(List<String> arr,
String separator)
Joins a list of strings to String using a separator.
|
static String |
base64dec(String str)
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(String s)
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)
Compiles a mustache template with a given scope (map of fields and values).
|
static String |
escapeJavascript(String str)
Escapes JavaScript.
|
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 |
generateSecurityToken()
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.
|
static List<Field> |
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 |
getCurrentYear()
Returns the current year.
|
static String |
getHostFromURL(String url)
Returns the host part of the URL.
|
static HumanTime |
getHumanTime()
HumanTime - a relative time formatter.
|
static Utils |
getInstance()
Returns an instance of this class.
|
static Locale |
getLocale(String localeStr) |
static int[] |
getMaxImgSize(int h,
int w)
Returns the adjusted size of an image (doesn't do any resizing).
|
static String[] |
getMonths(Locale locale)
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 as
System.getProperty(java.lang.String). |
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 url)
Email validation.
|
static boolean |
isValidURL(String url)
URL validation.
|
static String |
markdownToHtml(String markdownString)
Converts Markdown to HTML.
|
static String |
md5(String s)
md5 hash function.
|
static String |
noSpaces(String str,
String replaceWith)
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 |
stripHtml(String html)
Strips all HTML tags from a string.
|
static long |
timestamp()
Java timestamp.
|
static String |
type(Class<? extends ParaObject> clazz)
Returns the simple name of a class in lowercase (AKA the type).
|
static String |
urlDecode(String s)
Decodes a URL-encoded string.
|
static String |
urlEncode(String s)
URL-encodes a string.
|
public static Utils getInstance()
public static HumanTime getHumanTime()
public static String md5(String s)
s - the string to be hashedpublic static String bcrypt(String s)
s - the string to be hashedpublic static boolean bcryptMatches(String plain, String storedHash)
plain - plain text stringstoredHash - hashed stringpublic static String generateSecurityToken(int length, boolean urlSafe)
length - the length of the generated tokenurlSafe - switches to a URL safe encodingpublic static String generateSecurityToken(int length)
length - the length of the generated tokenpublic static String generateSecurityToken()
public static String escapeJavascript(String str)
str - a javascript stringpublic static String stripHtml(String html)
html - HTML stringpublic static String markdownToHtml(String markdownString)
markdownString - Markdownpublic static String compileMustache(Map<String,Object> context, String template)
context - a map of fields and valuestemplate - a Mustache templatepublic static String abbreviate(String str, int max)
str - a stringmax - max lengthpublic static String arrayJoin(List<String> arr, String separator)
arr - a list of stringsseparator - a separator stringpublic static String stripAndTrim(String str)
str - a dirty stringpublic static String stripAndTrim(String str, String replaceWith)
str - a dirty stringreplaceWith - a string to replace spaces withpublic static String stripAndTrim(String str, String replaceWith, boolean asciiOnly)
str - a dirty stringreplaceWith - a string to replace spaces withasciiOnly - if true, all non-ASCII characters will be strippedpublic static String noSpaces(String str, String replaceWith)
str - a string with spacesreplaceWith - a string to replace spaces withpublic static String formatMessage(String msg, Object... params)
msg - a message with placeholdersparams - objects used to populate the placeholderspublic static String base64enc(byte[] str)
str - the byte arraypublic static String base64encURL(byte[] str)
str - the byte arraypublic static String base64dec(String str)
str - the encoded stringpublic static String formatDate(Long timestamp, String format, Locale loc)
timestamp - the Java timestampformat - the date formatloc - the locale instancepublic static String formatDate(String format, Locale loc)
format - the date formatloc - the locale instancepublic static int getCurrentYear()
public static long timestamp()
System.currentTimeMillis()public static String[] getMonths(Locale locale)
locale - the locale used for the months' namespublic static Locale getLocale(String localeStr)
localeStr - locale stringLocale instance from a locale string.public static int round(float d)
d - a floatpublic static String formatPrice(double price)
price - a pricepublic static double roundHalfUp(double d)
d - a doublepublic static double roundHalfUp(double d,
int scale)
d - a doublescale - the scalepublic static String abbreviateInt(Number number, int decPlaces)
number - a big integerdecPlaces - decimal placespublic static String urlDecode(String s)
s - a stringpublic static String urlEncode(String s)
s - a stringpublic static boolean isValidURL(String url)
url - a URLpublic static boolean isValidEmail(String url)
url - a URLpublic static String getHostFromURL(String url)
url - a URLpublic static String getBaseURL(String url)
url - a full URLpublic static String getObjectURI(ParaObject obj, boolean includeName, boolean includeId)
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 URLpublic static String getSystemProperty(String name)
System.getProperty(java.lang.String).name - the name of the propertypublic static int[] getMaxImgSize(int h,
int w)
h - an image heightw - an image widthConfig.MAX_IMG_SIZE_PX.Config.MAX_IMG_SIZE_PXpublic static boolean isJsonType(String contentType)
contentType - the value of "Content-Type" headerpublic static String singularToPlural(String singul)
singul - a wordpublic static boolean isBasicType(Class<?> clazz)
clazz - a classpublic static String type(Class<? extends ParaObject> clazz)
clazz - a core classpublic static List<Field> getAllDeclaredFields(Class<? extends ParaObject> clazz)
clazz - a class to scanpublic static String getNewId()
Copyright © 2018 Erudika. All rights reserved.