Skip to content

ImSejin/common-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧰 Common Utils

Common utilities for java programming

GitHub Actions Workflow Status Codecov branch Maven Central
Sonarcloud Quality Gate Status Sonarcloud Maintainability Rating Codacy grade jdk8

Getting started

Maven

<dependency>
    <groupId>io.github.imsejin</groupId>
    <artifactId>common-utils</artifactId>
    <version>x.y.z</version>
</dependency>

Gradle

implementation 'io.github.imsejin:common-utils:x.y.z'

What's inside

Assertions

List<LocalDate> dates = Arrays.asList(
        LocalDate.of(1999, 12, 31), LocalDate.of(2000, 1, 1), LocalDate.of(2001, 1, 2));

Asserts.that(dates)
        // You can describe error message on assertion failure.
        .describedAs("dates should not be null or empty")
        // You can set what exception will be thrown on assertion failure.
        .thrownBy(IllegalStateException::new)
        // First of all, you have to make sure that variable to be asserted is not null,
        // before call the other assertion methods. Otherwise, it might throw NullPointerException.
        .isNotNull()
        .isNotEmpty()
        .hasSize(3)
        .is(them -> them.get(2).getYear() == 2001)
        .describedAs("dates should not have duplicated elements: '{0}'", dates)
        .doesNotHaveDuplicates()
        .describedAs("dates should contain '2000-01-01' or '2001-01-01': '{0}'", dates)
        .containsAny(LocalDate.of(2000, 1, 1), LocalDate.of(2001, 1, 1))
        .describedAs("dates should not have date in leap year: '{0}'", dates)
        .anyMatch(LocalDate::isLeapYear)
        // Target of assertion is changed from List to Integer.
        .asSize()
        .isPositive()
        // Assertion will fail and throw IllegalStateException on this step.
        .isGreaterThan(3);

Constants

// OS[LINUX, MAC, AIX, SOLARIS, WINDOWS, OTHER]
OS os = OS.getCurrentOS();

assert os.isCurrentOS();

// -----------------------------------------------------------------------------

// DateType[DATE, TIME, DATE_TIME, ALL, F_DATE, F_TIME, ...]
LocalDateTime dateTime = LocalDateTime.of(LocalDate.of(2000, 1, 1), LocalTime.of(12, 34, 56))
String formatted = dateTime.format(DateType.DATE_TIME.getFormatter());

assert formatted.equals("2000-01-01 12:34:56");

Tools

Stopwatch stopwatch = new Stopwatch(TimeUnit.MILLISECONDS);

stopwatch.start("First task");
TimeUnit.SECONDS.sleep(2);
stopwatch.stop();

stopwatch.start("Second task");
TimeUnit.SECONDS.sleep(1);
stopwatch.stop();

stopwatch.getTotalTime(); // About 3000.0 ms
stopwatch.setTimeUnit(TimeUnit.SECONDS);
stopwatch.getTotalTime(); // About 3.0 sec

Utilities

int[][] numbers = {{0, 1}, null, {2}, {}, {3, 4, 5}};
Integer[][] integers = (Integer[][]) ArrayUtils.wrap(numbers);
int[][] ints = (int[][]) ArrayUtils.unwrap(integers);

assert Objects.deepEquals(ints, numbers);

// -----------------------------------------------------------------------------

List<Character> greekAlphabets = Arrays.asList('Α', 'Β', 'Γ', 'Δ', 'Ε', 'Ζ');

// [['Α', 'Β', 'Γ'], ['Δ', 'Ε', 'Ζ']]
List<List<Character>> bySize = CollectionUtils.partitionBySize(greekAlphabets, 3);
// [['Α', 'Β'], ['Γ', 'Δ'], ['Ε', 'Ζ']]
List<List<Character>> byCount = CollectionUtils.partitionByCount(greekAlphabets, 3);