We need you!

Icon

The IzPack documentation needs work, and you are invited to edit it!

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

I am learning Groovy after more than 10 years of Java programming and design. To help in my first steps in the world of a new language I like books and the one I chose introduces Groovy in a nice and easy way for the Java developper I am : just take a Java class and replace the ".java" extension of the file by ".groovy" and you are done...

After playing a little with Groovy, I got a behavior that puzzled me : I wanted to write an integer class that was supporting the nice operator overloading feature of Groovy. My strategy was to first write the class in Java and slowly turning it to 100% Groovy while keeping an eye both on the behavior and performances :

Code Block
languagejava
public final class LongValue
        implements RingStructureElement<LongValue>, Comparable<Object> {

    long value;
    public LongValue(long value) {
        this.value = value;
    }
    @Override
    public LongValue plus(LongValue rightTerm) {
        return new LongValue(value + rightTerm.value);
    }
    @Override
    public LongValue negative() {
        return new LongValue(-value);
    }
    @Override
    public LongValue minus(LongValue rightTerm) {
        return new LongValue(value - rightTerm.value);
    }
    @Override
    public LongValue multiply(LongValue rightFactor) {
        return new LongValue(value * rightFactor.value);
    }
    public long toLong() {
        return value;
    }
    @Override
    public String toString() {
        return "LongValue("+value+")";
    }
    @Override
    public boolean equals(Object obj) {
        return compareTo(obj) == 0;
    }
    @Override
    public int compareTo(Object o) {
        if( o instanceof LongValue) {
            return compareToLongValue( LongValue.class.cast(o) );
        } if (o instanceof Long) {
            return compareToLong(Long.class.cast(o));
        }
        throw new IllegalArgumentException("Do not know how to compare "+this+" with "+o);
    }
    private int compareToLongValue(LongValue o) {
        if( value < o.value) {
            return -1;
        } else if( value > o.value) {
            return +1;
        } else {
            return 0;
        }
    }
    private int compareToLong(long o) {
        if( value < o) {
            return -1;
        } else if( value > o) {
            return +1;
        } else {
            return 0;
        }
    }
}

This java class is at a very early development status as I am still writing the requirement unit tests. I tried plain Groovy to write these tests, as this approach is usually recommanded to have a first real touch with it...

Code Block
languagejava
class LongValueTest extends GroovyTestCase {
    void testPlusLong() {
        def entier1 = new LongValue(45)
        def entier2 = new LongValue(5)
        assert entier1 + entier2 == 50L
    }
}

And this test failed...

Code Block

Assertion failed: 

assert entier1 + entier2 == 50L
       |       | |       |
       |       | |       false
       |       | LongValue(5)
       |       LongValue(50)
       LongValue(45)

    at org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:398)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailed(ScriptBytecodeAdapter.java:646)
    at berty.maths.integer.LongValueTest.testPlusLong(LongValueTest.groovy:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

WTF !?!

I set break points in my equals method... not hit !..

WTF !?!

I double checked the Groovy document page I had justread : Operator Overloading

WTF !?!

Then, I found this page on which I started to discuss with the Groovy team (thanks again to all of them for their answers) :

JIRA
serverjira.codehaus.org
keyGROOVY-3364

I am still not exactly sure at which level the issue lies and this why I started this page because the defect page is not the correct place to discuss such things.