2011-12-30

Java 7 does not work on some older Linux platforms:

A good hint is

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6538311


Just search for the part with system-config-securitylevel.

I started to use CX.COM, which is a new online storage like Dropbox.




You can have a FREE account which gives you 10 GB of FREE storage which is twice as much as on Dropbox. The disadvantage: the background synchronization seems to be slower than Dropbox, but I can life with that. I don't care if it takes 2 or 2.5 days...

I use such free online services (there are many more than mentioned here) for transferring my files between several computers, and for backup of certain, incredibly important files.

I can recommend both :-)

My opinion about 'The Clean Coder'

There are already several reviews of Robert C. Martin's book 'The Clean Coder' out there, but I couldn't resist to write my own comments / thoughts about that book.

It is a very important book for software developers, even if it does not contain any new ideas at all.

The important point is that Mr. Martin emphasizes "professional" software development, and he wants us to behave and sell ourselves as professionals. That's quite OK for me, I like that, but I don't need to read a complete book for figuring this out:-)


Mr. Martin touches several topics about so-called professional software development like testing, estimating, collaborating, and so on.

A typical chapter consists of some stories of Mr. Martin's own experience as a programmer. Those stories usually show what went wrong, so that one can learn from them. After having read so many bad stories I started to wonder, if Mr. Martin ever had a successful software project, but let's assume so...

Then there are some instructions for developers what to do and what not.


If you are a not so much experienced software developer:
  • Read it, learn it, try it, code it, quote it.

If you are an experienced software developer:
  • This book  probably does not help you, it does not contain anything new, just old stuff, extended by unnecessary personal stories by Mr. Martin.

A ridiculously poor (aka uninformative) appendix mentions some 'tools' like continuous integration software.

Some hints for calculations with floating point numbers

This is an old posting from 2009 which I am publishing again.

Here are some (slightly simplifying) hints or rules for the usage of BigDecimal.
  1. Use BigDecimal.valueOf(double) or new BigDecimal(String) to create BigDecimal objects.
    Never use new BigDecimal(double), unless you know what you are doing. Be aware that 0.1 is an infinite decimal number, 0.5 is not, so new BigDecimal(0.5) is fine, but not new BigDecimal(0.1). And if you do not want to think about the difference every time, do not use new BigDecimal(...) at all.
  2. If you want to compare BigDecimal objects, be aware: equals() compares the value and the scale, that means 1.001 is not equal to 1.00100. Use signum() and compareTo().
  3. Use setScale to round the BigDecimal value.
  4. Use a Context for rounding.
  5. Be aware that setScale or a division may throw an ArithmeticException, so always use a RoundingMode or a RoundingContext.



package testdrive;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.math.BigDecimal;

import org.junit.Test;



public class DoubleCalcTest
{

    @Test
    public void addDouble()
    {
        /**
         * This is actually a very old example which shows that calculations with double values can lead to unexpected
         * results.
         */
        double a = 0.1;
        double sum = 0;
        for ( int i = 0; i < 10; i++ )
        {
            sum += a;
        }
        // sum should be 1, but it isn't
        assertTrue( sum != 1 );
        /**
         * There is no exact binary representation for 0.1, so repeated calculations with 0.1 will probably have wrong
         * results.
         */
    }



    @Test
    public void createBigDecimalCorrectly()
    {
        /**
         * This shows the usage of the static valueOf method. In such a way, a BigDecimal object can be created from a
         * double value. Another possibility is the constructor with the String which is shown here just for fun. I would
         * recommend to always use the static valueOf method.
         */
        BigDecimal a = BigDecimal.valueOf( 0.1 );
        BigDecimal sum = new BigDecimal( "0" ); // Just to show this possibility.
        // OR: BigDecimal sum = BigDecimal.ZERO;

        for ( int i = 0; i < 10; i++ )
        {
            sum = sum.add( a );
        }
        BigDecimal expected = BigDecimal.valueOf( 1.0 );

        /**
         * If the two BigDecimal objects may have different scale, then use signum() and compareTo().
         */
        assertTrue( expected.signum() == sum.signum() );
        assertTrue( expected.compareTo( sum ) == 0 ); // == 0 means, they are equal.
    }



    @Test
    public void createBigDecimalInAWrongWay()
    {
        /**
         * Never use the constructor with double values, unless the binary representation of the double values is what you
         * want. So actually it is better to use the constructor with a String or the static value of method.

         * This example shows the problem with the constructor which has a double as parameter.
         */
        BigDecimal a = new BigDecimal( 0.1 );
        BigDecimal sum = new BigDecimal( 0.0 );
        for ( int i = 0; i < 10; i++ )
        {
            sum = sum.add( a );
        }
        BigDecimal expected = new BigDecimal( 1.0 );

        /**
         * The value of the expected variable (i.e. 1.0) is not the same as the value of the sum variable. The reason is
         * that there is no exact representation of 0.1 in the binary system, so the BigDecimal does not represent 0.1 but
         * something similar.
         */
        assertFalse( expected.equals( sum ) );

        /**
         * If the two BigDecimal objects may have different scale, then use signum() and compareTo().
         */
        assertTrue( expected.signum() == sum.signum() );
        assertTrue( expected.compareTo( sum ) != 0 ); // != 0 means, they are different.

        /**
         * A hint to JUnit:

         * 

         * If you use JUnit, then be aware that assertEquals does use the Number.longValue() method to compare BigDecimal
         * objects, so the result is often wrong. So please use the assertTrue or assertFalse methods.
         */
        assertEquals( expected, sum );
    }

}


2011-06-29

2011-05-13

2011-04-13

JSR-346 for new CDI 1.1

There is a new Java Specification Request (JSR) with number 346: Contexts and Dependency Injection for JavaTM EE 1.1.
It contains improvements for CDI  1.0, and is lead by Red Hat.

CDI might be the most helpful new piece of technology in Java EE 6. If you haven't heard of it or didn't use it before, then try Adam Bien's articles or the documentation of CDI's reference implementation, called Weld, which is actuaaly quite easy to read.

So what are the suggestions for CDI 1.1? Let's look what the JSR says:
  • "Global ordering of interceptors and decorators, as well as global enablement of alternatives [CDI-48]
  • An API for managing built in contexts, allowing the built in implementation of the conversation context to be used outside of JSF [CDI-30]
  • An embedded mode allowing startup outside of a Java EE container [CDI-26]
  • Declarative control over which packages/classes are scanned in a bean archive [CDI-87]
  • Bean declaration at constructor level [CDI-55]
  • Static injection [CDI-51]
  • Inclusion of @Unwraps from Seam Solder [CDI-89]
  • Align with current version of @Inject [CDI-51]
  • Numerous minor enhancements to the Portable Extensions SPI
  • Client controlled contexts allowing for SaaS style multi-tennancy [CDI-103]
  • Better support for CDI in libraries when used in the Java EE platform [CDI-84]
  • Send CDI events for Servlet events [CDI-38]
  • Application lifecycle events [CDI-86]"

The final release of CDI 1.1 is currently scheduled for Q3 2012.


.

2011-04-07

NMO

Today 6 months ago - on 2010-10-07 - I got a bad disease called neuromyelitis optica.

It nearly killed me within the first hours, but some good people in the hospital saved my life. I was lucky, because I was in a hospital at that time (for some other reasons).

I spent a month in the intensive care unit of the hospial, sleeping a lot, dreaming bad dreams, and fighting for my life.

Then I spent two long months in the monitoring station, struggeling with my blood circuit and other things. After another month in a regular station, I finally moved to the rehabilation center called Weisser Hof.

I cannot feel and move my legs anymore, so I am learning to use a wheel chair.

My body becomes stronger now, and I feel more energy, so I try to come back to a regular life.

.