I spent entire yesterday evaluating the possibility of Generifying Commons-Collections and still keeps the backward compatibility. It turned out to be not as simple as I thought initially.
Java 5 type-safe Map and Collection strictly requires the data that you retrieve from it must be of the same type of the data you put into it. This becomes a major problem generifying a number of useful Map and Collection implementations in Commons-Collections. For example, MultiMap, TransformedMap. While my initial evaluation goal yet to be archived, what did come to my attention is a difficult question: is the definition of Map (similarly to Collection) interface overly restricted in Java 5?
Well, in the real life, I put an egg in incubator can get the chick back after sometime. So for what reason the get method must return the same type of data as the put(or add or set) method takes? Let's look at the refined Map definition:
This pseudo Map interface is less restricted so apparently to be much useful. Let's exam how MultiMap can be supported with the refined interface.
return ...;
}
// same logic was in put
}
return ...;
}
return putInternal(key, input)? input:null;
}
return ...;
}
}
extends AbstractHashMap<K, Collection<V>, I>
implements MultiMap<K, V, I>
{
Collection<V> c = get(key);
if(c==null) {
c= new ArrayList<V>();
putInternal(key, c);
}
c.add(value);
return true;
}
}
return addInternal(key, input)? input:null;
}
No comments:
Post a Comment