https://github.com/alexheretic/fluent
Java 8 common library for fluent coding
https://github.com/alexheretic/fluent
Last synced: 4 days ago
JSON representation
Java 8 common library for fluent coding
- Host: GitHub
- URL: https://github.com/alexheretic/fluent
- Owner: alexheretic
- License: apache-2.0
- Created: 2015-03-09T23:29:26.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2020-12-22T13:04:35.000Z (over 4 years ago)
- Last Synced: 2025-04-04T08:03:01.308Z (26 days ago)
- Language: Java
- Size: 26.4 KB
- Stars: 6
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
Fluent
------
Fluent is an ultra-lightweight Java library to aid fluent coding in Java 8 onwards[](https://travis-ci.org/alexheretic/fluent)
[](http://mvnrepository.com/artifact/com.github.alexheretic/fluent)### Map Building
Classes to aid Map building in a declarative way. A bit nicer than the anonymous sub-class with initializer block approach, don't you think?```java
// simple usage, assuming someMap is a Map already declared
Map example = new Fluent.LinkedHashMap()
.append("key1", "val1")
.append("key2", "val2")
.appendAll(someMap);// nested structures declared all at once
Map users = new Fluent.HashMap<>()
.append("David", new Fluent.HashMap<>()
.append("customers", asList(
new Fluent.HashMap<>()
.append("name", "Darrel")
.append("age", 33),
new Fluent.HashMap<>()
.append("name", "John")
.append("age", 29)))
.append("keywords", asList("word1", "word2")))
.append("Karen", new Fluent.HashMap<>()
.append("customers", emptyList())
.append("keywords", asList("word33")));// fluent immutable maps
Map immutable = new Fluent.HashMap<>()
.append("one", 1)
.append("two", 2)
.append("three", 3)
.unmodifiable();
```### Checked Exception Handling With Functional Wrapping
Utility methods to wrap the propagation of a possible checked exception in an unchecked one.
Particularly useful in making fluent functional code involving checked exceptions not a horror to behold.```java
// in short Unchecker#unchecked lets you do this
unchecked(() -> methodThatThrowsACheckedException());// which is equivalent to
try {
methodThatThrowsACheckedException();
}
catch (RuntimeException | Error e) {
throw e;
}
catch (Throwable t) {
throw new RuntimeException(t);
}
```Dealing with checked exception mad code in lambdas is messy, take for example these methods
```java
// some Exception throwing instance
interface CheckMe {
void youMustHandleMyErrorCases() throws Exception;
List atLeastWriteThrowsAllOverYourCode() throws Throwable;
}// some exception throwing utility class
class ImportantUtility {
public static String dontIgnoreMeeeee(Integer i)
throws IOException, GeneralSecurityException, NoSuchMethodException {
/* some important logic ... */
}
}
```
This logic could cause the following mess in simple handler code
```java
List uncheckThrowerManual(CheckMe object) {
try {
object.youMustHandleMyErrorCases();
return object.atLeastWriteThrowsAllOverYourCode().stream()
.map(number -> {
try {
return ImportantUtility.dontIgnoreMeeeee(number);
}
catch (IOException | GeneralSecurityException | NoSuchMethodException ex) {
throw new RuntimeException(ex);
}
})
.collect(toList());
}
catch (RuntimeException | Error e) {
throw e;
}
catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}
```
We can clean this up with Java 8 & Fluent
```java
/*
* Unchecker#uncheckedGet returns a throwing supplier's result
* (wrapping checked in RuntimeExceptions by default)
* Unchecker#uncheck transforms a throwing lambda/method into a unchecked one
*/
List uncheckThrower(CheckMe object) {
return uncheckedGet(() -> {
object.youMustHandleMyErrorCases();
return object.atLeastWriteThrowsAllOverYourCode().stream()
.map(uncheck(ImportantUtility::dontIgnoreMeeeee))
.collect(toList());
});
}
```Fluent is licensed under the [Apache 2.0 licence](http://www.apache.org/licenses/LICENSE-2.0.html).
### Releases
2.0 is the current latest release, available at maven central. Requiring JDK 1.8 or later.
```xml
com.github.alexheretic
fluent
2.0```
### Changelog
Release 2.x
* Add Fluent.Map#unmodifiable()Release 1.x
* Fluent.Map classes
* Unchecker checked exception utility