commons-lang provides a host of helper utilities for dealing with Collections, DateTime formats and Files among other things
The standard Java libraries fail to provide enough methods for manipulation of its core classes. commons-lang
provides some of these extra methods. Primarily, commons-lang
provides a host of helper utilities for dealing with Collections, Date-Time formats and Files among other things
To add a dependency on this library using Maven, use the following:
<dependency>
<groupId>com.increff.commons</groupId>
<artifactId>increff-commons-lang</artifactId>
<version>1.0.1</version>
<scope>test</scope>
</dependency>
CollectionUtil
is a Utility class for conversion of Collections to maps, lists, etc. The primary methods available in the class are
HashMap<K, T> toMap(Collection collection, Function<T, K> mapper)
Allows conversion of a Collection of arbitrary objects into a HashMap such that the objects are stored as the values of the HashMap. The keys corresponding to an object are determined by applying amapper
function to the object itself. In this way, we can create a HashMap of Objects where they keys are a specific field of the object and the value is the object itself e.g. a HashMap from ''person_name(String)'' to ''Person(Object)''Collection toList(Collection list, Function<O, V> mapper)
Collects values from given list of objects, based on given mapper, into a Collection object e.g.toList
can be used to generate a list of ''person_name'' fields from a list of ''Person'' objectsCollection merge(Collection list, Function<O, Collection> mapper)
Similar to thetoList
method, however,merge
is used for constructing a continuous list of field from Objects, when the field to be collected itself is a Collection. It returns all the collected lists as a single, merged and continuous lists (''not a list of collected lists'')Collection merge(Collection list1, Collection list2)
: Merges two collection of objects into one CollectionList merge(List<List> listOfLists)
Merge a list of lists into a single listCollection getDuplicates(Collection list, Comparator comparator)
Find duplicated values (except first occurrence) in a Collection, based on aComparator
.Comparator
is used to specify the field of an object to be used as reference for duplicate checkingSet getUnique(Collection list, Comparator<? super O> comparator)
: Returns Set of Unique values (and first occurrences of duplicated values) of a Collection, based on aComparator
Utility for performing Date-Time related operations. Primary functions are
boolean between(String date, String startDate, String endDate)
Determines whether a date value lies in an inclusive range of dates. Date inputs may be provided as Strings in the format ''YYYY-MM-DD''String getStartDate(String endDate, int durationInDays)
Returns a date after adding a specified duration to the provided date. Thus, can be used to get the date a certain duration (duration in days) before/after the specified dateDate getDateForMonth(int month, int year)
Convert a provided month, year pair to aDate
object (Assuming first day of month). Months are indexed from 0, i.e. 0-January, 11-DecemberLocalDate getLocalDateForMonth(int month, int year)
Convert a provided month, year toLocalDate
object (Assuming first day of month). Months are indexed from 1, i.e.1-January, 12-December
Utility to perform Encryption of Strings using AES Encryption. By Default, uses AES, 128 bit, ECB mode encryption with UTF-8 output encoding
String encrypt(String plaintext, String keyStr)
Encrypt aplaintext
input String withkeyStr
used as key through AES encryption. Returns the encrypted text stringString decrypt(String ciphertext, String keyStr)
Decrypts aciphertext
string encrypted through AES usingkeyStr
as key
Allows the execution of operating system processes through provided commands. Primary methods are
void runCmd(String[] cmd, Redirect out, Redirect error)
Runs an operating system command. cmd is the list of Strings signifying external program to execute along with its arguments (if any). out and error parameters denote the destination of process output/error. E.g. to get the Java version, we can ran thejava -version
command as followsCmdUtil.runCmd(new String[]{"''java''", "-''version''"}, ProcessBuilder.Redirect.INHERIT, ProcessBuilder.Redirect.INHERIT);
Allows evaluation/execution of a script in ECMAScript/Javascript through Java
ScriptEngine getEngine()
: Returns a ScriptEngine for executing Javascript codeObject eval(String script)
: Evaluates/Executes a provided JS Script and returns value returned on execution of scriptObject eval(String script, Map<String, String> params)
: Evaluates/Executes a provided JS Script ''with params''
Allows creation/deletion of temporary files and directories through the folllowing methods
File getTempDir(String dirName)
: Create a temporary directory with the specified name in the ''JAVA_TEMP_DIR'' location (if file doesn't already exists). The directory is created in-memory and temporarily.File createTempFile(File dir, String fileName)
: Create a temporary, in-memory file with given filename in the specified directoryFile createTempFileWithExt(File dir, String ext)
: Create a temporary, in-memory file using just the extension (assigns random name) in specified directoryvoid deleteFile(String filePath)
: Quietly delete a file
Converts a TSV (Tab Separated Value) File into a CSV File (ignoring the first row). Primary methods are
void tsvToCsv(File tsvFile, File csvFile)
: Converts a TSV file into a CSV file
Interner
allows the creation of a list of lists for containing arbitrary Objects. Each sub-list is referred to as a Bucket
and has a maximum load that it can contain. The Interner
may be instantiated by passing the two constructor arguments, capacity
and load
. The capacity
specifies the total number of objects to be accommodated and the load
specifies the maximum size per bucket (by default, capacity=256
, load=16
). Objects are inserted into a Bucket arbitrarily based on their hash code values. To insert an object into one of the Buckets, the following method is used
T intern(T t)
: Insert an element into a bucket based on Object's hash code. If object already exists, returns it
A Timer Class that allows measurement of time duration through the following methods
void start()
: Start a timervoid stop()
: Stop the timerlong getDuration()
: Get elapsed time between start and stop in milliseconds
Copyright (c) Increff
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.