Posts

String Encodings? Unicode? ASCII? UTF-8? ãéîöû?

Image
4 years of my career as a Software Engineer and I was able to put off the topic of character encoding and somehow sleep peacefully at night everyday until last night. A piece of python code that I had written broke and the error was UnicodeEncodeError. My reaction was similar to the title of this post. The last night's sleeplessness was not because I had a broken code but because why I had put off something which is needed every time I worked with strings. ASCII: The time when strings used to be simple! Just like aliens were only sighted in America, computers too were sighted only in America (or at least only in English speaking countries). To deal with strings, only English characters were standardised as ASCII and they were mapped to each number between 32-127. The first 32 numbers were mapped to control characters(number 7 made your computer beep). Since computers worked with 8 bit word size then, characters could be represented eas...

Euler Path in a graph using Hierholzer's algorithm

We will discuss how to find the Euler path(path is a sequence of vertices connected by edges) in a graph. The assumption we are taking in this article is, the given graph is connected and there is a Euler path in the given graph, although we will discuss a criteria which will help us know if there is any Euler path in graph. Moreover, we will present the pseudo code of one of the algorithm and pick up a problem and solve it using the algorithm discussed in the aricle. Intro: Definition: Euler path is a path such that we travel all the edges of the graph exactly once (repetion of vertices is allowed). Euler path example In the above example, one of the Euler path is: 1,2,3,4,2,6,2,5 Euler path can be a closed path i.e. a cycle also called Euler circuit or it can be an open path as in the above example aslo called Euler walk. Prerequisite Degree of a ve...

Enjoy quickies with supplyAsync by knowing how to handle exceptions.

PROBLEM: I have a method:  public String getChuckNorrisFact(); This method may also throw an exception, in my case IOException because this method does a service call to get a fact about Chuck Norris from web. I learned about  CompletableFuture  and now I am ASYNCING the shit out everywhere. Here is what I did: CompletableFuture<String> factFuture  = CompletableFuture.supplyAsync(() -> getChuckNorrisFact()); But the compiler tells me to handle or throw the exception, so I do this: CompletableFuture<String> factFuture2 = CompletableFuture.supplyAsync(() -> { try  { return getChuckNorrisFact(); } catch (IOException e ) { e .printStackTrace(); } }); Sadly there is still a compilation error in this. The supplier(lambda expression) in supplyAsync()  needs to return a string but there is no return statement if an exception occurs. So I do this: CompletableFuture...

Singletons!

Singletons! Five different implementations of singleton design pattern in Java: Singleton class with eager initialisation of object. 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 /* * The object is initialized while the class is being loaded * If your singleton class is not using a lot of resources, this is the approach to use * No option for exception handling while object creation. */ public class SingletonEagerInitialisation { private String name ; @Override public String toString () { return "SingletonEagerInitialisation [name=" + name + "]" ; } public String getName () { return name ; } public void setName ( String name ) { this . name = name ; } private static SingletonEagerInitialisation singletonObject = new SingletonEagerInitialisation (); public st...

A Generic method to convert ResultSetFuture (of Datastax Java driver for Cassandra) to a list of table-row-model(POJO) for any table query

This post shows my new found love for CompletableFuture , lamdba expression and Streams API in Java. Problem: You do an async query on a table, get a ResultSetFuture object and need to populate a list of java objects representing a row in Cassandra table. A normal way to do this would be Assume: ResultSetFuture rsf = resultFromSomeTableQuery(); ResultSet rs = rsf.get(); //get ResultSet from rsf get all rows as list by rs.all() now for each Row in above list create a new object(POJO/java object representing table row) and populate the new object by calling Row.get(column index) for each column. This would lead to writing new code which does same task of converting ResultSetFuture to List of POJOs for each table query. Solution: I'll give my solution approach with an example...

Converting google/guava ListenableFuture to Java 8 CompletableFuture

Java was a bit late in bringing Javascript's promise-like functionality. Future was there but wasn't as cool. So people at google made ListenableFuture . Now that java has CompletableFuture , it is common to run into problems like converting a ListenableFuture to a CompletableFuture. This can be because part of your older code uses ListenableFuture and now you have to compose your new logics utilising the newer and Java native technique. Following is one possible way to do it. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class FutureConverter { public static CompletableFuture toCompletableFuture ( ListenableFuture listenableFuture ) { final CompletableFuture completableFuture = new CompletableFuture (); Futures . addCallback ( listenableFuture , new FutureCallback () { public void onFailure ( Throwable throwable ) { completableFuture . completeException...