Popularity
0.7
Growing
Activity
0.0
-
9
1
1

Description

Kotlin's scope functions (let, also, takeIf and takeUnless) and the safe call operator are now available to Java.

Programming language: Java
License: Apache License 2.0
Tags: Kotlin     Android     Java     Android-library     Compatibility     Let     Also    
Latest version: v1.0.1

K2J-Compat alternatives and similar packages

Based on the "Kotlin" category.
Alternatively, view K2J-Compat alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of K2J-Compat or a related project?

Add another 'Kotlin' Package

README

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.


*Note that all licence references and agreements mentioned in the K2J-Compat README section above are relevant to that project's source code only.