Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

alexengrig/lambdax

Repository files navigation

LambdaX logo

LambdaX 0.5.0 Maven CentralJavadocsGitHub

Build StatuscodecovCoverage StatusCodacy BadgeBCH complianceGitHub code size in bytes FOSSA Status

⚠️ This library is under experimental development - expect the major version ⚠️

This library contains utility classes with useful lambdas.

Features

LambdaX uses concepts of functional programming:

LambdaX provides the following:

Examples

Chain

  • Data flow:
    int integer = ChainX.of("some string")
            .map(String::length)
            .filter(length -> length > 0)
            .mutate(length -> holder[0] = length)
            .orElse(-1);

Collection

  • Get the value from Map and print it on the console:
    Map<Integer, String> map = new HashMap<>();
    map.put(1, "one");
    Holder<Map<Integer, String>> holder = new Holder<>(map);
// Plain Java
    Map<Integer, String> map = holder.get();
    String actual = map.get(key);
    System.out.println(actual);
// Chaining Style
    Optional.of(holder)
            .map(Holder::get)
            .map(m -> m.get(key))
            .ifPresent(System.out::println);
// LambdaX
    String actual = Optional.of(holder)
             .map(Holder::get)
             .map(MapX.get(key))
             .ifPresent(System.out::println);
  • Insert the value into Collection if it does not contain it:
    Collection<Integer> numbers = new ArrayList<>();
    Holder<Collection<Integer>> holder = new Holder<>(numbers);
    int value = 1;
// Plain Java
    Collection<Integer> target = holder.get();
    if (!target.contains(value)) {
        target.add(value);
    }
// Chaining Style  
    Optional.of(holder)
            .map(Holder::get)
            .filter(collection -> !collection.contains(value))
            .ifPresent(collection -> collection.add(value));
// LambdaX
    Optional.of(holder)
            .map(Holder::get)
            .filter(CollectionX.notContains(value))
            .ifPresent(CollectionX.onlyAdd(value));

Predicate

  • Filter holders by name (not null safe):
List<Holder<Item>> filterByName(List<Holder<Item>> list) {
    return list.stream()
            .filter(PredicateX.of(Holder<Item>::get)
                    .map(Item::getName) // NPE if item is null
                    .equal("Item name"))
            .collect(Collectors.toList());
}

and null safe variant:

List<Holder<Item>> filterByExistingName(List<Holder<Item>> list) {
    return list.stream()
            .filter(PredicateX.ofNullable(Holder<Item>::get)
                    .map(Item::getName)
                    .equal("Item name")
                    .orLie()) // return false if item or item name is null
            .collect(Collectors.toList());
}

Installation

Releases are available in Maven Central.

List of version changes.

Maven

Add this snippet to the pom.xml dependencies section:

<dependency>
  <groupId>io.github.alexengrig</groupId>
  <artifactId>lambdax</artifactId>
  <version>0.5.0</version>
</dependency>

Gradle

Add this snippet to the build.gradle dependencies section:

implementation 'io.github.alexengrig:lambdax:0.5.0'

Others

Other snippets are available in The Central Repository.

License

This project is licensed under Apache License, version 2.0.

FOSSA Status