项目作者: alexengrig

项目描述 :
Useful Java 8 Lambdas
高级语言: Java
项目地址: git://github.com/alexengrig/lambdax.git
创建时间: 2019-07-03T20:49:56Z
项目社区:https://github.com/alexengrig/lambdax

开源协议:Apache License 2.0

下载


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

Lambda**X** uses concepts of functional programming:

Lambda**X** provides the following:

Examples

Chain

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

Collection

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

Predicate

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

and null safe variant:

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

Installation

Releases are available in Maven Central.

List of version changes.

Maven

Add this snippet to the pom.xml dependencies section:

  1. <dependency>
  2. <groupId>io.github.alexengrig</groupId>
  3. <artifactId>lambdax</artifactId>
  4. <version>0.5.0</version>
  5. </dependency>

Gradle

Add this snippet to the build.gradle dependencies section:

  1. 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