Bringing some of the Kotlin goodness to java

Overview

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.
You might also like...
Web blog application written on Kotlin with Ktor framework

polytech-blog Web blog application written on Kotlin with Ktor framework Docker was used to create docker image Docker Compose was used to create cont

Bringing the best of the web to Android!
Bringing the best of the web to Android!

Android Geocities Theme Bringing the best of the web to native! Features 🚧 Cool "under construction" views 👍 ImageViews have their images replaced w

Bringing extended scrolling features to Android's native ListView and ExpandableListView.
Bringing extended scrolling features to Android's native ListView and ExpandableListView.

About QuickScroll QuickScroll is a library aimed at creating similar scrolling experiences to the native Contacts app's People view. It has the same s

Briar-desktop - A desktop program for Briar, bringing secure messaging to your desktop and mobile devices

Briar Desktop A desktop program for Briar, bringing secure messaging to your des

Bringing webhooks into Telegram. Push messages to chats via URL with a simple API.
Bringing webhooks into Telegram. Push messages to chats via URL with a simple API.

Webhooks over Telegram (WoT) WoT aims to provide a simple API for registering webhooks and sending messages to Telegram chats via them using a single

Some beautiful android loading drawable, can be combined with any view as the LoadingView or the ProgressBar. Besides, some Drawable can customize the loading progress too.
Some beautiful android loading drawable, can be combined with any view as the LoadingView or the ProgressBar. Besides, some Drawable can customize the loading progress too.

LoadingDrawable: Android cool animation collection 前言 CircleRotate源码解析 Fish源码解析 LoadingDrawable is some android animations implement of drawable: a li

Don't know what to do next? Don't worry, NEG or NotEnoughGoals will give you some help by giving you some goals to achieve to make skyblock less boring.

NotEnoughGoals Don't know what to do next? Don't worry, NEG or NotEnoughGoals will give you some help by giving you some goals to achieve to make skyb

Application that allows to search some products and display them in a list, also allows to add some product to the shopping cart and remove it
Application that allows to search some products and display them in a list, also allows to add some product to the shopping cart and remove it

Application that allows to search some products and display them in a list, also allows to add some product to the shopping cart and remove it

ArchGuard Scanner for scan Git change history, scan source code by Chapi for Java, TypeScript, Kotlin, Go..、Java bytecode use for JVM languages, scan Jacoco test coverage.

Arch Scanner Requirements: JDK 12 Scanner: scan_git - Git commit history scan scan_jacoco - Jacoco scan scan_bytecode - for JVM languages known issues

Provides Kotlin libs and some features for building Kotlin plugins
Provides Kotlin libs and some features for building Kotlin plugins

Kotlin Plugin Provides Kotlin libs and some features for building awesome Kotlin plugins. Can be used instead of CreeperFace's KotlinLib (don't use to

DS-for-Kotlin - Some classic data sturctures write in kotlin for fun

DS-for-Kotlin Just write some classic data structure by kotlin during my leisure

A Java serialization/deserialization library to convert Java Objects into JSON and back

Gson Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to a

A Java API for generating .java source files.

JavaPoet JavaPoet is a Java API for generating .java source files. Source file generation can be useful when doing things such as annotation processin

a 2d Java physics engine, native java port of the C++ physics engines Box2D and LiquidFun

jbox2d Please see the project's BountySource page to vote on issues that matter to you. Commenting/voting on issues helps me prioritize the small amou

LiteHttp is a simple, intelligent and flexible HTTP framework for Android. With LiteHttp you can make HTTP request with only one line of code! It could convert a java model to the parameter and rander the response JSON as a java model intelligently.

Android network framework: LiteHttp Tags : litehttp2.x-tutorials Website : http://litesuits.com QQgroup : 42960650 , 47357508 Android网络通信为啥子选 lite-htt

A gradle plugin for getting java lambda support in java 6, 7 and android

Gradle Retrolambda Plugin This plugin will automatically build your java or android project with retrolambda, giving you lambda goodness on java 6 or

I was fed up with writing Java classes to mirror json models. So I wrote this Java app to automate the process.

Json2Java I was fed up with writing Java classes to mirror json models. So I wrote this Java app to automate the process. What this tool can do right

enjarify 8.6 0.0 L5 Python Enjarify is a tool for translating Dalvik bytecode to equivalent Java bytecode. This allows Java analysis tools to analyze Android applications.

Note: This repository may be out of date. Future development will occur at https://github.com/Storyyeller/enjarify. Introduction Enjarify is a tool fo

CLI tool for decompiling Android apps to Java. It does resources! It does Java! Its real easy!

Easy-as-pie Android Decompiler Why One stop shop I got pretty tired of decompiling Android apps with a bunch of steps that I had to remember all the t

Releases(V1.0.1)
Owner
Alexandru Dochioiu
Alexandru Dochioiu
An android project presenting some transitions you can use between activities

ActivityTransition An android project presenting some transitions you can use between activities #Integration Add the anim folder to your Android proj

null 260 Nov 29, 2022
A Java library that models spring dynamics and adds real world physics to your app.

Rebound About Rebound is a java library that models spring dynamics. Rebound spring models can be used to create animations that feel natural by intro

Facebook Archive 5.5k Jan 9, 2023
A Java library that models spring dynamics and adds real world physics to your app.

Rebound About Rebound is a java library that models spring dynamics. Rebound spring models can be used to create animations that feel natural by intro

Facebook Archive 5.5k Dec 18, 2022
🎢 Zoom Recycler Layout Manager For Android Kotlin

Zoom Recyler Layout An beautiful Zoom Animation Library for RecyclerView Items in Android using Kotlin. Preview 1. Horizontal Scroll 2. Vertical Scrol

Sanju S 778 Jan 2, 2023
An simple & awesome animation library written in Kotlin for Android

An simple & awesome animation library written in Kotlin for Android

Romman Sabbir 53 Oct 17, 2022
Support extension to use Android ViewBinding quickly and smooth transform from kotlin-synthetic-extension.

ViewBinding Delegate Extension Support extension to use Android ViewBinding quickly and smooth transform from kotlin-synthetic-extension. Binding inst

Cuong V. Nguyen 3 Dec 13, 2021
Android Country Picker is a Kotlin-first, flexible and powerful Android library that allows to integrate Country Picker with just a few lines.

1. Add dependency dependencies { implementation 'com.hbb20:android-country-picker:X.Y.Z' } For latest version, 2. Decide your use-case

Harsh B. Bhakta 65 Dec 6, 2022
A mix of random small libraries for Kotlin, the smallest reside here until big enough for their own repository.

klutter Random small libraries, usually extensions making other libraries happier. Versions later than 2.x are for JDK 8 and newer only. Maven Depende

Kohesive 140 Nov 1, 2022
ConfigurNation - Annotation based Kotlin Interfaces for Android SharedPreferences

ConfigurNation Annotation based Kotlin Interfaces for Android SharedPreferences Installation Gradle Dependencies implementation 'com.izikode.izilib:co

Fanis Veizis 4 Jan 19, 2019
Special-waddle - MiniGDX Game Template With Kotlin

MiniGDX Game Template Create your first game using miniGDX by clicking the "Use

Dan Rousseau 0 Jan 10, 2022