Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

toolisticon/kotlin-java-interop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

257 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kotlin-java-interop

Extensions for kotlin/java interoperability

incubating Build Status sponsored Maven Central

When you are writing libraries and tools in kotlin that are supposed to also be used from java, it is a good idea to use the java Functional interfaces (Function, Consumer, Supplier, ...) instead of kotlin lambda functions, so java developers can easily use and implement your features.

But this means, that you also have to work with java syntax when you are using the lib from kotlin. The kotlin-java-interop library aims to combine the best of those two worlds: expose your features as java functions, but still have a convenient access when working with kotlin.

Features

invoke() for java.util.function.*

This is mainly achieved by providing operator fun ... invoke() extensions for the most common java interfaces.

You can implement a feature class in kotlin, exposing a java interface:

val multiply = BinaryOperator<Int> { a,b -> a * b }

and use it from java as usual

Integer fourTimesFive = multiply.apply(4, 5); 

because multiply really is a java binaryOperator, but when using kotlin, leverage this lib and write:

import io.toolisticon.kotlin.JavaUtilFunctionExt.invoke

val fourTimesFive = multiply(4, 5) 

as if it would be a (Int,Int) -> Int lambda function.

Design

This lib is designed to be as usable as possible, without being too invasive to your code base and dependency tree. Consequentially, it does not require any transitive dependencies. It just uses the core features provided by JDK 11.x and Kotlin 1.5.x.

About

Extensions for kotlin/java interoperability

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages