Archive for November, 2006

Indicating the absence of an object

Sometimes I’ve seen some Java code intended to indicate the absence of parameters, that is, code validating null parameters (every public method must have a test to check against null parameter), verifying if a certain range is respected (like date, number ranges) and other kinds of contextual validation before calling the objects’ public methods. Some validation code (written in Java) is shown below:

At class Player:

public void pickUp (Item item)
   assert(item != null): "item cannot be null";
   // some code here

The above code uses Assertions, introduced in JDK1.4, to validate the item parameter. If item is null, an AssertionError will be thrown with the message “item cannot be null”.

public void drop (Item item)
   if(item == null) {
      throw new IllegalArgumentException(“item cannot be null”);
   // some code here

This above one uses an IllegalArgumentException, which is a RuntimeException, to report a null item.

public boolean addToInventory (Item item)
   if(item == null) {
      return false;
   // some code here

And this above Java code snippet returns false if an item is null.

or, maybe, applying the NullObject design pattern:

class NullItem implements Item

and at Player it becomes:

public void drop (Item item)
   // some code here

The last avoids a lot of tests to see if the parameter is null, replacing the tests with an object that provides the appropriate null behavior (“do nothing” behavior).
The question is: which do you think is the best way to indicate the absence of such an object? Which do you use often? Tell me if you use another approach to report this.


Open sourcing Java with GPL license?

CRN is announcing that Sun is set to put JavaMe and J2SE under the GPL license. Last months Sun commented about putting its Java plataform under a public license, but they were deciding which license to put Java into. Now the preferred choice is GPL. I think it’s to promote and increase Sun’s reputation in the open-source community. However, as the new said, a GPL license would require those developers making changes to the core Java platform to freely release their code. What do you think about this? Has Sun made the right choice? And what about Apache’s license?


List or List ?

Java 1.5 introduced some new concepts at the Java language level. One of them is Generics, which you can make use mainly when creating type safe collections. This new concept facilitates the programmers’ life: being type safe, errors when working with collections can be caught at compile time (if you have a List<String>in hand, only Strings can be added to this list, neither Integers, Cats or Dogs ) and the programmer doesn’t have to cast when taking out an object of a collection. I was thus wondering whether there is any difference between List<?> and List<Object>. Let’s take a look at the following lines of Java code:

   1:import java.util.ArrayList;
   2:import java.util.List;
   4:public class Music
   6:    abstract class Instrument
   7:    {
   8:    }
  10:    class Guitar
  11:            extends Instrument
  12:    {
  13:    }
  15:    class Flute
  16:            extends Instrument
  17:    {
  18:    }
  20:    class Drums
  21:            extends Instrument
  22:    {
  23:    }
  25:    public static void main(String [ ] args)
  26:    {
  27:        List<Instrument> instruments =
  28:            new ArrayList<Instrument>();
  29:        List<Guitar> guitars = new ArrayList<Guitar>();
  30:        List<Flute> flutes = new ArrayList<Flute>();
  31:        List<Drums> drums = new ArrayList<Drums>();
  32:        List<Integer> integers = new ArrayList<Integer>();
  33:        List<Object> objects = new ArrayList<Object>();
  34:        check(instruments);
  35:        check(guitars);
  36:        check(flutes);
  37:        check(drums);
  38:        check(integers);
  39:        validate(objects);
  40:        validate(drums);
  41:    }
  43:    private static void check(List<?> instruments)
  44:    {
  45:    }
  47:    private static void validate
  48:        (List<Object> instruments)
  49:    {
  50:    }

If you try to execute the lines of code, you´ll note:

  • List<?>, which is the wildcard <?> bounded type, also know as List<capture-of ?>, simply means “any type.” That is, it could be a List of <Guitar>, <Flute>,<Drums>, whatever. It also means that you cannot ADD anything to the list referred to as List<?>.
  • List<Object> only accepts Object as an argument. Not Guitars, Drums, Flutes or Integers. If you have a method which the argument specifies List<Object>, this method can only take a List<Object>. The compiler allows you to add to the List<Object>, since you pass an Object as an argument.

So, there are differences between List<?> and List<Object>. The above code doesn´t compile, because an List<Drums> is being passed to a method which has a List<Object> as argument. But if you modify the method to this one:

   1:private static void validate
   2:        (List<? extends Object> instruments)

The code now compiles!! So, we saw the behavior of List<?> and List<? extends Object> is the same! They both means “I can refer to any type of object”. But neither List<?> nor List<?> nor List<? extends Object> are the same as List<Object>. When you see code using the wildcard notation (?), you can think: “this code refer to many options”. If you try to add something to a List<?>, the compiler won’t let you, because whether it were possible, it would be an unsafe operation, as you could pass a List<Guitar> to a method which receives a List<?> and add, say, a String to the list, as List<?> accepts “any type”. So now you may think: “Ok, Generics is a good feature, I can now create type safe collections and work with them in a safer way at compile time, that’s very good”. But everything isn’t the way we’d want it to be. If you try to mix Java generics code and legacy code,

   1:private static void add(List instruments)
   3:    instruments.add("45");



It compiles!! But let’s deal with it in another post.