Skip to content

increff/commons-lang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

commons-lang

commons-lang provides a host of helper utilities for dealing with Collections, DateTime formats and Files among other things

Overview

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

Table of Contents

Installation

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>

Key Classes

CollectionUtil

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 a mapper 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'' objects
  • Collection merge(Collection list, Function<O, Collection> mapper) Similar to the toList 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 Collection
  • List merge(List<List> listOfLists) Merge a list of lists into a single list
  • Collection getDuplicates(Collection list, Comparator comparator) Find duplicated values (except first occurrence) in a Collection, based on a Comparator. Comparator is used to specify the field of an object to be used as reference for duplicate checking
  • Set getUnique(Collection list, Comparator<? super O> comparator): Returns Set of Unique values (and first occurrences of duplicated values) of a Collection, based on a Comparator

DateUtil

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 date
  • Date getDateForMonth(int month, int year) Convert a provided month, year pair to a Date object (Assuming first day of month). Months are indexed from 0, i.e. 0-January, 11-December
  • LocalDate getLocalDateForMonth(int month, int year) Convert a provided month, year to LocalDate object (Assuming first day of month). Months are indexed from 1, i.e.1-January, 12-December

CryptoUtil

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 a plaintext input String with keyStr used as key through AES encryption. Returns the encrypted text string
  • String decrypt(String ciphertext, String keyStr) Decrypts a ciphertext string encrypted through AES using keyStr as key

CmdUtil

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 the java -version command as follows CmdUtil.runCmd(new String[]{"''java''", "-''version''"}, ProcessBuilder.Redirect.INHERIT, ProcessBuilder.Redirect.INHERIT);

EvalEngine

Allows evaluation/execution of a script in ECMAScript/Javascript through Java

  • ScriptEngine getEngine(): Returns a ScriptEngine for executing Javascript code
  • Object eval(String script): Evaluates/Executes a provided JS Script and returns value returned on execution of script
  • Object eval(String script, Map<String, String> params): Evaluates/Executes a provided JS Script ''with params''

FileUtil

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 directory
  • File createTempFileWithExt(File dir, String ext): Create a temporary, in-memory file using just the extension (assigns random name) in specified directory
  • void deleteFile(String filePath): Quietly delete a file

FileFormatUtil

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

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

TimerUtil

A Timer Class that allows measurement of time duration through the following methods

  • void start(): Start a timer
  • void stop(): Stop the timer
  • long getDuration(): Get elapsed time between start and stop in milliseconds

License

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.

About

commons-lang provides a host of helper utilities for dealing with Collections, DateTime formats and Files among other things

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published