Programming

Programming Books 2008

These are the programming books that I read or am reading for year 2008, which I think is good for reading by developers.
December is a long holiday. Any recommendations for good books for year 2008 which worth reading, irregardless of programming languages?
Effective Java (2nd Edition) by Joshua Bloch

Java Power Tools by John Ferguson Smart

 
Clean […]

Java: ServiceLoader in JDK 6

ServiceLoader is a class available in earlier version of Java and staring JDK 6, it is available formally under java.util package.
In JDK 6 - java.util.ServiceLoader
Prior to JDK 6 - Refer to the document at http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html
A service is a well-known set of interfaces and (usually abstract) classes. A service provider is a specific implementation of a […]

Java: Refactoring If-Then-Else using Enum

Download the code here
One thing good about object oriented language is that now we can reduce the conditional statements typically found in procedural language, which in turn can make our code more maintainable.
In Java, normally if we want to refactor a long conditional statements, we use polymorphism, which results in a lot of classes.
Another way […]

Java: Debug Commons Digester Exception

This is a exception produced by Commons Digester. After spending few hours to find out the root cause, including studying the source code, I found that I make a silly mistake.

1: java.lang.NullPointerException
2: at org.apache.commons.digester.Digester.createSAXException(Digester.java:3181)
3: at org.apache.commons.digester.Digester.createSAXException(Digester.java:3207)
[…]

Java: A Simple JSON Utility

Here is a simple JSON utility class that I used to do the followings

Convert JSON string to a Map and vice versa
Convert JSON string to list of maps and vice versa

This class encapsulate all the JSON string manipulation in a single Java class so that it frees you up from the actual JSON implementation. You […]

Java: Ordered Map and Set

If you need an ordered Map or Set, meaning maintaining the original in your Map or Set in the order you insert or add them, then you should use LinkedHashMap or LinkedHashSet.
Some sample code below.

1: import java.util.*;
2: 
3: public class TestClass {
4: 
[…]

Java: Refactor Code by Introducing NULL Object

This is from the Refactoring book by Martin Fowler.
In our code we always need to check if a object is null, and depending on it, different actions are taken.
E.g. for the following code

1: if (subscriber == null) {
2: ratePlan = defaultRatePlan;
[…]

Java: Logger Configuration

Normally if you want to avoid creating multiple logger in your based and derived classes, you can declare a single logger in the based class.
E.g. in the following base class, a single logger is declared

1: package twit88;
2: 
3: import org.apache.commons.logging.Log;
4: import org.apache.commons.logging.LogFactory;
[…]

Java: Static Initializer Problem

Have a look at the following code

1: public class StaticTest {
2:
3: static {
4: name = “my name”;
5: }
[…]

Apache Tomcat Servlet Error in GenericServlet.getServletName

Here is the error I got for one of the Java servlet

1: 2008-09-30 02:36:50,915 | ERROR | [StandardWrapperValve:260 invoke | http-8881-Processor20] - Servlet.service() for servlet myServlet threw
2: exception
3: java.lang.NullPointerException
4: at javax.servlet.GenericServlet.getServletName(GenericServlet.java:322)
5: […]

Java: Automate Your Load Test using JMeter

In all critical applications when response, throughput and transaction processing time are important factors to determine the success of the system, load test is definitely a must.
In my projects normally I used JMeter to carry out load testing by running JMeter across multiple machines. The problem is that the server can only be load […]

Java: Automate Your Documentation Process

In any project, documentation is definitely necessary. Problem is that software documentation is always out of sync with the software itself as software is ever evolving. Software documentation is always outdated if they are managed in a manual way.
Here are some open source software that can be used to automate software documentation, and thus avoid […]

Java Generics Summary

A short note on Java Generics

Generics are not covariant.

1: List<Integer> li = new ArrayList<Integer>();
2: List<Number> ln = li; // illegal

Construction Delays

1: public void doSomething(T param) {
2: T copy = new T(param); // illegal
3: }

Constructing wildcard references

[…]

Java: Check Class Version

Here is a simple piece of code to check your Java class version, whether is it is compiled with JDK 4, JDK 5 or JDK 6, etc…

1: try {
2: String filename = “C:\\MyClass.class”;
3: […]

Java: Coding Guidelines

Here are some simple guidelines I followed for one of the system that I refactored.
Always Program Using the Interface
E.g.

1: Map<String,String> obj = new HashMap<String,String>
2: 
3: List<String,String obj> = new ArrayList<String,String>

 
Prefer HashMap over Hashtable, ArrayList over Vector, StringBuilder over StringBuffer
If you really need synchronized access, […]

Generate Statistics Report from Source Code Repository

Here are some tools to help generate statistics report from your source code repository.
StatCVS retrieves information from a CVS repository and generates various tables and charts describing the project development, e.g.

Timeline for the lines of code
Lines of code for each developer
Activity by Clock time
Authors Activity
Author activity per Module
Author Most Recent Commits with links to ViewVc
Stats […]

Java: Code Analysis using Eclipse

Code analysis is carried to normally do the followings

Coding standards
Code duplication
Code coverage
Dependency analysis
Complexity monitoring

These analysis areas can be uncovered using a number of the following slick Eclipse plugins:

CheckStyle: For coding standards
PMD’s CPD: Enables discovering code duplication
Coverlipse: Measures code coverage
JDepend: Provides dependency analysis
Eclipse Metrics plugin: Effectively spots complexity

Quoted from
Automation for the people: Improving code with Eclipse […]

Java: StringUtils.isNumeric with Decimal Point

For the code snippet

1: System.out.println(StringUtils.isNumeric(“10.0″));

Using Apache Commons Lang, the above will return false. This is one bug I recently found in one of the existing application.
As quoted from the documentation, StringUtils.isNumeric
Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.
To return the correct […]

Java: Create Immutable Object using Builder Pattern

To create immutable Java object, we should use the Builder Pattern, as described in Effective Java. This is the pattern I normally used now for immutable class.

1: public class Person {
2: private final String name;
3: private final int […]

Java: Trap in Arithmetic II

What will you expect from the following code?

1: public static void main(String[] args) {
2: System.out.println(10.00-9.10) ;
3: 
4: }

Instead of printing “0.90″, it prints “0.9000000000000004″ on my machine.
This is a know floating point issue with all languages. To get it […]