List type mapping in Java

November 28th, 2010

Recently someone asked me how to convert a List of type X to a List of type Y, given a well-defined function from X to Y. I’ve seen some horrible “solutions” for that in real world programs, and I hope that some people will benefit from my way of doing this:

/**
 * Defines a Function from E to F.
 * @param <E> the domain
 * @param <F> the codomain
 */
public interface Converter<E,F> {
    F convert(E f);
}
/**
 * Maps a List of one type to a List of another type.
 * @param c the function used to convert from type E to type F
 * @param list the List to be mapped
 * @param <E> the domain
 * @param <F> the codomain
 * @return a new List with the elements mapped
 * @throws NullPointerException iff any argument is <code>null</code>
 */
public static <E,F> List<F> map(final Converter<E,F> c, final List<E> list) {
    if (c == null || list == null) {
        // fail early; particularly important when using lazy evaluation
        throw new NullPointerException();
    }
    return new AbstractList<F>() {
        @Override
        public F get(final int index) {
            return c.convert(list.get(index));
        }
 
        @Override
        public int size() {
            return list.size();
        }
    };
}

This is some pretty generic code that can be used in many situations. For example in web applications it’s often necessary to map a certain entity to a String representation to display on the page.

Here is a usage example:

final List<JFrame> frames = Arrays.asList(new JFrame("hello"), new JFrame("world"));
 
final List<String> titles = map(
        new Converter<JFrame, String>() {
            @Override
            public String convert(final JFrame f) {
                return f.getTitle();
            }
        },
        frames
);
 
System.out.println(titles); // prints: [hello, world]
 
final List<Integer> nums = Arrays.asList(1,2,3);
final List<Integer> inc = map(
        new Converter<Integer, Integer>() {
            @Override
            public Integer convert(final Integer f) {
                return f + 1;
            }
        },
        nums
);
 
System.out.println(inc); // prints: [2, 3, 4]

Not as sweet as it would look like in a functional programming language like Haskell or Scala, but as good as it gets in Java.

See also:

  • Functional Java provides a more pure approach, using its own List implementation rather than java.util.List
  • Jushua Bloch. Effective Java, 2nd Edition. Chapter 4, Item 18 provides a similar application of AbstractList

The history of JTiger

May 22nd, 2010

JTiger is a test framework, developed by Tony Morris in 2004 and published March 23, 2005.

Defining attributes

JTiger had a rich set of features and predefined assertions:

an integrated test assertion for serializability

an integrated test assertion for serializability


check for expected Exception

check for expected Exception


check for equals/hashCode contract

check for equals/hashCode contract


It was easy to run tests programmatically or from an Apache Ant task.

History

Tony Morris developed Assertion Extensions for JUnit (at that time known as JUnitX; not to be confused with other frameworks sharing the same name) a while ago. It contained advanced assertions to be used with plain old JUnit. When Java SE 5 was in beta, he started with the development of JTiger. Because of his contract as an employee of IBM, he needed permission by IBM to publish it as Open Source, even though he developed it in his free time. That process took several months. JTiger even crossed the desk of Erich Gamma, who commented on it in a private email to Tony Morris.

While JTiger was in this approval process, TestNG has been released. At that time it had less fancy assertions (see examples above), but the concept was just as good and additional assertions have been introduced later. Also there were IDE-Plugins for Intellij Idea and Eclipse.

After JTiger has been approved by IBM and released, it gained a fair amount of community acceptance. Independent comparisons with JUnit and TestNG were rather positive, such as Justin Lee’s Test Framework Comparison.

In December 2005 I officially took charge of the project and Tony Morris retired from it. I never changed much except for the documentation and website, though.

Early in 2010 the transfer of the domain ownership for jtiger.org to me failed. I don’t know what went wrong. I got the correct release code from Tony Morris and gave it to my registrar. A few days later my registrar informed me that either the current registrar or the owner (Tony Morris) did not approve the transfer. That was the end of the website.

What now?

In 2008, Tony Morris released Reductio. Similar to QuickCheck, it uses Automated Specification-based Testing. Reductio has become part of the Functional Java API.

And of course you can still use JTiger:


JTiger tar.gz

jtiger-2.1.0376.tar.gz

JTiger MD5 tar.gz

jtiger-2.1.0376.tar.gz.MD5


JTiger zip

jtiger-2.1.0376.zip


JTiger MD5 zip

jtiger-2.1.0376.zip.MD5

Slow Research – A Mod for Warzone 2100

February 23rd, 2010

This mod doubles the time for all research in Warzone 2100.

Download Slow Research Mod Version 1.0 alpha 3

System requirements

Warzone 2100 Version 2.3 beta 10 (MIGHT work with other versions).

Installation

See the reference on the official project website.

Tactical motivation

Experienced players have optimised their research in such a way that many technologies are outdated a few minutes after they have been researched. The lifespan of some weapons is very short in games with experienced players. For example ripple rockets are available in less than 19 minutes (T1, no starting bases), which makes mortars pretty much useless. When someone tries to use mortar or bombard pits, the opponent can simply build a few bunkers and wait for CB tower and ripple rockets. Even a huge field of mortar pits can only destroy a few bunkers before it gets smashed by ripple rockets.
With this mod, ripple rockets will be available around game minute 37, which makes mortars more attractive.

It does not slow down the entire game! Production speed and unit movement speed as well as power income are unchanged. So relative to the research, these elements are twice as fast now. For example in the previously short time span from when twin mg gets researched until the research of heavy MG (or a different superior counter weapon like mini pod), a player can build twice as many tanks with twin MG and move twice as far with them as before.

Java EE-Vortrag in Kiel

September 6th, 2009

Hallo Teilnehmer,

für die Codeschnipsel, die ich in der Präsentation gezeigt habe, habe ich ein kleines Java EE-Projekt angelegt, das Ihr hier herunterladen könnt:

javaee-vortrag.zip

Erwartet bitte nicht zu viel. Es macht nichts Sinnvolles, sondern enthält nur die gezeigten Programmteile. Allerdings funktioniert es, und Ihr könnt darauf zum Testen aufbauen. Ich helfe gern bei der Installation, falls die Anweisungen unter “Dokumentation” im zip nicht reichen sollten. Projektdateien für Intellij Idea und Eclipse (ungetestet) sind dabei.

Als komplettes Beispiel für eine Java EE 5 Applikation bietet sich ansonsten die Pet Store Demo von Sun an.

Wer den Stand der Technik mit Schwerpunkt Webfrontend durch Java EE-basierte Frameworks sehen will, für den bietet sich das Booking-Example von Seam an oder auch die Demos von JBoss RichFaces.

Fragen und Diskussion zum Vortrag oder zu Java EE gern hier in den Kommentaren, oder kontaktiert mich direkt.

Gruß

Kai

Too depraved to book a hotel

May 30th, 2009

For my business trip to Hamburg next week I booked an apartment. But today they cancelled. First reason is that they find a software developer who does not have a mobile phone suspicious. Second is the text on my private website which says:

Death Knight Kai
At first glance, this clean-cut, well mannered man appears to be an average software developer, but a dark heart dwells within that handsome exterior. Kai is one of the most depraved men alive. Nothing is sacred to him, and with each passing day he sinks further and further into his darkest thoughts.

It’s not astonishing at all that they do not understand it. It’s an insider joke for people who played Heroes of Might and Magic IV back in the days, which features a death knight with the same description ("Jarvis"):

Death Knight Jarvis
At first glance, this clean-cut, well-mannered man appears to be an average knight, but a dark heart dwells within that handsome exterior. Jarvis is one of the most depraved men alive. Nothing is sacred to him, and with each passing day he sinks further and further into his darkest thoughts.

Strange is that someone would think that there really is a dark side to software development, and that those who have fallen to it would not only use PHP and Ruby on Rails, no, they would also devastate hotel rooms.

Found undocumented trojan

November 28th, 2008

I recently came across a trojan, which is detected by common virus scanners, but not much documented. So I analysed it just a little.

Like many others it is located in the Windows directory (for example c:\winnt) and named svchost.exe (the real svchost.exe belongs to system32). It is set up to start with Windows.

These are its names according to jotti.org:

A-Squared  	
Found Win32.SuspectCrc!IK
AntiVir 	
Found TR/Downloader.Gen
ArcaVir 	
Found Trojan.Agent.Wo
Avast 	
Found Win32:Trojan-gen {Other}
AVG Antivirus 	
Found BackDoor.Agent.MEA
BitDefender 	
Found Backdoor.Agent.WO
ClamAV 	
Found Trojan.Agent-8319
CPsecure 	
Found nothing
Dr.Web 	
Found BackDoor.IRC.Spreader
F-Prot Antivirus 	
Found W32/IRCBot-based!Maximus (probable variant)
F-Secure Anti-Virus 	
Found Backdoor.Win32.Agent.wo
G DATA 	
Found Win32:Trojan-gen
Ikarus 	
Found Win32.SuspectCrc
Kaspersky Anti-Virus 	
Found Backdoor.Win32.Agent.wo
NOD32 	
Found probably unknown NewHeur_PE (probable variant)
Norman Virus Control 	
Found W32/Agent.CWBV
Panda Antivirus 	
Found Trj/Downloader.MDW
Sophos Antivirus 	
Found Mal/Generic-A
VirusBuster 	
Found nothing
VBA32 	
Found Backdoor.Win32.Agent.wo

The size is 86.016 bytes, MD5: 5ec89f1f189fc5af94aa9306d7df4b8b

What it does is this: It tries to connect to IRC.BENDOVER.BE (that server is no longer operational), then joins #spreader.crew (password: spreadmaster). In my case it used the name!id: oppqrrstc!oppqrrstc (probably generated individually). Then it stayed in the channel and did nothing more. It probably waited for a bot or real person to contact it with further instructions.

Any hints about this trojan or the creators (former owner of bendover.be? "spreader.crew"?) are appreciated.

How to fix multiplayer_msg_general_failure

November 6th, 2008

In Heroes of Might and Magic IV (aka Heroes of Might and Magic 4, HOMM 4, HOMM IV) this error occurs when the name of the executable used to launch the program differs:

multiplayer_msg_general_failure

The content of the executable does not matter, it is the name. So both must use for example h4tour351.exe, even though in Equilibris 3.51 it has the same content as h4mod.exe.

Chess puzzle: The two pawns (medium)

October 19th, 2008

The moves following here look like a harmless, automated trade of the minor pieces. But actually I (white) missed a chance to win material here. What should I have done in move 14?

The actual game is below. Find the moves for white that win material.


Hint 1: show

Hint 2: show

JNI (Java Native Interface): Exception in thread “main” java.lang.UnsatisfiedLinkError: Can’t find dependent libraries

September 22nd, 2008

How to fix UnsatisfiedLinkError in JNI

I have never had problems with JNI. In the few situations when I needed it, it just worked. But last week someone asked me for help with his problem. When I tried to reproduce it, I found out that it had spread even to my old JNI programs which worked until 2006!

When running a program that uses a native function the following error occured:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Test.dll: Can't find dependent libraries

It’s obviously a linker problem related to the generation of the .dll file. I have no idea why it occurs now and didn’t occur in the past. Different OS, compiler version, something like that? For some reason both the linker of Visual Studio and the linker of GCC started to think that is was a good idea to replace all method names by some arbitrary name which then cannot be found at runtime.

Fix for GCC

To fix that with GCC, the linker opion --add-stdcall-alias can be used.

gcc -Wl,--add-stdcall-alias -mno-cygwin -shared -I/cygdrive/c/Program\ Files/Java/jdk1.6.0_05/include -I/cygdrive/c/Program\ Files/Java/jdk1.6.0_05/include/win32 -o HelloWorld.dll HelloWorld.c

Fix for Visual C

For Microsoft Visual C Compiler it should theoretically be possible with a linker option as well.

Find out the correct linker option

First compile and link in the usual way that didn’t work. This will create the .dll file which results in the error we are talking about. Then use the dumpbin tool to list the procedure names:

dumpbin HelloWorld.dll /EXPORTS

You will find your procedure name, something like _Java_HelloWorld_print@8. Now try to guess what the name should be. My guess in this case would be Java_HelloWorld_print. This would result in the linker option /EXPORT:Java_HelloWorld_print=_Java_HelloWorld_print@8

Supply the linker option
  1. Use a #pragma comment in the source file:
    #pragma comment(linker, "/EXPORT:Java_HelloWorld_print=_Java_HelloWorld_print@8")

    OR

  2. Split into linking and compiling and provide the option to the linker in the command line, OR
  3. Supply it to the compiler at the end using /link:
    cl -I"C:\Program Files\Java\jdk1.6.0_05\include" -I"c:\Program Files\java\jdk1.6.0_05\include\win32" -MD -LD HelloWorld.c -FeHelloWorld.dll /link /EXPORT:Java_HelloWorld_print=_Java_HelloWorld_print@8

Edit: According to user comments, the -MD option above is wrong and causes the problem described below. Do not use -MD! See MSDN for further information about the -MD option.

Option 3 is probably the easiest. After that dumpbin will show that now there is an additional procedure with the correct name:

dumpbin HelloWorld.dll /EXPORTS

I even compared it with the working .dll I created with GCC (see above), and it looks the same! But it doesn’t work, still getting the same error!

Chess puzzle #24 (medium)

August 30th, 2008

White to move and win a pawn.

White to move and win a pawn.