10 Subtle Best Practices when Coding Java
This is a list of 10 best practices that are more subtle than your average Josh Bloch Effective Java rule. While Josh Bloch’s list is very easy to learn and concerns everyday situations, this list here contains less common situations involving API / SPI design that may have a big effect nontheless.
C# StructureMap : Getting Started
In this post we are going to learn about How we can do dependency injection with StructureMap. Here we are going to take a sample application of shopping cart. This shopping cart can process two type of orders 1. Sales order 2. Purchase Order. We want an abstraction for this. So first we are going to create an interface IOrder which will be implemented by both Purchase Order and Sales Order classes.
Dependency Injection with Simple Injector
Before some I have written a blog post about how to do dependency injection with StructureMap. In this post we are going to learn Simple Injector IOC library and how we can do dependency injection with Simple Injector. Simple Injector is a open source dependency injection library developed with C#. Followings are few characteristics of Simple Injector.
How to install or remove fonts in Linux
These days Linux distributions comes with different desktop flavors,and those different desktop environments contain different applications for this. To install a font, first download it in...
mysqld_multi: How to run multiple instances of MySQL
mysqld_multi is designed to manage several mysqld processes that listen for connections on different Unix socket files and TCP/IP ports. It can start or stop servers, or report their current status. The need to have multiple instances of MySQL (the well-known mysqld process) running in the same server concurrently in a transparent way, instead of having them executed in separate containers/virtual machines, is not very common. MySQL provides a tool to facilitate the execution of multiple instances called mysqld_multi...
Java 8 Map Enhancements
In previous posts, we’ve already dealt with a couple of new Streams features, for instance when sorting. Most API improvements are indeed part of the new Streams API. But a few nice methods were also added to java.util.List and most importantly, to java.util.Map. If you want a quick overview of feature additions, go to the JDK8 Javadoc and click on the new “Default Methods” tab:
String Deduplication – A new feature in Java 8 Update 20
Strings consume a lot of memory in any application. Especially the char containing the individual UTF-8 characters is contributing to most of the memory consumption of a JVM. It is not uncommon to find 30% of the memory consumed by Strings, because not only are Strings the best format to interact with humans, but also popular HTTP APIs use lots of Strings. With Java 8 Update 20 we now have access to a new feature called String Deduplication, which requires the G1 Garbage Collector and is turned off by default.
Calculate the Optimum Number of Threads
Working out the theoretical optimal number of threads you should use for your application is fairly straightforward. You do, however, need to understand your applications runtime characteristics. Is it mostly occupied with CPU intensive work or is it mostly waiting for IO? In this post, we look at how to work out a good thread pool size for your CPU and IO based tasks.
Currying Functions in Java & Scala
Currying is the technique of transforming a function with multiple arguments into a function with just one argument. The single argument is the value of the first argument from the original function and the function returns another single argument function. This in turn would take the second original argument and itself return another single argument function. This chaining continues over the number of arguments of the original. The last in the chain will have access to all of the arguments and so can do whatever it needs to do.
Functional Interfaces in Java 8
Java 8 treats lambdas as an instance of an interface type. It formalises this into something it calls “functional interfaces”. A functional interface is just an interface with a single method. Java calls the method a “functional method” but the name “single abstract method” or SAM is often used. All the existing single method interfaces like Runnable and Callable in the JDK are now functional interfaces and lambdas can be used anywhere a single abstract method interface is used.
Classes vs. Functions
You can use lambdas in Java 8 anywhere you would have previously used a single method interface so it may just look like syntactic sugar but it’s not. Let’s have a look at how they differ; it’s anonymous classes vs. lambdas or simply put, classes vs. functions. So, a typical implementation of an anonymous class (a single method interface) in Java pre-8, might look something like this. The anonymousClass method is calling the waitFor method passing in some implementation of Condition, in this case it’s saying wait for some server to have shutdown.
Method References in Java 8
Method References in Java 8, The “closure” method reference, The “lambda” method reference... Java 8 brings with it method references; shortcuts that you can use anywhere you would use a lambda. The Oracle docs describe four types of method reference but do such a poor job of describing them that I felt compelled to describe them myself.
Java File I/O Basics
Java 7 introduced the java.nio.file package to provide comprehensive support for file I/O. Besides a lot of other functionality this package includes the Files class (if you already use this class you can stop reading here). Files contains a lot of static methods that can be used to accomplish common tasks when working with files. Unfortunately it looks to me that still a lot of newer (Java 7+) code is written using old (pre Java 7) ways of working with files. This does not have to be bad, but it can make things more complex than needed.
Building a simple RESTful API with Spark
In this blog post we will see how Spark can be used to build a simple web service. As mentioned in the disclaimer, Spark is a micro web framework for Java inspired by the Ruby framework Sinatra. Spark aims for simplicity and provides only a minimal set of features. However, it provides everything needed to build a web application in a few lines of Java code.
Using Markdown syntax in Javadoc comments
In this post we will see how we can write Javadoc comments using Markdown instead of the typical Javadoc syntax. So what is Markdown? Markdown is a plain text formatting syntax designed so that it optionally can be converted to HTML using a tool by the same name. Markdown is popularly used to format readme files, for writing messages in online discussion forums or in text editors for the quick creation of rich text documents.
An alternative approach of writing JUnit tests (the Jasmine way)
Recently I wrote a lot of Jasmine tests for a small personal project. It took me some time until I finally got the feeling of getting the tests right. After this, I always have a hard time when I am switching back to JUnit tests. For some reason JUnit tests did no longer feel that good and I wondered if it would be possible to write JUnit tests in a way similar to Jasmine tests.
Understanding JUnit's Runner architecture
A JUnit Runner is class that extends JUnit's abstract Runner class. Runners are used for running test classes. The Runner that should be used to run a test can be set using the @RunWith annotation. Some weeks ago I started creating a small JUnit Runner (Oleaster) that allows you to use the Jasmine way of writing unit tests in JUnit. I learned that creating custom JUnit Runners is actually quite simple. In this post I want to show you how JUnit Runners work internally and how you can use custom Runners to modify the test execution process of JUnit.
Java 8: Implement a local cache using the good old ConcurrentHashMap
Now get ready for one of the most awesome revelations in this series so far. We’ll show you an easy-as-pie way to implement a local cache using the good old ConcurrentHashMap and lambda expressions. Because, Map now has a new way of a atomically calculating a new value in case a key is absent. Perfect for caches. Let’s delve into code:
Caching with ConcurrentHashMap and computeIfAbsent
Everybody knows about Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21...). They are a nice example of recurrent calculation of sequence of numbers: f(n) = f(n-2) + f(n-1). Let's write a simple code that calculates the n-th (0-based) Fibonacci number...
Java can finally join strings
I am sure you were in a situation in which you wanted to join multiple strings. If you were using a programming language other than Java you probably used the join() function provided by the programming language. If you were using Java you could not do this. There was no join() method. The Java Standard Class Library provided you tools for building GUI applications, accessing databases, sending stuff over the network, doing XML transformations or calling remote methods. A simple method for joining a collection of strings was not included.