[EMAIL PROTECTED] wrote:
bayard 2002/10/22 20:35:23[snip]
Added: collections/src/test/org/apache/commons/collections
TestClassMap.java
collections/src/java/org/apache/commons/collections
ClassMap.java
Log:
Uses inheritence in the get() value lookup to decide which value to return.
I find this very useful when implementing registries of which Class acts
upon which value. So in a ConvertUtils class, a ClassMap would handle the
many Converters, deciding which Converter to use on which value.
1.1 jakarta-commons/collections/src/java/org/apache/commons/collections/ClassMap.java
Index: ClassMap.java
ClassMap looks like it needs more documentation describing the selection process if more than one superclass/interface matches the requested class passed to get. More specifically...
public Object get(Object key) {... should document that interfaces have a higher priority for matching than superclasses.
if(key == null) {
return null;
}
Class clss = null;
if(key instanceof Class) {
clss = (Class)key;
} else {
clss = key.getClass();
}
Object obj = super.get(clss);
if(obj == null) {
// if this is null, let's go up the inheritence tree
obj = getInterfaces(clss);
if(obj == null) {... and that the interfaces are checked in the order in which they are declared in the "implements" clause of the class.
obj = getSuperclass(clss);
}
}
return obj;
}
private Object getInterfaces(Class clss) {
if(clss == null) {
return null;
}
Object obj = null;
Class[] interfaces = clss.getInterfaces();
for(int i=0; i<interfaces.length; i++) {
obj = (Object)super.get(interfaces[i]);
if(obj != null) {... and that interfaces that implement other interfaces for a declared interface are checked before the next declared interface.
return obj; }
obj = getInterfaces(interfaces[i]);
if(obj != null) {
return obj; }
.. followed by super interfaces to a declared interface
obj = getSuperclass(interfaces[i]);in other words, the semantics of the interface selection stuff is pretty unclear without looking at the code, and even then, can be a bit confusing.
if(obj != null) {
return obj; }
}
return null;
}
private Object getSuperclass(Class clss) {
if(clss == null) {
return null;
}
Object obj = null;
Class superclass = clss.getSuperclass();
obj = (Object)super.get(superclass);
if(obj != null) {
return obj; }
obj = getInterfaces(superclass);
especially when you add in the interfaces of super classse
if(obj != null) {
return obj; obj = getSuperclass(superclass);
and super-super classes.
if(obj != null) {
return obj; }
return null;
}
:) michael -- Michael A. Smith [EMAIL PROTECTED] -- To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@;jakarta.apache.org> For additional commands, e-mail: <mailto:commons-dev-help@;jakarta.apache.org>