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'
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
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
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
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
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