Sunday 15 September 2019

Kotlin Code to break forEach inline loops

In General we are unable to break the forEach inline loops.  Please check the below tries and see the proper way to do so.

1
2
3
4
5
6
7
8
9
 var data = listOf<String>("Dev", "Prd", "Test", "Sbx", "demo")
    data.forEach {
        if (it.equals("Prd")) {
            println("I'm Selected $it")
            return
        }
        println("I'm in the list $it")
    }
    println("Done")

the above code produces the below output, if we use "return" on the line 5 we will be getting the below output, because that will abundantly break and come out of the method.  You can see the "Done" is not printed.

I'm in the list Dev
I'm Selected Prd

Hence to resolve we can try out with the label concept available in kotlin,  Since our programming language says there is no explicit label needed for the inline function. we can directly use it as "return@forEach".  But this will also lead to be failed, this produces the below output.

I'm in the list Dev
I'm Selected Prd
I'm in the list Test
I'm in the list Sbx
I'm in the list demo
Done

The highlighted text are unnecessary to this case.  In the below code snippet run block with label is used to break the loop as soon as we get the expected result and breaks the blocks and proceeds the normal flow function flow.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var data = listOf<String>("Dev", "Prd", "Test", "Sbx", "demo")
    run envBlock@{
        data.forEach {
            if (it.equals("Prd")) {
                println("I'm Selected $it")
                return@envBlock
            }
            println("I'm in the list $it")
        }
    }
    println("Done")

And will get the expected output.


I'm in the list Dev
I'm Selected Prd
Done

Happy Coding :-)

Saturday 17 August 2019

Kotlin Null Handling


- Kotlin Null Safe
- Here we can’t initialise or assign null value.
- If purposely we need to have null, it provides with Question mark.

val username: String? = null

Here we have 4 types of null handling shots.

1. Safe Call
2. Safe Call with let
3. Elvis Operator
4. Non-Null Assertion Operator.

1. Safe Call: It is represented by "?." and this facilitates to access the methods of the object.  In case the reference object or variable is null it will safely handle that and produce the return value as string as "null"

var username: String? = null
println("Username Length is : ${username?.length}")   // We won't get crash, it just print the value as null
username = "manakular"
println("Username Length is : ${username?.length}")

Output:


Username Length is : null
Username Length is : 9

2. Safe Call with let:  It is also similar to the Safe call, instead of accessing the method this allows you to execute the block of code and implicitly returns the last line of the block based on the code,  here "it" keyword will refer the concern data value.

    var username: String? = "manakularpondy"
    val status: String? = username?.let {
        println("Username is $it")
        if (it.length > 10) "Valid Input" else "Invalid length"
    }
    println(status)

Output:

Username is manakularpondy
Valid Input

In case, if we change the value to null  var username: String? = null  the status value will be also null.  So, in order to handle the failure i.e. null scenario we can take help of run block.


    var username: String? = null
    val status: String? = username?.let {
        println("Username is $it")
        if (it.length > 10) "Valid Input" else "Invalid length"
    }.run {
        "Need Input"
    }
    println(status)

Output:

Need Input

3. Elvis Operator: Hope we most of them aware we don't have ternary operator in kotlin programming, but similar to that we are having this elvis operator.  This will be denoted by ?: as mentioned on the below code snippet the username is initialised as null and once we trying to access the length of the variable we are all aware it going to be a null type.   Hence so we are accessing the username value via Safe call (?), then it sees the value as null our elvis operator helps to executes the or take the values after the ?:  Hence here we can see the length value as  0 even the username value is null.    

var username: String? =null
val length: Int = username?.length ?: 0
println("Length is $length")

Output:

Length is 0

4. Non-Null Assertion Operator: This need to handled very carefully, it is referred by using the double exclamation (!!).   Since you are 100% sure the object is non-null then you can use this else the value is null it will throws the null pointer exception.


var username: String? =null
val length: Int = username!!.length ?: 0
println("Length is $length")

The above code snippet throws the null pointer exception, because username object is null

Saturday 20 July 2019

Kotlin Code - Get IPV4 & IPV6 Address

The below code helps to get the IPv4 and IPv6 Address using Kotlin.

import java.net.NetworkInterface
import java.util.*

/*
* Author: Rajendhiran E
* Date: 21-Jan-2018
* */

object IPHelper {
    val IPV4Address: String
        get() = getIPAddress(true)

    val IPV6Address: String
        get() = getIPAddress(false)

    /**
     * Get IP address from first non-localhost interface
     * @param userIPV4 true=return ipv4, false=return ipv6
     * @return address or empty string
     */
    private fun getIPAddress(userIPV4: Boolean): String {
        try {
            Collections.list(NetworkInterface.getNetworkInterfaces()).forEach {
                Collections.list(it.inetAddresses).forEach {
                    if (!it.isLoopbackAddress) {
                        val sAddr = it.hostAddress
                        val isIPv4 = sAddr.indexOf(':') < 0
                        if (userIPV4) {
                            if (isIPv4) {
                                return sAddr
                            }
                        } else {
                            if (!isIPv4) {
                                val delim = sAddr.indexOf('%')
                                return if (delim < 0) sAddr.toUpperCase() else sAddr.substring(0, delim).toUpperCase()
                            }
                        }
                    }
                }
            }
        } catch (ignored: Exception) {
        }
        return ""
    }
}


println("IP Address V4 - ${IPHelper.IPV4Address}")
println("IP Address V6 - ${IPHelper.IPV6Address}")


Output  Console
IP Address v4 - 192.168.240.2
IP Address v6 - FE80:0:0:0:4D9:5C92:3433:8A6B

Happy Coding :-)

Saturday 25 May 2019

Kotlin Lambda Expression & HO Functions - Quick Reference

Lambda Expression: As we know, syntax of Kotlin lambdas is similar to Java Lambdas. A function without name is called anonymous function. For lambda expression we can say that it is anonymous function. A lambda expression is always surrounded by curly braces, argument declarations go inside curly braces and have optional type annotations, the code_body goes after an arrow -> sign. If the inferred return type of the lambda is not Unit, then the last expression inside the lambda body is treated as return value.

Higher-Order Function: In Kotlin, a function which can accept a function as parameter or can return a function is called Higher-Order function. Instead of Integer, String or Array as a parameter to function, we will pass anonymous function or lambdas. Frequently, lambdas are passed as parameter in Kotlin functions for the convenience.

class Operators {
    fun add(a: Int, b: Int) = a + b
    fun inc(a: Int) = a + 1
}

fun calculate(a: Int, b: Int, opr: (Int, Int) -> Int) = opr(a, b)
fun calculate(a: Int, opr: (Int) -> Int) = opr(a)

val addition = { a: Int, b: Int -> Operators().add(a, b) }
fun addition(a: Int, b: Int) = Operators().add(a, b)

val increment = { a: Int -> Operators().inc(a) }
fun increment(a: Int) = Operators().inc(a)

fun main() {
    println("Output: " + calculate(1, 2) { a, b -> Operators().add(a, b) }) // Lambda as param
    println("Output: " + calculate(1, 2, Operators()::add)) // Function as param - reference through its object
    println("Output: " + calculate(1, 2, addition)) // Lambda Expression as param
    println("Output: " + calculate(1, 2, ::addition)) // Function as param
    println("")
    println("Output: " + calculate(1) { a -> Operators().inc(a) }) // Lambda as param
    println("Output: " + calculate(1) { Operators().inc(it) })  //Lambda as param Since it is the only param so, `it` referenced here
    println("Output: " + calculate(1, Operators()::inc)) // Function as param - reference through its object
    println("Output: " + calculate(1, increment)) // Lambda Expression as param
    println("Output: " + calculate(1, ::increment)) // Function as param
}

Output: 3
Output: 3
Output: 3
Output: 3

Output: 2
Output: 2
Output: 2
Output: 2
Output: 2

Happy Coding :-) 

Tuesday 8 January 2019

View animation Expand and Collapse - Android

This Post helps to animate the android views expand and collapse like Expandable list view.

Find below the code snippet.

ViewAnimationUtils.java







 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class ViewAnimationUtils {

    int animDuration = 600;
 
    public static void expand(final View view) {
        view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final int targtetHeight = view.getMeasuredHeight();
        view.getLayoutParams().height = 0;
        view.setVisibility(View.VISIBLE);
        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                view.getLayoutParams().height = interpolatedTime == 1
                        ? ViewGroup.LayoutParams.WRAP_CONTENT
                        : (int) (targtetHeight * interpolatedTime);
                view.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        animation.setDuration(animDuration);
        view.startAnimation(animation);
    }

    public static void collapse(final View view) {
        final int initialHeight = view.getMeasuredHeight();
        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if (interpolatedTime == 1) {
                    view.setVisibility(View.GONE);
                } else {
                    view.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                    view.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        animation.setDuration(animDuration);
        view.startAnimation(animation);
    }
}

Example
TextView description;
Button expand, collapse;
expand.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
  ViewAnimationUtils.expand(description)
 }
});

collapse.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
  ViewAnimationUtils.expand(description)
 }
});