// JP opened flex table

Click to See Complete Forum and Search --> : instanceof UnmodifiableList<?>


bigBA
July 22nd, 2008, 04:54 AM
Hi,

i have a method which gets some kind of output from a data call and in case that output is a List<?> turns it into an UnmodifiableList.


if (callOutput instanceof List<?>)
{
callOutput = Collections.unmodifiableList((List<? extends Object>) callOutput);
}


now i would like add the check if callOutput isn't already an instance of UnmodifiableList<?>.

Has anyone an idea how to it?

instanceof UnmodifiableList does not work because UnmodifiableList is declared as inner class of java.util.Collections without any visibility modifier and therefor not public.

dlorde
July 22nd, 2008, 05:41 AM
Interesting question...

The class of the unmodifiable list is accessible via getClass(), but there are two kinds of unmodifiable list (random access or non-random access) that may be returned by Collections, so my best guess would be:if (!callOutput.getClass().getName().contains("Unmodifiable")) {
callOutput = Collections.unmodifiableList(callOutput);
}It might be best to stick this into a utility method somewhere, e.g. ensureReadOnly(..), or something.

It's a bit of a hack, but I've seen worse ;-)

Any programming problem can be solved by adding a level of indirection...
Anon.

bigBA
July 22nd, 2008, 09:50 AM
Yeah.. hm. I'll have to check if this has too much impact in the performance.

In the mean time i had to adjust the existing instanceof into
callOutput.getClass() == ArrayList.class
because we also have cusomt lists which extend ArrayList and which would loose added methods if stored inside an unmodifiable list...

Well, and here we go.. implementing a custom UnmodifiableList (extend it) isn't possible *thumbsup* :)

//JP added flex table