Common Java utilities for everyday programming, compiled with Java 11.
- Author: Abdul Habra
Helper function to handle collections.
copy(C from, C to)
: Copy a collection to another of the same typeC copy(C from)
: Make a copy of a collectionboolean contains(Collection<T> col, T target, BiPredicate<T,T> matcher)
: Check if a collection contains a target value using given matcherboolean contains(Collection<T> col, Predicate<T> matcher)
: Check if a collection contains any value that matches.boolean isEmpty(Collection<T> c)
: Check a collection is null or emptyMap<K, V> toMapByKey(String key, Collection<V> col)
: Convert a collection to a map using the given field's name as keyMap<K, V> toMapByKey(String key, V... col)
: Convert a collection to a map using the given field's name as key
A list where each item is a pair (tuple)
Handy functions to search lists
ListSearcher(List<T> source)
: define the list that will be searchedstartIndex(int startIndex)
: Specify the search start indexmaxIndex(int maxIndex)
: Specify the search max indexreset()
: reset startIndex to 0, maxIndex to size, and matcher to nullmatcher(BiPredicate<T,T> matcher)
: define a matcher to be used when searchingequalsMatcher()
: use a standard equality matcher. This is the default matcherList<T> subList()
: get a sub-list between start and max indexesList<T> slice(int count)
: get a slice fromstartIndex
for a given count of elementsint indexOf(T target)
: Find the index of first element that matches given targetint lastIndexOf(T target)
: Find the index of last element that matches given targetint indexOfAny(Collection<T> targets)
: Find the index of the first element that matches any element in targetsindexOfAny(T... targets)
: Find the index of the first element that matches any element in targetsint indexOfSubList(List<T> target)
: Find index of the first occurrence of the given target listint indexOfSubList(T... target)
: Find index of the first occurrence of the given target listboolean isPrefix(List<T> target)
: Check if given target is a prefix to the source listboolean isPrefix(T... target)
: Check if given target is a prefix to the source listT getLast()
: get the last element in the listListSearcher<T> setLast(T value)
: set the value of last element in the listList<T> left(int count)
: get the left mostcount
elements of the listList<T> right(int count)
: get the right mostcount
elements of the list
File handling functions
remove(String filePath)
: remove a filewrite(String filePath, String text)
: write text to given fileappend(String filePath, String text)
: append text to given fileString readAsString(String filePath)
: read content of given file as a stringList<String> readLines(String filePath)
: read content of given file as a list of lines
Resource handling functions
InputStream readAsInputStream(String resourceName)
: Read content of a resource to an input streamString readAsString(String resourceName)
: Read content of a resource as a stringProperties readAsProperties(String resourceName)
: Read content of a resource into aProperties
objectList<String> readLines(String resourceName)
: read content of given resource as a list of lines
Yaml files handling
Map<String, String> readFile(String resourceName)
: Read a yaml file from resources into a Map.
Math related classes
Represent an exact decimal number
Represent a number in an easy to read string
Reflection related classes
Access the fields of an object
Access the methods of an object
Access the properties of an object
<R> R get(String propertyName)
: get the value of a property, including nested properties, for examplecustomer.address.getCity
Access fields, methods, and properties on an object
An enumeration of possible scopes: private
, protected
, public
, and package
.
String handling classes
String escapeUrl(String s)
: Escape a url using utf8.String unescapeUrl(String s)
: Unescape a url using utf8.
Search a string
String replaceBetween(String text, String start, String end, String with)
: replace a substring between a start and end markers.String clearBetween(String text, String start, String end)
: remove a substring between a start and end markers.
Make it easy to convert an object to a string
Time handling classes
Represent time as a tuple of (year, month, day, hour, minute, second, nano).