Skip to content

ImSejin/common-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

3c13116 Â· Feb 13, 2025
Jan 9, 2025
Jan 2, 2025
Feb 13, 2025
Jan 4, 2025
Aug 18, 2021
Jan 4, 2025
Dec 9, 2020
Jan 9, 2025
Dec 26, 2024
Dec 26, 2024
Jan 2, 2025
Jan 2, 2025
Feb 12, 2025

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 java17

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

// Locale[ALBANIAN, ARABIC, BELARUSIAN, BULGARIAN, ...]
Locale[] languages = Locales.getLanguages();
// Locale[ALBANIA, ALGERIA, ARGENTINA, AUSTRALIA, ...]
Locale[] countries = Locales.getCountries();

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

// Platform[AIX, SOLARIS, LINUX, MACOS_ARM64, MACOS_X64, WINDOWS, UNKNOWN]
Platform platform = Platform.getCurrentPlatform();

assert platform.isCurrent();

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);