Extension functions in kotlin

Kotlin language recently gained some traction owing to Google promoting it as a primary language for writing android apps. Kotlin has already been for a while in production owing to it’s creator JetBrains incorporating it in own tools, such as Intellij IDEA and so forth.

One, amongst many other neat features, is extension functions. In depth documentation can be found on official kotlin website.

About extension functions

In short – extension functions provide us useful tool to extend any existing class, no matter if it’s part of JDK, some 3rd party library or even own, custom ones.

Example, taken from this project, is the following one:

fun Random.betweenInclusive(lower: Int, upper: Int) = this.nextInt(upper - lower) + lower

Here we extend java.util.Random class from JDK, with betweenInclusive(lower: Int, upper: Int) extension method. This method helps us generate random integer between two numbers, lower and upper bounds, inclusive. Project contains couple of useful extension functions, implemented in rs/dodalovic/extension_functions/Students.kt kotlin file. Other examples are extending List<Student> with additional capabilities, taking advantage of functional programming applied on collections. Continue reading

Kotlin powered Spring Boot app consuming NBA Rest API

Ladies and gents, once again I’m sharing some sample app, this time we are building sample Spring Boot app implemented in Kotlin, talking to http://data.nba.net/ API to help us get some interesting, NBA related data. In this case we’re just listing NBA teams, but you extend this sample even further.

Creating project skeleton

The easiest way to create project skeleton is to navigate directly to Spring Boot Initializer website and download pre-configured project. In case you like kotlin – you can choose it from drop down with supported languages. If you’re using Intellij or Spring Tools Suite, you can achieve the same from within your IDE.

Application entry point

KotlinNbaApplicaiton.kt is our application entry point, it’s just a standard kotlin class containing main method and being annotated with @SpringBootApplication.

package rs.dodalovic.nba

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

class KotlinNbaApplication

fun main(args: Array<String>) {
    SpringApplication.run(KotlinNbaApplication::class.java, *args)

Handling request with TeamController

Continue reading

Inject language / reference feature of Intellij IDEA

One of very neat features of Intellij IDEA that I came across is injecting language / reference feature into String object. Let’s jump to code straight away to do simple example:

package rs.dodalovic.inject_reference;

public class InjectReferenceMain {
    public static void main(String[] args) {
        final String html = "";

Continue reading

Eclipse to Intellij IDEA migration

Ladies & gents, here are some impressions after switching to Intellij IDEA. Prior to that, I’ve been using Eclipse IDE for more than 10 years.It took 3 days to get basic efficiency with IDE. After 10 days of using it I decided to forget I was using Eclipse ever.

I could say IDEA is exactly what one developer needs. It is context aware (if you’re using code assistance in Java servlet it won’t offer you classes from Swing packages, or other nonsense). Refactoring support is incredible. It also warns you for some common mistakes while you’re coding (duplicate code, unused references, possible null pointers…). There’s also no need to save your file, forget about ctrl + s combination, your changes are auto-saved as you type. Continue reading