Saturday, 25 December 2021

Kotlin Delegation

In object-oriented programming, delegation refers to evaluating a member (property or method) of one object (the receiver) in the context of another original object (the sender).

Delegation is a design pattern in which an object handles a request by delegating to a helper object, called the delegate. The delegate is responsible for handling the request on behalf of the original object and making the results available to the original object.

Kotlin supports “delegation” design pattern by introducing a new keyword “by”. Using this keyword or delegation methodology, Kotlin allows the derived class to access all the implemented public methods of an interface through a specific object

Delegation should be used when:

  • When your subclass violates the Liskov substitution principle.  For example, when we are dealing with situations where inheritance was implemented only to reuse code of the superclass, but it is not really acting like it.
  • When the subclass uses only a portion of the methods of the superclass. In this case, it is only a matter of time before someone calls a superclass method that they were not supposed to call. Using delegation, we reuse only methods we choose (defined in the interface).
  • When we cannot or we should not inherit, because:
    • The class is final
    • It is not accessible and used from behind interface
    • It is just not designed for inheritance
Example: It will be very easy and self explanatory code snippet, here we have an interface for image loader and two other implementation with Glide and COIL Library.  There will be a delegation done through the ImageLoad class using "by" keyword.  

interface ImgSrcLoader {
    val loaderName: String
    fun onLoad()
    fun transition()
}

class GlideImpl(private val url: String, private val view: Any) : ImgSrcLoader {
    override val loaderName: String
        get() = "Glide"

    override fun onLoad() {
        println("Implementation with $loaderName : $url")
    }

    override fun transition() {
        println("Implementation with $loaderName transition Here")
    }
}

class CoilImpl(private val url: String, private val view: Any) : ImgSrcLoader {
    override val loaderName: String
        get() = "COIL"

    override fun onLoad() {
        println("Implementation with $loaderName : $url")
    }

    override fun transition() {
        println("Implementation with $loaderName transition Here")
    }
}

class ImageLoad(private val iLoader: ImgSrcLoader) : ImgSrcLoader by iLoader {
    /* This property change will not reflect/accessed on
     the super class implementations.*/
    override val loaderName: String
        get() = "NA"

    override fun transition() {
        if (iLoader is CoilImpl) {
            iLoader.transition()
            return
        }
        println("Transition with ${iLoader.loaderName} is restricted")
    }
}

fun main() {
    val url = "http://yourapp.photos.now/photo/0"

    ImageLoad(GlideImpl(url, "")).apply {
        transition()
        onLoad()
        println("ImageLoad: $loaderName")
    }

    ImageLoad(CoilImpl(url, "")).apply {
        transition()
        onLoad()
        println("ImageLoad: $loaderName")
    }
}

Output:

Transition with Glide is restricted
Implementation with Glide : http://yourapp.photos.now/photo/0
ImageLoad: NA
Implementation with COIL transition Here
Implementation with COIL : http://yourapp.photos.now/photo/0
ImageLoad: NA

Happy Coding :-)

Saturday, 13 November 2021

Vetoable - Delegates -- Kotlin

Vetoable, this allows us to modify the values when the argument input by the user(s) fulfills the specified condition, it can be used in place of observable properties if the user wants to intercept assignment. Vetoable is like Observable Properties with additional features to allows to modify and notify the values when the condition is met.

Simple example: 

The string which you never want to be reassigned with empty or to some specific text. 

The number values which is assigned should be match some condition.  

The generic observable kind of where the condition check will happen and then the value will be get assigned to the variable, incase if the condition fails, older value will be retained. 

var acceptOnlyEven: Int by Delegates.vetoable(0) { property, prevStr, nextStr ->
        nextStr.mod(2) == 0
}

var allowNonEmptyText: String by Delegates.vetoable("NA") { property, oldValue, newValue ->
        newValue.isNotEmpty()
}

var acceptStatus: String by Delegates.vetoable(STATUS_START) { property, oldValue, newValue ->
        listOf(STATUS_START, STATUS_INPROGRESS, STATUS_DONE).contains(newValue)
}

println("Str: $acceptStatus")
acceptStatus = "Status"
println("Str: $acceptStatus")
acceptStatus = ""
println("Str: $acceptStatus")
acceptStatus = "DonE"
println("Str: $acceptStatus")
acceptStatus = "Done"
println("Str: $acceptStatus")

println("Value: $acceptOnlyEven")
acceptOnlyEven = 10
println("Value: $acceptOnlyEven")
acceptOnlyEven = 5
println("Value: $acceptOnlyEven")

println("Str: $allowNonEmptyText")
allowNonEmptyText = "Status"
println("Str: $allowNonEmptyText")
allowNonEmptyText = ""
println("Str: $allowNonEmptyText")

Output:

Str: Start
Str: Start
Str: Start
Str: Start
Str: Done
Value: 0
Value: 10
Value: 10
Str: NA
Str: Status
Str: Status

Good Luck, Happy Coding :-)

Sunday, 17 October 2021

Kotlin getOrElse & getOrPut

 getOrElse: 

This provides safe access to elements of a collection. It takes an index and a function that provides the default value in cases when the index is out of bound.

getOrPut: 

This returns the value of the key.  If the key is not found in the map, calls the default value function, puts its result into the map under the given key and returns value of the same.

Code Snippet:

    data class Header(val heading: String, val subHeading: String)    

    val items = listOf("AAA", "BBB", "CCC", "DDD")
    val itemMap = mutableMapOf("x" to "AAA", "y" to "BBB")

    val header = listOf(
        Header("Heading1", "SubHeading1"), Header("Heading2", "SubHeading2")
    )

    println("Item at index 2 ${items.getOrElse(2) { "NA" }}")
    println("Item at index 4 ${items.getOrElse(4) { "NA" }}")
    println("Header at index 1 ${header.getOrElse(1) { "NA" }}")
    println("Header at index 2 ${header.getOrElse(2) { "NA" }}")
    
    println("Map Data: $itemMap")
    println("Map Key x ${itemMap.getOrElse("x") { "NA" }}")
    println("Map Key z ${itemMap.getOrElse("z") { "NA" }}")
    
    println("Map Key x ${itemMap.getOrPut("x") { "XXX" }}")
    println("Map Key z ${itemMap.getOrPut("z") { "ZZZ" }}")

    // Remove Map Key "x"
    itemMap.remove("x")

    println("Refreshed Map Data: $itemMap")
    println("Map Key x ${itemMap.getOrPut("x") { "AAA" }}")
    println("Refreshed Map Data: $itemMap")

Output:

Item at index 2 CCC
Item at index 4 NA
Header at index 1 Header(heading=Heading2, subHeading=SubHeading2)
Header at index 2 NA
Map Data: {x=AAA, y=BBB}
Map Key x AAA
Map Key z NA
Map Key x AAA
Map Key z ZZZ
Refreshed Map Data: {y=BBB, z=ZZZ}
Map Key x AAA
Refreshed Map Data: {y=BBB, z=ZZZ, x=AAA}

Happy Coding :-)

Sunday, 26 September 2021

groupBy vs associateBy in Kotlin

associateBy and groupBy function helps to build the maps from the elements of a collection indexed by the specified key. 

keySelector: The key is defined in the keySelector parameter. 

valueSelector: We can also specify an optional valueSelector to define what will be stored in the value of the map element, in case of not specifying the same it will consider the complete object as a value. 

The difference between associateBy and groupBy is about how they process the objects with the respective key

associateBy - It uses the last suitable element as the result value.

groupBy - it produces the list of all suitable elements and puts it in the result value(s).

P.S. The returned map preserves the entry iteration order of the original collection.

val boardingList = listOf(
        OnBoarding("1", "AAA", "Chennai", "Bang", "IT"),
        OnBoarding("2", "BBB", "Bang", "Hyd", "IT"),
        OnBoarding("3", "CCC", "Bang", "Chennai", "Finance"),
        OnBoarding("4", "DDD", "Hyd", "Pune", "Finance"),
        OnBoarding("5", "DDD", "Chennai", "Bang", "IT")
    )

// Param either be 'it' or field references, both does the same job.
println(boardingList.groupBy({ it.baseLocation }, { it.eName })) 
println(boardingList.groupBy(OnBoarding::baseLocation, OnBoarding::eName))

println(boardingList.associateBy({ it.baseLocation }, { it.eName }))
println(boardingList.associateBy(OnBoarding::baseLocation,OnBoarding::eName))

Output:

{Chennai=[AAA, DDD], Bang=[BBB, CCC], Hyd=[DDD]}
{Chennai=[AAA, DDD], Bang=[BBB, CCC], Hyd=[DDD]}
{Chennai=DDD, Bang=CCC, Hyd=DDD}
{Chennai=DDD, Bang=CCC, Hyd=DDD}

Good Luck, Happy Coding :-)

Sunday, 12 September 2021

DSL Code References

We should use DSL to simplify and improve the complexity of the app and make the code more readable, Before jumping into writing our own DSL we need to aware about the concept of lambda with receiver, invoke and operator.

Assume an example we have a class called Device, Please see below different ways how the object got initialized

class Device() {
    var id: Int = 0
    var name: String = ""
    var make: String = ""

    fun printDeviceInfo() {
        println("ID:  $id | Name: $name | Make: $make")
    }

    operator fun invoke(device: Device.() -> Unit): Device {
        device()
        return this
    }
}

Now, to instantiate the Class we can create an object like below ways,

// Traditional Way
    val env = Device()
    env.id = 102
    env.name = "5.4 GHz Wifi"
    env.make = "Amazon-Cable"
    env.printDeviceInfo()

// Invoke Way
    val en = Device()
    val obj = en {
        id = 102
        name = "5.4 GHz Wifi"
        make = "Amazon-Cable"
    }
    obj.printDeviceInfo()
    en.printDeviceInfo()

There is also other way where we are able to do without using invoke operator function, but here we can't use the traditional way of object creation. Since we pass it as parameter to the class primary constructor and followed by init method should call the same.

class Device(device: Device.() -> Unit) {
    var id: Int = 0
    var name: String = ""
    var make: String = ""

    fun printDeviceInfo() {
        println("ID:  $id | Name: $name | Make: $make")
    }

    init {
        device()
    }
}

val device = Device {
        id = 102
        name = "5.4 GHz Wifi"
        make = "Amazon-Cable"
    }
    device.printDeviceInfo()

Here we can also use it on the Data class also, but we should declare the constructor param as val/var, whereas it is not required on the normal model class like above.

data class User(val obj: User.() -> Unit) {
    var id: String = "NA"
    var age: Long = 0

    init {
        obj()
    }
}

fun main() {
    val userObj = User {}
    printUser(userObj)
    val usersList = listOf(userObj, User {
        id = "ABC567"
        age = 28
    }, User {
        id = "BCG678"
        age = 24
    }, User {
    })

    usersList[0].id = "BGH905"
    usersList.forEach(::printUser)
}

fun printUser(user: User) {
    println("Id: ${user.id}|| Age: ${user.age.takeIf { it > 0 } ?: "NA"}")
}

Good Luck :-) Happy Coding !!!

Sunday, 13 June 2021

Kotlin Collection - zip & unzip function

zip function builds one new list of paired elements from two existing arrays. Zipping transformation is useful to combine two array values. The final result is equal to the length of the smaller array if both arrays have different size. The extra elements of the larger array are not included in the final list.

Similarly, to do the reverse transformation, i.e. unzipping, unzip() method is called. In this post, Please refer to the below code snippet for the same. 

In the below example we are associating the states with their corresponding registration code, and in the unzip we are doing reverse the things.   

Here, while doing the zip we are getting the list of pairs, and for the unzip we are using the destructuing it with two separate list of strings. 






Saturday, 12 June 2021

Get Initials for the Display name - String - Android (Kotlin)

This post explains to extract the initial for the display name, i.e. Wanna get the first character of the first two words from the string, below is my code snippet can any one help to optimize it?

For eg: 

Username                         Display initial 

Rajendra prasad guru     RP

Rahul                                 R

Gurunath Desigan            GD


See in the above code snippet, We had tried out the logic in multiple ways, i.e. with Sequence (line 3 to 12) and without sequence (14 to 19).   Here we have added println to see how the internal iteration works, while you are about to use please remove that print statement. 

As per my knowledge all the three are good and optimized in their own way. 

The first one is seems to be optimum, since the split itself has the limit of  2, so it takes only the first 2 split with delimiters. 

This second one adds one more extra function take(2) functions to the logic and it reduces the internal iteration before doing the map transformation function.

The third one is made it as very simple approach, which deals with the split function without sequences and it uses both limit and take function (take function used on the joinToString()'s transforming expression as lambda). 

Sunday, 30 May 2021

Kotlin vs Java - String - Count lambda - Importance

In Kotlin we have count function which exactly provides the same output what we get in the length function.  here we have an overloading function too which takes predicate lambda (Char -> Boolean) as an input param.  

Lets see the below example and hope you will understand.  Here we are about to find the No. of times the character occurs in the string, will see the code snippet in both Kotlin and Java versions.

Kotlin: Type Inference and lambda expression.

val text = "Tester"
val findMe = 'e'
val count = text.count { it == findMe }
println("No. of $findMe in $text is $count")

Output: No. of e in Tester is 2

Java (Version< 8): In an optimized way, here we have written an extended logic by using recursive method and avoids the loops.  

  private static int countOccurences(String someString, char searchedChar, int index) {
        if (index >= someString.length()) {
            return 0;
        }

        int count = someString.charAt(index) == searchedChar ? 1 : 0;
        return count + countOccurences(
                someString, searchedChar, index + 1);
    }

long count = countOccurences("Tester",'e',0);
System.out.println(count);

Output: 2

Java 8: Here the Lambda expression and filter function takes lambda expression and returns the IntStream and from that we can call IntStream functions, here data type has to be explicit and those not type inferences. 

String str="Tester";
char findMe='e';
long count = str.chars().filter(ch -> ch == findMe).count();
System.out.println(count);

Output: 2

Code Snippet: In Kotlin this count function with predicate helps us on many things like password validation helpers  Please refer the below code . 

val password = "T1e st2er$#"
val digits = password.count { it.isDigit() }
val letters = password.count { it.isLetter() }
val lowercase = password.count { it.isLowerCase() }
val letterOrDigit = password.count { it.isLetterOrDigit() }
val uppercase = password.count { it.isUpperCase() }
val whiteSpaces = password.count { it.isWhitespace() }

println("Password: $password  ||| Length: ${password.count()}")
println("# of Digits: $digits")
println("# of Letters: $letters")
println("# of Lowercase char: $lowercase")
println("# of Letter or Digits: $letterOrDigit")
println("# of Uppercase char: $uppercase")
println("# of White Space: $whiteSpaces")

Output: 

Password: T1e st2er$#  ||| Length: 11
# of Digits: 2
# of Letters: 6
# of Lowercase char: 5
# of Letter or Digits: 8
# of Uppercase char: 1
# of White Space: 1

Sunday, 18 April 2021

Kotlin - Partition Function

Splits the original array / collections into pair of lists, where first list contains elements for which predicate yielded true, while second list contains elements for which predicate yielded false.

Please refer the below examples, here we used 2 filters to get the objects with certain crieteria, but it can be easily achieved through partition function, this executes the predicate and produce the pairs of list and we can access directly on pari's (first and second function) or we can destructure and use the same. 

data class Movies(val id: String,
                      val movieName: String,
                      val rating: Double = 1.0)

    val movies = listOf(Movies("100XA", "Guru", 4.5),
            Movies("100JK", "Ghab", 3.2),
            Movies("100HN", "Qualis", 1.2),
            Movies("1089O", "Tree"))

    // Using Filter
    println("### Rating Above 3 - Filter ###")
    movies.filter { it.rating > 3 }.forEach { println(it.movieName) }

    println("### Rating Not Above 3 - Filter ###")
    movies.filterNot { it.rating > 3 }.forEach { println(it.movieName) }

    // Using Partition - Destructuring the paris of list
    val (above3, notAbove3) = movies.partition { it.rating > 3 }
    println("\n### Rating Above 3 - Partition  ###")
    above3.forEach { println(it.movieName) }

    println("### Rating Not Above 3 - Partition  ###")
    notAbove3.forEach { println(it.movieName) }

Output:

### Rating Above 3 - Filter ###
Guru
Ghab
### Rating Not Above 3 - Filter ###
Qualis
Tree

### Rating Above 3 - Partition  ###
Guru
Ghab
### Rating Not Above 3 - Partition  ###
Qualis
Tree

Sunday, 7 March 2021

Kotlin Collection - Mathematic Sets Specific Operation - Union, Intersection & Subract

The Kotlin collections package contains extension functions for popular operations on sets: finding intersections, merging, or subtracting collections from each other.

To merge two collections into one, use the union() function. It can be used in the infix form a union b. Note that for ordered collections the order of the operands is important: in the resulting collection, the elements of the first operand go before the elements of the second.

To find an intersection between two collections (elements present in both of them), use intersect(). To find collection elements not present in another collection, use subtract(). Both these functions can be called in the infix form as well, for example, a intersect b

Code Snippet:

val universityDefCenters = listOf("Delhi", "Mumbai","Puducherry","Chennai")
val universitySouthCenters = listOf("Karnataka", "Puducherry","Chennai", "Kerala")

val allCenters= universityDefCenters union universitySouthCenters
val commonCenters = universityDefCenters intersect universitySouthCenters
val nonSouthCenters = universityDefCenters subtract universitySouthCenters

println("All Centers (Union): $allCenters")
println("Common Centers (Intersection): $commonCenters")
println("Non-South Centers (Subtract): $nonSouthCenters")

Output:
All Centers (Union): [Delhi, Mumbai, Puducherry, Chennai, Karnataka, Kerala]
Common Centers (Intersection): [Puducherry, Chennai]
Non-South Centers (Subtract): [Delhi, Mumbai]