Most used short code snippets generically designed.
The goal is to avoid NullPointerException
and IndexOutOfBoundException
, still providing meaningful outputs.
All the methods in this library accepts Nullable
s and gracefully performs alternate actions defined.
This is overall top level usage doc, detailed docs are present as javadoc within code files
Gradle:
compile 'in.yajnesh.util:java-util:1.0.0' //required for android-util
compile 'in.yajnesh.util:android-util:1.0.0'
Friendly APIs to get item from any Sequence.
It is bettet to get null instead of crashing with NullPointerException
or IndexOutOfBoundException
This will try to get 6th item from sequence.
Sequence can be any of the following
- Array
- List
- String
- CharSequence
- ArraySet
- ViewGroup
- JSONArray
Return value will be
- In case sequence is null, null will be returned instead of crashing
- In case of index is above the size of negative index, null will be returned instead of crashing
This is similar to the above api, but instead of null values, this will return defaultvalue provided
Friendly APIs to identify if elements/sequence is empty.
This will return true if something is empty or null
parameter something can be any of the following
- Array
- Collection
- CharSequence
- Map
- String
- EditText
- TextView
- Editable
- JSONArray
- Bundle
- PagerAdapter
Return value will be
- In case parameter is null, true will be returned instead of crashing
- In case parameter is empty(decided by underlaying datastructure) , return true
- Return false, in all other cases
Friendly APIs to get the size of element/sequence.
This will return the size of something
parameter something can be any of the following
- Array
- Collection
- String
- CharSequence
- ViewGroup
- ArraySet
- Bundle
- JSONArray
Return value will be
- In case parameter is null, 0 will be returned
- if not null, then based on the underlaying datastructure, size will be returned
Friendly APIs to get item from any Sequence.
It is bettet to get null instead of crashing with NullPointerException
or IndexOutOfBoundException
This will try to get last item from sequence.
Sequence can be any of the following
- Array
- List
- String
- CharSequence
- ArraySet
- ViewGroup
- JSONArray
Return value will be
- In case sequence is null, null will be returned instead of crashing
- In case of index is above the size of negative index, null will be returned instead of crashing
This is similar to the above api, but instead of null values, this will return defaultvalue provided
Compare two obects for equality
This will return true if both objects are equal
parameter something can be any of the following
- Any Object
Return value will be
- In case of either parameter is null, false will be returned
- In case of both parameters are null, false will be returned
- If objects are equal , true will be returned, false otherwise
This is similar to the above api, but ignores the cases while comparing
Does source has searchTerm in it?
true if searchTerm is found in source, otherwise false. Also false if either or both params are null
This is similar to the above api, but ignores the cases while checking
TypeCast and object to passed class
In this case, safely typecasts object to Integer and returns an Integer
Get string out of Collection
returns string representation of collection/map
This is similar to the above api, adds preText infront of the string representaion
Is any object passed is null or empty.
returns true, if any of the object passed is null or empty, false otherwise
Are all the object passed is null or empty.
returns true, if all of the object passed is null or empty, false otherwise
Get string from given object
This will return string representaion of the parameter
parameter something can be any of the following
- Any Object
- EditText
- TextView
Return value will be
- String contained under the object, or null
This is same as above, but returns empty string in case input in null
Translates a string into x-www-form-urlencoded.
returns the encoded url
Validate common string patterns
This will return true if the parameter is a valid email
Return value will be
- True, if the parameter is a valid email
This will return true if the parameter is a valid phone number
Return value will be
- True, if the parameter is a valid phone number
Version related info
Returns displayable styled text from the provided HTML string.
Better Logger class with multipurpose use. Supports the following types of logs. Notice that tag is omitted, calling class name is automatically detected and put as tag. All of these operations are null
safe.
ALog.v(msg);
ALog.i(msg);
ALog.d(msg);
ALog.w(msg);
ALog.e(msg);
ALog.wtf(msg);
retrns the msg itself.
ALog.e(context,resId);
retrns the string msg.
ALog.e(msg,throwable);
retrns the msg itself.
ALog.enableFileLogging(true)
This will write(append) all logs to a file
ALog.setLogFileName("myLog.txt");
filename or filePath, Eg: "myLog.txt" , "my/directory/myLog.txt"
ALog.setLogLevel(LogLevel.NONE)
only the set log level and the levels above it will be printed.
Possible values are,
- LogLevel.WTF
- LogLevel.ERROR
- LogLevel.WARN
- LogLevel.INFO
- LogLevel.DEBUG
- LogLevel.VERBOSE
- LogLevel.NONE
ALog.enableAutoDetectionOfCaller(true/false);
Prefix to be added before each log.
ALog.setLogPrefix(string);
ALog.writeToFile(string);
Show a short toast
ALog.toast(context,string);
Show a short toast
ALog.toast(context,resId);
Convert most common collection to json and vice-versa
Converts map to json
parameter map can be any of the following
- Any Map
Return value will be
- Converted json data
Converts json object to map
parameter something can be any of the following
- String representation of json
- JSONObject
Return value will be
- Converted Map
Converts set to json array
parameter set can be any of the following
- Any Set
Return value will be
- Converted json array
Converts json array to set
parameter something can be any of the following
- String representation of json array
- JSONArray
Return value will be
- Converted Set