« Adding Simplified Chinese locale - Expense Report (2) | Main | Reorganisation of internal state management - Expense Report (3) »

02 December 2008

List.append() - Fragmentation in MIDP

They say that Java ME has been fragmented due to proprietary extensions by manufactures and network operators. In reality, not only proprietary extensions but also MIDP, which should be a baseline, has been fragmented. This article describes an example that I have experienced with List class in Expense Report project.

I defined DataBountList class that extends List, which is for displaying expense summary. While List has string labels and icons for each item, DataBoundList additionally has arbitrary objects bound to items. In the case of Expense Report, each item is bound to an object that represents each expense.

I have added an Object parameter to methods adding an item like this:

public int append(String stringPart, Image imagePart, Object dataPart) {
  super.append(stringPart, imagePart);
  // Handling dataPart parameter
  ...
}

public void insert(int elementNum, String stringPart, Image imagePart, Object dataPart) {
  super.insert(elementNum, stringPart, imagePart);
  // Handling dataPart parameter
  ...
}

And have overridden append() and insert() methods that List class originally has so that they throw IllegalStateException – so was set() method but it is a bit out-of-topic here.

public int append(String stringPart, Image imagePart) {
  throw new IllegalStateException();
}

public void insert(int elementNum, String stringPart, Image imagePart) {
  throw new IllegalStateException();
}

When I tried this version on KDDI's mobile phone (KDDI is one of Japanese mobile phone operators), my application was aborted with IllegalStateException.  Meanwhile, it ran successfully on Nokia and Sony Ericsson handsets.  The application only invoked DataBoundList.append(String, Image, Object). I supposed that overridden version of DataBoundList.append() or insert() that did not have an Object parameter was invoked, and it resulted in throwing IllegalStateException.  After testing some situations, my conclusion was that, on KDDI's handset, DataBoundList.insert(int, String, Image) was invoked because List.append(String, Image) invoked insert(int, String, Image) internally.


MIDP specification says that, calling Choice.insert(int, String, Image) with designating a value equal to Choice.size() to the first parameter has the same effect as calling Choice.append(String, Image). Whereas, descriptions of Choice's implementation classes such as ChoiceGroup and List do not specify so. The behaviour of KDDI's looks in line with the sentence. However, my question is, “Do programmers really expect that List.append() invokes the subclasses insert() when he/she extends List class?” He/she may have to do so because List.insert() is not a “final” method. At least, the policy of KDDI's implementation requires it.


I have modified my code as the following in order to prevent the overriding version of DataBoundList.insert() being called from List.append().

public int append(String stringPart, Image imagePart, Object dataPart) {
  int index = size();
  insertImpl(index, stringPart, imagePart);
  return index;
}

public void insert(int elementNum, String stringPart, Image imagePart, Object dataPart) {
  insertImpl(elementNum, stringPart, imagePart);
}

private void insertImpl(int elementNum, String stringPart, Image imagePart, Object dataPart) {
  super.insert(elementNum, stringPart, imagePart);
  // Handling dataPart parameter
  ...
}

This is just a hobby programming. It is fun to fight fragmentation among MIDP implementations ;-)  Happy programming!

|

« Adding Simplified Chinese locale - Expense Report (2) | Main | Reorganisation of internal state management - Expense Report (3) »

Mobile」カテゴリの記事

Programming」カテゴリの記事

TrackBack

TrackBack URL for this entry:
http://app.cocolog-nifty.com/t/trackback/532178/45454772

Listed below are links to weblogs that reference List.append() - Fragmentation in MIDP:

« Adding Simplified Chinese locale - Expense Report (2) | Main | Reorganisation of internal state management - Expense Report (3) »