k2j-compat

Kotlin 2 Java Compat for Scope Functions

License

License

GroupId

GroupId

com.github.alexdochioiu
ArtifactId

ArtifactId

k2j-compat
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

k2j-compat
Kotlin 2 Java Compat for Scope Functions
Project URL

Project URL

https://github.com/AlexDochioiu/K2J-Compat
Source Code Management

Source Code Management

https://github.com/AlexDochioiu/K2J-Compat

Download k2j-compat

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.alexdochioiu/k2j-compat/ -->
<dependency>
    <groupId>com.github.alexdochioiu</groupId>
    <artifactId>k2j-compat</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.alexdochioiu/k2j-compat/
implementation 'com.github.alexdochioiu:k2j-compat:1.0.1'
// https://jarcasting.com/artifacts/com.github.alexdochioiu/k2j-compat/
implementation ("com.github.alexdochioiu:k2j-compat:1.0.1")
'com.github.alexdochioiu:k2j-compat:jar:1.0.1'
<dependency org="com.github.alexdochioiu" name="k2j-compat" rev="1.0.1">
  <artifact name="k2j-compat" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.alexdochioiu', module='k2j-compat', version='1.0.1')
)
libraryDependencies += "com.github.alexdochioiu" % "k2j-compat" % "1.0.1"
[com.github.alexdochioiu/k2j-compat "1.0.1"]

Dependencies

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

K2J-Compat

Bringing some of the Kotlin goodness to java

bintray License Build Status codecov

Installation with Android Gradle

// Add K2J-Compat dependency
dependencies {
    implementation 'com.github.alexdochioiu:k2j-compat:1.0.1'
}

Usage

1. Kotlin's .let{ } as .let()

import static com.github.alexdochioiu.k2jcompat.K2JCompat.take;

final String number = take("+44 77 1234 1234")
                .let((it) -> "0" + it.substring(4)) // 077 1234 1234
                .let((it) -> it.replaceAll(" ", "")) // 07712341234
                .unwrap(); // unwraps the result to String

2. Kotlin's ?.let{ } as ._let()

import static com.github.alexdochioiu.k2jcompat.K2JCompat.take;

final String nullStr = null;
final String number = take(nullStr)
                ._let((it) -> it.replaceAll(" ", "")) // this code will not run as 'it' is null
                .unwrap(); // returns null

3. Kotlin's .also{ } as .also()

import static com.github.alexdochioiu.k2jcompat.K2JCompat.take;

StringBuilder builder = new StringBuilder();
take(builder).also((it) -> it.append("Hello World!"));

4. Kotlin's ?.also{ } as ._also()

import static com.github.alexdochioiu.k2jcompat.K2JCompat.take;

StringBuilder builder = null;
take(builder)._also((it) -> it.append("Hello World!"));
// Note: using 'also' instead of '_also' will cause a null pointer exception when trying to append

5. Kotlin's .takeIf{ } as .takeIf()

import static com.github.alexdochioiu.k2jcompat.K2JCompat.take;

String phoneNumber = "889944";
Integer number = take(phoneNumber)
                .takeIf((it) -> it.matches("[0-9]+"))
                ._let(Integer::parseInt)
                .unwrap();

6. Kotlin's ?.takeIf{ } as ._takeIf()

import static com.github.alexdochioiu.k2jcompat.K2JCompat.take;

String phoneNumber = null;
Integer number = take(phoneNumber)
                ._takeIf((it) -> it.matches("[0-9]+"))
                ._let(Integer::parseInt)
                .unwrap();

Note: If you use int instead of Integer NPE will be thrown as java will try to unbox a null Integer to int

7. Kotlin's .takeUnless{ } as .takeUnless()

Similar to takeIf but with inverted logic

8. Kotlin's ?.takeUnless{ } as ._takeUnless()

Similar to _takeIf but with inverted logic

Known Limitations

  • Unlike Kotlin, Java has primitive data types which get boxed/unboxed automatically into objects when needed. However, NPE is thrown when Java tries to unbox a null Object to a primitive data type. This cannot be solved so a custom lint will be created to add a warning when using primitives are used.

Versions

Version
1.0.1
1.0.0