Skip to main content

Enum Types in Scala - How to?

Java is well known for provinding Enum Types since its version 1.5. Powerful, simple to use, they make your code easy to understand, and provide a nice way to implement Singleton Objects, since an Java Application will have only one instance of the each Enum Type. Among some of its advantages Enums variables cannot have any other type other than those defined in the Enum class.

Let us model Gender in a Java Enum Type

Reviewing Java Type-Safe Enum Type

In the above code, there is only to options for any variable of type Gender: MALE or Female. Now  what if we want model Enum Types like this in other JVM Languages? Such as Scala, our focus today.

Scala, a Functional and yet Object Oriented language for the Java Platform has no built-in Enumeration type like Java's. Instead, it provides an abstract class Enumeration which can be used :


Popular posts from this blog

Always Use StringBuilder while concatenating Strings within loops

A common tendency of Java programmers is to always concatenate Strings using + operator. Which is actually very good, and simplifies the code by improves readability, since we would have to use StringBuilder.append(String), if the single + operator was not allowed.

In fact if we look in byte code generate from such concatenation style, we will see a StringBuilder being used to perform the action.

Check the JSL: JLS

Now, the point is, although this facility, you should not use the + operator in loop concatenation.


A new StringBuilder Object will be constructed at every single loop iteration (with initial value of str) and at the end of every iteration there will be concatenation with initial String (actually StringBuilder with initial value of str).
So you need to create StringBuilder by yourself only when you work with String concatenation in loop.

Let us procuce the evidence

First, run this code, and see how long it takes to be executed:

Now, bellow is the code you should stick with, …

Maven Tips... and Tricks

Maven, one of the central actors in the Java World, resposible for managing the building life-cycles of many projects, is full of little features, that sometimes we forget to explore.

Let us go straight away and take a look at some very useful Maven features that will make your builds shine.

From where it stopped
Sometimes it is needed to build a bunch of projects all together, artifact-a, artifact-b and so on. What do we usually do when one of them fail? Build it all again!

But not anymore:

By using this option you can run the build from the project that failed.

Two out of ten

Ok, suppose you have 10 projects, and you only want to build 2 of them, how would you do?

The option -pl will do the job

Multi-threaded Build

If in the machine you run the build you have many Cores, tou can take advantage of them by using the following option(it means 2 Threads per Core):

It is also possible to define 3 Threads per Core(T3C)

Skip your Tests when you want to

With a lot of tests to perform your complete…

When to use NOSQL - An opinion based post of 2014...

In the last weeks, I've been hearing a lot about Cassandra, and other NoSQL Solutions that were candidates to one of the projects I am working on. Which is currently set to function properly with a RDBMS solution - Oracle 11g.

I decided then to take a deeper look into those kind of solution, NoSQL solutions, and compare them with RDBMS solutions. This article is intended to help you understand NoSQL, and pick the solution that best fits your requirements and scenario. This article does not cover all the features of a Specific NoSQL solution, rather it shows the general scenario.

In order to fully understand NoSQL, let us first see some key concepts of distributed computer systems and storage systems.

What is NoSQL?

A NoSQL or Not Only SQL database provides a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases. Motivations for this approach include simplicity of design, horizontal scaling and finer contro…