Ajay George Code Blog

Stuff that I hack on

First among Equals

Much has been told about the Equals method in java. It’s importance along with hashCode is well documented.

The contract of equals method states that it should be

Reflexive, Symmetric, Transitive and Consistent.

Symmetry sounds easy, but could be extremely hard to get right.

The CaseInsensitive String example in Effective Java Item 8 is an excellent discussion on the same. Read it if you haven’t read it already.

I was quite surprised to find the equals implementation broken in the MongoDB Java Driver.

The equals Implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public boolean equals( Object o ){

    if ( this == o )
        return true;

    ObjectId other = massageToObjectId( o );
    if ( other == null )
        return false;

    return
        _time == other._time &&
        _machine == other._machine &&
        _inc == other._inc;
}

The culprit is the massageToObjectId method used in the equals Implementation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/** Turn an object into an <code>ObjectId</code>, if possible.
 * Strings will be converted into <code>ObjectId</code>s, if possible, and <code>ObjectId</code>s will
 * be cast and returned.  Passing in <code>null</code> returns <code>null</code>.
 * @param o the object to convert
 * @return an <code>ObjectId</code> if it can be massaged, null otherwise
 */
public static ObjectId massageToObjectId( Object o ){
    if ( o == null )
        return null;

    if ( o instanceof ObjectId )
        return (ObjectId)o;

    if ( o instanceof String ){
        String s = o.toString();
        if ( isValid( s ) )
            return new ObjectId( s );
    }

    return null;
}

Switching off Camel Case in Octopress

Ever wondered how to switch off Camel Case in Octopress?. By default Octopress has Camel case switched on for both pages and titles, which could be a bit annoying at times.
imathis just committed an update using which you can switch off Camel Case. View the commit details here .

Breaking it down, here is what you have to do :

  • Update to latest version of octopress.
  • In your _config.yml change titlecase from true to false. That’s it. !

Why are com.sun.management.ThreadMXBean methods disabled in JConsole

I had always wondered why some methods on the JConsole are disabled. You can see it in action if you check out the ThreadMXBean which is a part of the JDK

I asked this question on SO and indeed got a wonderful reply. Here is the reason :

1
Only primitives, primitive wrappers and  additional Classes such as BigDecimal.class, BigInteger.class, Number.class, String.class, ObjectName.class are allowed as arguments to valid methods.

For the simple reason that JConsole does not know how to read up other objects as arguments to the exposed methods, they are disabled. Read the complete discussion on SO here .

Error in JLS

There was an interesting question on SO regarding the JLS.

Is a member interface in a class declaration implicitly public?

I got to answer the question correctly. Turns out the JLS was indeed wrong !.