Skip to content

Commit

Permalink
Release v2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
evpl committed Feb 18, 2024
1 parent c7d1bfe commit 524d9d0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 2.1 (released 18.02.2024)

- added `Opt.throwIfEmpty` method
- added `JKScope.withResource` and `JKScope.letWithResource` static methods

## 2.0 (released 17.02.2024)

Big refactoring. Stable API.
Expand Down
68 changes: 40 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# JKScope

![Last version](https://img.shields.io/badge/last_version-2.0-blue "Last version")
[![Last version](https://img.shields.io/badge/last_version-2.1-blue)](https://github.com/evpl/jkscope)
[![Maven Central](https://img.shields.io/maven-central/v/com.plugatar.jkscope/jkscope)](https://central.sonatype.com/artifact/com.plugatar.jkscope/jkscope)
[![Javadoc](https://javadoc.io/badge2/com.plugatar.jkscope/jkscope/javadoc.svg)](https://javadoc.io/doc/com.plugatar.jkscope/jkscope)
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/evpl/jkscope/tests.yml?branch=main)
[![Javadoc](https://javadoc.io/badge2/com.plugatar.jkscope/jkscope/javadoc.svg?color=blue)](https://javadoc.io/doc/com.plugatar.jkscope/jkscope)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/evpl/jkscope/build.yml?branch=main)](https://github.com/evpl/jkscope)
[![Lines](https://sloc.xyz/github/evpl/jkscope/?category=lines)](https://github.com/evpl/jkscope)
[![Code lines](https://sloc.xyz/github/evpl/jkscope/?category=code)](https://github.com/evpl/jkscope)
[![Hits of Code](https://hitsofcode.com/github/evpl/jkscope?branch=main)](https://hitsofcode.com/github/evpl/jkscope/view?branch=main)

Java scope functions inspired by Kotlin

## Table of Contents
Expand All @@ -20,56 +24,40 @@ Java scope functions inspired by Kotlin
* [`letOpt`](#letopt)
* [JKScope static methods](#jkscope-static-methods)
* [`run`, `runCatching` and `runRec`](#run-runcatching-and-runrec)
* [`with`, `withInt`, `withLong` and `withDouble`](#with-withint-withlong-and-withdouble)
* [`with`, `withInt`, `withLong`, `withDouble` and `withResource`](#with-withint-withlong-withdouble-and-withresource)
* [`let` variations](#let-variations)
* [`Opt` monad](#opt-monad)
* [Unchecked functions](#unchecked-functions)
* [Examples](#examples)
* [Collection initialization](#collection-initialization)
* [Nth Fibonacci Number](#nth-fibonacci-number)
* [Nth Fibonacci number](#nth-fibonacci-number)

## Motivation

Inspired by the [Kotlin scope function](https://kotlinlang.org/docs/scope-functions.html) I want to reduce the number of
lines of my Java code and make this code more readable.

This library should have been written at least for this feature alone 😄

```
Map<String, Integer> map = let(new HashMap<>(), it -> {
it.put("val1", 1);
it.put("val2", 2);
});
```

It is also worth noting that all presented functions allow you to not process checked exceptions.

```
public static void main(String[] args) {
URI uri = let(() -> new URI("abc"));
}
```

## How to use

Java 1.8+ version required. The library has no dependencies. All you need is this (get the latest
Java 8+ version required. The library has no dependencies. All you need is this (get the latest
version [here](https://github.com/evpl/jkscope/releases)).

Maven:

```xml

<dependency>
<groupId>com.plugatar.jkscope</groupId>
<artifactId>jkscope</artifactId>
<version>2.0</version>
<version>2.1</version>
<scope>compile</scope>
</dependency>
```

Gradle:

```groovy
dependencies {
implementation 'com.plugatar.jkscope:jkscope:2.0'
implementation 'com.plugatar.jkscope:jkscope:2.1'
}
```

Expand Down Expand Up @@ -154,7 +142,7 @@ runRec(func -> {
});
```

#### `with`, `withInt`, `withLong` and `withDouble`
#### `with`, `withInt`, `withLong`, `withDouble` and `withResource`

These methods perform given function block on given values.

Expand All @@ -169,6 +157,8 @@ with(value1, value2, (v1, v2) -> {
});
```

`withResource` method does the same thing, but with a `AutoCloseable` resource and closes this resource.

#### `let` variations

`let` returns `Opt` instance of given value, `letNonNull` returns `Opt` instance of given value of given value or
Expand Down Expand Up @@ -216,11 +206,13 @@ int value = letIntRec(10, (n, func) -> {
int value = letWith("42", it -> Integer.valueOf(it));
```

`letWithResource` method does the same thing, but with a `AutoCloseable` resource and closes this resource.

### `Opt` monad

The `Opt` monad is similar in meaning to Java `Optional`, but allows the null value.

`Opt` monad contains standard `Optional` methods and scope functions methods:
`Opt` monad contains some `Optional` methods and scope functions methods:

```
String result = Opt.of("value").takeIf(it -> it.length() > 10).orElse("");
Expand All @@ -230,6 +222,16 @@ String result = Opt.of("value").takeIf(it -> it.length() > 10).orElseGet(() -> "
String result = Opt.of("value").takeIf(it -> it.length() > 10).orElseThrow(() -> new IllegalArgumentException());
```

### Unchecked functions

All presented functions allow you to not process checked exceptions.

```
public static void main(String[] args) {
URI uri = let(() -> new URI("abc"));
}
```

### Examples

#### Collection initialization
Expand Down Expand Up @@ -257,3 +259,13 @@ int value = letIntRec(10, (n, func) -> {
});
```

#### Method argument processing

```
public static String checkNonNullNonEmptyStr(String value) {
return let(value)
.takeNonNull().throwIfEmpty(NullPointerException::new)
.takeUnless(String::isEmpty).throwIfEmpty(IllegalArgumentException::new)
.get();
}
```

0 comments on commit 524d9d0

Please sign in to comment.