All posts by admin

Starting Stopping Kafka and ZooKeeper on Mac

Starting ZooKeeper

zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties

Start/Stop Kafka after starting up Zoo Keeper

kafka-server-start /usr/local/etc/kafka/server.properties

Creating Topic ‘Test’

open a terminal:

kafka-topics –create –zookeeper localhost:2181 -replication-factor 1 –partitions 1 –topic test

Creating Consumer

open a terminal:

kafka-console-consumer –bootstrap-server localhost:9092 –topic test –from-beginning

we wait for the producers to produce for topic ‘test’

Produce for our topic Test

kafka-console-producer –broker-list localhost:9092 –topic test

> haha
> hehe
> hoho

Now in your consumer terminal, you should see the texts come up

Kotlin Inheritance

ref – https://www.programiz.com/kotlin-programming/inheritance

Why do we inherit?

Suppose, in your application, you want three characters – a math teacher, a footballer and a businessman.

Since, all of the characters are persons, they can walk and talk. However, they also have some special skills. A math teacher can teach math, a footballer can play football and a businessman can run a business.

In each of the classes, you would be copying the same code for walk and talk for each character. This means you’d be implementing walk and talk three different times for our three characters.

And to make matters worse, if you want to add a new feature (i.e eat) you need to implement the eat three different times (once for each character!). This can easily become error prone (when copying) and duplicate codes.

It would be a lot easier if we had a Person class with basic features like talk, walk, eat, sleep, and add special skills to those features as per our characters. This is done using inheritance.

Using inheritance, now we don’t need to repeatedly implement the same code for walk(), talk() and eat() for each class. We just implement a class with all these common features. Then when we create each charaeter, we simlpy need to inherit from our common class.

So, for MathTeacher (derived class), you inherit all features of a Person (base class) and add a new feature teachMath(). Likewise, for the Footballer class, you inherit all the features of the Person class and add a new feature playFootball() and so on.

This makes your code cleaner, understandable and extendable.

Is A

It is important to remember: When working with inheritance, each derived class should satisfy the condition whether it is a base class or not.

In the example above, MathTeacher is a Person, Footballer is a Person.

You cannot have something like, Businessman is a Business.

Make it inheritable by using Open

By default, classes in Kotlin are final. A final class cannot be subclassed. By using the open annotation on a class, compiler allows you to derive new classes from it.

Hence, if we want to inherit from say class A, we MUST put open in front of class A.
We have a primary constructor

We then declare our characters. In our primary constructor, we extend from class Person.
We then pass in our primary constructor’s parameters age and name into our parent class Person.

Child primary constructor must use base

If the derived class has a primary constructor, the base class must be initialized using the parameters of derived class’s primary constructor.

The base class must be executed first. Then, the derived primary constructor executes.

In the above program, both derived classes have two parameters age and name, and both these parameters are initialized in primary constructor in the base class.

In our next example, we have base class Person. We want it to be the base class where other classes can derive from it so we use open.

We then give Person a constructor.

We then create Footballer which extends Person. In order for this extending to take place, we must put the base class behind our Footballer class like so:

This means Person constructor gets executed first.


output:
Person constructor (newAge, newName)
Football player Cristiano of age 29 and plays for LA Galaxy.

Child that has no primary constructor

In case of no primary constructor, each base class has to initialize the base (using super keyword), or delegate to another constructor which does that.

Constructor in Kotlin

ref – https://www.programiz.com/kotlin-programming/constructors

In Kotlin, a class can also contain one or more secondary constructors. They are created using constructor keyword.

The most common use of secondary constructor comes up when you need to extend a class that provides multiple constructors that initialize the class in different ways.

For example, you want to initialize your class with absolutely no parameters. And then other instances where you want to initialize it with multiple parameters.

In our Log example, we have two secondary constructors. No primary constructor.

Constructor calling constructor

In Kotlin, you can also call a constructor from another constructor of the same class using this().
In our case, say for a 1 parameter constructor, we call a 2 parameter constructor right after the one parameter constructor finishes running.

Over-riding rules in Kotlin

First, we have an abstract class. Remember that our abstract functions/properties MUST be over-ridden.

In order to use this abstract class, we can use an open class. An open class means it can be extended. It can also extend from abstract class Course. When we extend Course, we initialize its properties. We’re not happy with its default learn function, so we override it and implement it ourselves.

open in abstract means it is over-ridable. If there is no open keyword, then the default keyword ‘final’ applies.

As you can see, when we instantiate KotlinCourse, we execute learn function and see that it calls KotlinCourse’ learn function.

What we have a specialized class SpecializedKotlinClass that extends KotlineCourse?

By default, we can override the learn function in KotlinCourse. If we DO NOT override it, it will simply call KotlinCourse’s learn function.

However, say we don’t want people to over-ride KotlinCourse’s function. Any class that extends KotlinCourse MUST USE its learn function.

In order to prevent others to -override KotlinCourse’ learn function, simply put final override fun.
Thus, this prevents SpecializedKotlinClass from overriding the learn function. Doing so would give you a compiler error.

Multiple Super Types

Say we create another interface with a learn function:

And we extend both from abstract and interface…where both have function learn. When you use super, the compiler will be confused which super type you are referring to. In this case, we can use the bracket and the super type name to designate whether you want to use the interface or the abstract one.

interfaces in Kotlin

Interfaces define a contract to which different classes can follow. To do that they must override each function and property defined in the interface.

We first declare Buildable interface, which says whatever class conforms to me must declare:

1) val timeRequired of type Int
2) function build

Interfaces cannot have state. Thus, the properties you declare in your interface cannot be initialized.
Classes that conform to these interfaces must declare and initialize this themselves.

Besides declaring a Car type like so:

We can also have type of interface Buildable:

Which then we can only access the function build, and property timeRequired, as specified in the interface. If class Car conformed to other interfaces, we cannot access the functions of the other interfaces because our type is only for Buildable.

Say we a Truck class that also conforms to Buildable. Thus, we can simply have a reference of type Buildable, and call build on the objects like so

Now we are free to change the implementation of function build in class Car and Truck, without affecting this outer code, which does the building. In other words…we can freely change HOW the vehicle is built, without affecting the building of it.

Infix Functions in Kotlin

https://medium.com/makingtuenti/infix-functions-in-kotlin-2db3d3142dd2
https://www.programiz.com/kotlin-programming/infix-notation
https://github.com/http4k/http4k/blob/master/http4k-core/src/main/kotlin/org/http4k/routing/routing.kt

Say we have enums that represents the shapes of the cards.

We also have enums that represent the numbers (or ranking) of the cards.

In order to create a card we have to do the following:

We can simplify the way of creating a new card so we’re going to add a function called of to Rank
For enum class Rank, we create a function called of.
It takes in a parameter of Suit object.

We declare that our function takes in parameter of type Suit. When we instantiate Card, we use this to reference our Rank object, and then just have the user pass in suit

Then just use it like so:

‘this’ references Rank.Queen
‘suit’ references Suit.Hearts

We can still improve it if we use static imports for QUEEN and HEARTS to get the card creation closer to natural language. So you put the class enum in another class file.

For example, without static imports, we’d have to go

But WITH static imports, we don’t always have to reference Math anymore.

Hence, in our example, we can use static imports to simply access the enums value themselves:

This is much better, but we can do more.

Let’s convert the of function in an infix function. We only have to add the reserved word infix at the beginning of the function:

An infix function add to the function the ability to use it with infix notation.
We can now do this:

Given

from definition:

where http4k added functionality ‘bind’ to String.

(https://github.com/http4k/api/blob/master/org.http4k.contract/kotlin.-string/index.md)

In this example, we declare the infix function name to be bind.
‘this’ is the String itself.

Hence this is how we get “hello” bind

Notice in the infix fun String.bind that it takes in a parameter of type Method. This is how we have “hello” bind GET

So now using infix notation, we provided a string that gets bound to a Method called GET.
We want this to be paired with a provided anonymous function like so:

“hello” bind GET – string that identifies the HTTP Method.
to {….} – the string that identifies the HTTP Method should execute this anon function.

First, let’s see a simpler example.

We created a much simpler infix function tofunction

Now we see how a to { name: String -> “Welcome $name”} can actually execute a function.

Hence, it is similar way in http4. Http4’s object have set up the string hello to GET using infix fun ‘bind’.

“hello” bind GET to {request:Request -> Reponse…}

Then it uses infix function ‘to’ to have it execute with an anonymous function.

Its using syntactic sugar to execute many functions together on 1 line.

abstract vs interface

ref – https://medium.com/@eneszor95/big-challenge-in-kotlin-interface-vs-abstract-class-520cc234e7c1

Abstract let’s other classes inherit from the abstract class.
Interface, let’s others conform to it.

Interfaces can have abstract methods

We provide a base abstract function. If you over-ride it in your class, it will call it. Otherwise, it will default down to the base abstract function in your interface.

In our example, addMilk is over-ridden. But we did not implement addCoffee, so it calls CoffeeMachineInterface’s addCoffee.


output:
SuporCoffee – Adds Foamy Milk
CoffeeMachineInterface – Coffee is being added…

Interfaces can be properties…but need to be abstract


output:
set() on ‘coffeeAmount’ for SuporCoffee
get() on ‘coffeeAmount’ for SuporCoffee
cup1 has 16 oz of coffee

An interface can be inherited from other interface.

abstract classes cannot be instantiated. You must derive from it, and then instantiate it.

  • an abstract class Person is created. You cannot create objects of the class.
  • the class has a non-abstract property age and a non-abstract method displaySSN. IF you need to override these members in the subclass, they should be marked with open keyword.
  • The class has an abstract method displayJob(). It doesn’t have any implementation and MUST be overridden in its subclasses.

So we create class Teacher and override displayJob. Then when we instantiate it, we can access its own implementation of displayJob and the abstract’s final base function displaySSN.

Also, the practical side of abstract classes is that you can encapsulate a part of implementation that works with the state, so that it cannot be overridden in the derived classes.

Abstract properties can be initialized or abstract. When it is abstract, it cannot be initialized. It must be over-riden in the derived class

Property that are initialized must have keyword ‘open’

Abstract can hold state by using open and initializing a property. We then have the option of over-riding it in a derived class.