Tag Archives: Java

How to use JUnit4 Hamcrest Matchers

JUnit borrows matchers from a library called Hamcrest, which look a lot like RSpec matchers in Ruby. They can be tricky to write, but end up being much more readable for complex assertions. At its simplest case, we can turn this assertion assertTrue(board.isGameOver()); into assertThat(board.isGameOver(), is(true)); It’s slightly wordier, but I think it reads in […]

Getting the last element in an array in JSF and JSTL

I’d love it if JSTL had some sort of way to get the last element in an array. Ruby conveniently makes negative indexes start from the end of the array: myarray[-1] For some reason, lists and arrays in Java have “.length” and “.size()” accessors, instead of either getLength() or getSize(), and there isn’t a convenience […]

Turning an Image File into a binary Bitmask in Java

Some people who have stumbled across my LED Hoop have asked how I imported the Carrot logo into a bitmask in source code. I thought I had deleted it, but I recently found it… import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageRead { public static boolean hilo(int pixel) { int r, g, […]

Java for-each iterating with a Scanner

Usually you want to do something like this, with String.split(…) for(String unit : units.split(“,”)) { System.out.println(“unit: “+unit); } But if for some reason you need the functionality that Scanner affords, and although it’s probably not very readable, you can use an anonymous class to do it like this for (String unit : new Iterable() { […]

Multiple Instances of the Same Embedded JPA Entity

EclipseLink JPA has this neat feature of @Embedded objects, which allow you to pull out attributes of an entity into another entity, and embed that second entity back into the first object. The result is a Java object that might look a little more logical, but doesn’t actually change the database structure since the embedded […]

Chained Struts Actions Accessible Only by Forward

So I just stumbled into doing this little trick. It may go against some pattern, or best practice, but I’m sure there’s a use for it somewhere. In Struts 1.3 (and probably any Struts 1.x), it is sometimes useful to have a request chain across multiple Action classes, maybe to break them up into more […]

New in Java 7: switch()ing on Strings

Finally, Java can switch on Strings! No more inefficient strings of if/else chains or Hash lookups or translation into Enums. Finally just clean readable and intuitive code. switch(suit) { case “club”: case “diamond”: trickValue = 20; break; case “heart”: case “spade”: trickValue = 30; break; case “no-trump”: trickValue = 40; break; } It’s actually just […]

Adding Tiles to a Struts 1.3 Project with a Custom Request Processor

In Struts, if you wanted to do funny shenanigans (e.g. override the Roles checking for authorization, or override the ActionForm population to parse a JSON object rather than standard parameters) with the Struts Controller, up until Struts 1.2, you had to extend the Request Processor and configure the Struts ActionServlet to use it instead. This […]

Wicket Homepage Redirecting

With the Wicket Java Web framework where a lot of magic happens behind the scenes, and almost no XML needs to be configured. Most of it is done with Java. It has a lot of pretty cool features. I just came across this one. In the init setup of your class that extends WebApplication, you […]

Static Java List Instantiation

But it recently occurred to me that with variable-length methods, I can simply call asList like this:

List<String> strings = Arrays.asList(foo","bar");