Computers – Kai Witte's Blog http://witte-consulting.com/blog Brief articles by Kai Witte Thu, 01 May 2014 16:03:04 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.7 List mapping in Java 8 http://witte-consulting.com/blog/list-mapping-in-java-8/ http://witte-consulting.com/blog/list-mapping-in-java-8/#respond Wed, 23 Apr 2014 19:36:31 +0000 http://witte-consulting.com/blog/?p=357 For Java 5 – 7, see List type mapping in Java.

A common problem is to convert a List of type X to a List of type Y, given a well-defined function from X to Y. For example, we have a List<Double> and want a List<Long> containing those numbers rounded. In Java 8, there is a quick solution for this:

final List<Double> l = Arrays.asList(1.1, 2.9, 3.3, 4.6, 5.5);
final List<Long> solution1 = l.stream().map(Math::round).collect(Collectors.toList());
System.out.println(solution1); // [1, 3, 3, 5, 6]

Short enough, but not lazy: We may not always need to access every member of the List, and thus we don’t need to allocate memory for the whole result and calculate it all at once. In those situations, I use this utility method:

/**
 * Maps a List of one type to a List of another type.
 *
 * @param f    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 List<E> list, final Function<E, F> f) {
  Objects.requireNonNull(list);
  Objects.requireNonNull(f);
 
  return new AbstractList<F>() {
    @Override
    public F get(final int index) {
      return f.apply(list.get(index));
    }
 
    @Override
    public int size() {
      return list.size();
    }
  };
}

Usage example:

final List<Double> l = Arrays.asList(1.1, 2.9, 3.3, 4.6, 5.5);
final List<Long> solution2 = map(l, Math::round);
System.out.println(solution2); // [1, 3, 3, 5, 6]

As you can see, line 2 is much simpler now (once map is available as a global utility method). It allocates very little memory, which does not depend on the size of the source List, and doesn’t run Math::round yet. However, when the same number is accessed more than once, it has to calculate it again. So it’s not always the best solution.

]]>
http://witte-consulting.com/blog/list-mapping-in-java-8/feed/ 0
List type mapping in Java http://witte-consulting.com/blog/list-type-mapping-in-java/ http://witte-consulting.com/blog/list-type-mapping-in-java/#respond Sun, 28 Nov 2010 01:23:03 +0000 http://witte-consulting.com/blog/?p=292 Update for Java 8: List mapping in Java 8

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
]]>
http://witte-consulting.com/blog/list-type-mapping-in-java/feed/ 0
The history of JTiger http://witte-consulting.com/blog/the-history-of-jtiger/ http://witte-consulting.com/blog/the-history-of-jtiger/#respond Sat, 22 May 2010 11:20:00 +0000 http://witte-consulting.com/blog/?p=222 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

]]>
http://witte-consulting.com/blog/the-history-of-jtiger/feed/ 0
Slow Research – A Mod for Warzone 2100 http://witte-consulting.com/blog/slow-research-a-mod-for-warzone-2100/ http://witte-consulting.com/blog/slow-research-a-mod-for-warzone-2100/#comments Tue, 23 Feb 2010 21:32:42 +0000 http://witte-consulting.com/blog/?p=212 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.

]]>
http://witte-consulting.com/blog/slow-research-a-mod-for-warzone-2100/feed/ 1
Java EE-Vortrag in Kiel http://witte-consulting.com/blog/java-ee-vortrag-in-kiel/ http://witte-consulting.com/blog/java-ee-vortrag-in-kiel/#respond Sat, 05 Sep 2009 23:45:15 +0000 http://witte-consulting.com/blog/?p=206 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

]]>
http://witte-consulting.com/blog/java-ee-vortrag-in-kiel/feed/ 0
Found undocumented trojan http://witte-consulting.com/blog/found-undocumented-trojan/ http://witte-consulting.com/blog/found-undocumented-trojan/#respond Fri, 28 Nov 2008 13:10:31 +0000 http://witte-consulting.com/blog/?p=162 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.

]]>
http://witte-consulting.com/blog/found-undocumented-trojan/feed/ 0
How to fix multiplayer_msg_general_failure http://witte-consulting.com/blog/how-to-fix-multiplayer_msg_general_failure/ http://witte-consulting.com/blog/how-to-fix-multiplayer_msg_general_failure/#respond Thu, 06 Nov 2008 20:06:53 +0000 http://witte-consulting.com/blog/?p=159 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.

]]>
http://witte-consulting.com/blog/how-to-fix-multiplayer_msg_general_failure/feed/ 0
JNI (Java Native Interface): Exception in thread “main” java.lang.UnsatisfiedLinkError: Can’t find dependent libraries http://witte-consulting.com/blog/jni-java-native-interface-exception-in-thread-main-javalangunsatisfiedlinkerror-can-t-find-dependent-libraries/ http://witte-consulting.com/blog/jni-java-native-interface-exception-in-thread-main-javalangunsatisfiedlinkerror-can-t-find-dependent-libraries/#comments Mon, 22 Sep 2008 06:48:51 +0000 http://witte-consulting.com/blog/?p=141 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!

]]>
http://witte-consulting.com/blog/jni-java-native-interface-exception-in-thread-main-javalangunsatisfiedlinkerror-can-t-find-dependent-libraries/feed/ 33
uCertify review http://witte-consulting.com/blog/ucertify-review/ http://witte-consulting.com/blog/ucertify-review/#comments Fri, 25 Jul 2008 09:08:20 +0000 http://witte-consulting.com/blog/?p=36 Recently the PR department of uCertify approached me and asked me to blog a review about their exam preparation kit. Since this is an interesting topic to me (see my posts Whizlabs vs. Enthuware, and Effective Enthuware) and since I would get a free simulator, I agreed.

uCertify produces preparation kits for many different IT certifications, including SCJA, SCJP, SCJD, SCWCD, SCBCD, SCMAD and SCDJWS. I got the one for SCBCD 5.0 (Sun Certified Business Component Developer for the Java Platform, Enterprise Edition 5, CX-310-091), which costs USD 59.99, to test it. I used the uCertify PrepEngine Version No: 12.21.05.

Strengths

  • A nice looking user interface with good usability, especially regarding all the little things. For example it is possible to click on the answer itself, not only on the checkbox next to it. Several keyboard shortcuts are available.
  • It has an optional study mode for every kind of test. When enabled, it says immediately which answers were wrong, rather than evaluate all answers only after they have been given. This provides a more harmonic way of learning and measuring progress.
  • It automatically shuffles the order of the answers. That’s an awesome feature that I miss in Enthuware and Whizlabs! It prevents me from remembering the position of the right answer instead of really learning the right answer.
  • The explanations to answers are good and include diagrams where it makes sense.
  • Huge amounts of study material are included! It is not only a simulator, it is a full preparation kit, just as promised.
  • Aside from the option to study the topics using small articles in the style “What is the PreDestroy method?” and larger articles about complex topics, there is an interactive mentor that almost provides the feeling of having a real private teacher. In small chunks it presents the exam topic as specified by Sun, then explains it, and afterwards asks me a question about it to check whether I understood it.
  • There is a feature to discuss any single question with others, similar to a forum thread. Once there are more users this can be very useful. I tried it a few times, and it took more than a day to get my comments “approved”. Hope that gets better.
  • Questions can be assigned a custom tag. For example I can give several questions the tag “unsure” or “do again before exam” and then create a custom test based on the tag. I would highly recommend to use this feature to assign box IDs according to the Leitner System, similar to how I described it for Enthuware in Effective Enthuware. (Note that numbers and short names are problematic, so use for example “bbbb” for box 2.)

    uCertify custom test screen

  • There are lots of features which I didn’t cover in this review, because there are so many. I covered the ones I considered important to my way of learning, but one of the other ones might just be the feature you always wanted.
  • Question quality and exam topic coverage: I trained for a section using Enthuware until I was able to answer all questions in Enthuware corectly. After that I tried questions for the same topic in the uCertify simulator. I passed on the first try, but there were some exam-relevant topics that had not been covered enough in Enthuware! (Concrete example: It is possible to get through the Enthuware questions without fully understanding MEMBER [OF] in JPQL. uCertify did not let me get away with that.) Nothing against Enthuware – it is possible that this test would have worked the other way round in the same way. Simulators have different strengths and weaknesses.

Weaknesses

  • It doesn’t have a direct way of selecting a range of questions. For example I can’t easily create a test with questions 1 – 6 of topic X and later questions 7 – 12 of topic X. (To do that, I have to add all questions to the pool, sort by ID, then delete some of them, remember which ones I deleted, do the others later.)
  • Support. I used the general support, not the one for paying customers, because I am not really a paying customer. My questions were answered only partially sometimes, and one of three support requests was ignored completely. Let’s just hope they provide better support for normal customers.
  • The license is complicated. I asked the support about it, but still don’t understand if I can install the product years later on different hardware. An on-line activation is required. I wouldn’t recommend buying it if you are planning to use it for longer than a year, unless you sort this out.
  • Getting the custom tags, an important feature (see above), to work was a challenge. First I tried tags like “1”, or “2”, and they just didn’t show up in the custom test dialogue. Then I tried “a” and “b”, still no success. So I tried “11111” (maybe it’s a length issue?). Then it showed up, but when I tried to add the questions with that tag, only two or three, it added more than 100 to the question pool. In brief, my impression is that there are certain rules for tags: 1. At least 3 or 4 chars; 2. no numbers (or not only numbers?).
  • Portability – requires Windows. Maybe one of the thin emulators like Wine can handle it? VMware certainly can.
  • The integrated help is a single, huge page.

Conclusion

uCertify provides more than Enthuware or Whizlabs. I cannot answer the very important question of which simulator provides the most relevant questions regarding the real exam, and if someone who uses the complete preparation kit of uCertify will get a better result in the real exam than a low budget learner with Enthuware plus free study material. More fun with uCertify – well, that’s indeed possible due to the nice user interface and the interactive mentor. The complicated license and poor support are a problem.

]]>
http://witte-consulting.com/blog/ucertify-review/feed/ 31
Glassfish: WebService in Session EJB http://witte-consulting.com/blog/glassfish-webservice-in-session-ejb/ http://witte-consulting.com/blog/glassfish-webservice-in-session-ejb/#respond Sun, 13 Jul 2008 15:08:44 +0000 http://witte-consulting.com/blog/?p=28 I just got the strangest error message when trying to use a stateless session EJB as a WebService in Glassfish v2 u1:

[#|2008-07-13T16:41:25.366+0200|SEVERE|sun-appserver-pe9.0|javax.enterprise.system.tools.deployment|_ThreadID=12;_ThreadName=Thread-31;_RequestID=d816d64c-543d-47ac-b339-2231f346754e;|Exception occured in J2EEC Phase
com.sun.enterprise.deployment.backend.IASDeploymentException
	at com.sun.enterprise.webservice.WsUtil.runWsGen(WsUtil.java:1817)
	at com.sun.enterprise.webservice.WsUtil.genWSInfo(WsUtil.java:2089)
	at com.sun.enterprise.deployment.backend.AppDeployerBase.loadDescriptors(AppDeployerBase.java:328)
	at com.sun.enterprise.deployment.backend.AppDeployer.deploy(AppDeployer.java:200)
	at com.sun.enterprise.deployment.backend.AppDeployer.doRequestFinish(AppDeployer.java:129)
	at com.sun.enterprise.deployment.phasing.J2EECPhase.runPhase(J2EECPhase.java:169)
	at com.sun.enterprise.deployment.phasing.DeploymentPhase.executePhase(DeploymentPhase.java:95)
	at com.sun.enterprise.deployment.phasing.PEDeploymentService.executePhases(PEDeploymentService.java:871)
	at com.sun.enterprise.deployment.phasing.PEDeploymentService.deploy(PEDeploymentService.java:266)
	at com.sun.enterprise.deployment.phasing.PEDeploymentService.deploy(PEDeploymentService.java:739)
	at com.sun.enterprise.management.deploy.DeployThread.deploy(DeployThread.java:174)
	at com.sun.enterprise.management.deploy.DeployThread.run(DeployThread.java:210)
|#]

Apparently there is a problem in this Glassfish version that prevents it from showing the proper Exception as JAX-WS reports it. The verifier only barfed random shit at me, too.

I updated to the lastest Glassfish to get a better error message, but then it even worked without an error. Strange days.

]]>
http://witte-consulting.com/blog/glassfish-webservice-in-session-ejb/feed/ 0