Friday, June 8, 2007

Hi Dad, Hi Mom

Past two days: frustrating. I took on two tasks and have failed at them both.

1. I wrote a simple UI plugin that would query Discovery for the services sent to it by the various finders. However, I could not figure out a way to query the Discovery plugin from the UI plugin. Hmm, I thought. I ran to Barnes & Noble, read the book on Eclipse programming, thought I had the answer, came back and tried it. It didn't work.

2. I'd written a MultiMap class (a hash table from keys to multiple values) and then tried to write tests for it. Here's the code

public class MultiMap extends HashMap {

/* If KEY exists, insert VALUE under its Set,
* otherwise insert VALUE
into a new Set.

public Object put(Object key, Object value) {
Object oldValue = this.get(key);

if (oldValue != null){
oldValue = ((HashSet)oldValue).clone();
}else {
super.put(key, new HashSet().add(value));

return oldValue;

and here's the exception

java.lang.ClassCastException: java.lang.Boolean

For some reason, get(key) was returning Booleans instead of HashSets! Why? And no, I was not inserting Booleans of any sort into the MultiMap.

Oh well, here's hoping I get some inspiration by tomorrow...

P.S. Hi Dad. Hi Mom. Like my blog?


Bull said...

Because hashset.add(o) returns a boolean, not the hashet.

Aviv said...

Exactly as the previous poster said.
In the else branch you need to create the HashSet, add the value to it an then put() the set.

It's good practice to read documentation of API code you're not familiar with.
Also, if you used generics you'd see that you're not inserting the expected type.

Channing Walton said...

for a MultiMap you might want to look at Commons Collections.



Markus said...

Discovery targets Foundation 1.0, that's why generics are out of the question.

Oge said...

Thanks for the answers, y'all. I feel perfectly dolt-like for missing that one :-)

Bull said...

Don't feel bad, the only reason I was able to spot this is because i have made the same mistakes before too :-)

Also, since you can't use generics, I assume you are stuck on Java 1.4. However, since the compiler automatically converted boolean to java.lang.Boolean I assume you have your compiler setting at 1.5 or higher. You may want to set your compiler settings to 1.4 (and install a 1.4 vm) to ensure that you are not using utils that are not available in 1.4.