for (Iterator p = profiles.iterator(); p.hasNext(); ) {
ProfileImpl profile = (ProfileImpl) p.next();
for (Iterator i = profile.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
AttributeType type = (AttributeType) entry.getValue();
if (type.getBinding().isAssignableFrom(clazz)) {
assignable.add(entry);
}
if (clazz.equals(type.getBinding())) {
return (Name) entry.getKey();
}
}
}
if (assignable.isEmpty()) {
return null;
}
if (assignable.size() == 1) {
return (Name) ((Map.Entry) assignable.get(0)).getKey();
} else {
//sort
Comparator comparator = new Comparator() {
public int compare(Object o1, Object o2) {
Map.Entry e1 = (Map.Entry) o1;
Map.Entry e2 = (Map.Entry) o2;
AttributeType a1 = (AttributeType) e1.getValue();
AttributeType a2 = (AttributeType) e2.getValue();
Class c1 = a1.getBinding();
Class c2 = a2.getBinding();
if (c1.equals(c2)) {
return 0;
}
if (c1.isAssignableFrom(c2)) {
return 1;
}
return -1;
}
};
Collections.sort(assignable, comparator);
Map.Entry e1 = (Map.Entry) assignable.get(0);
Map.Entry e2 = (Map.Entry) assignable.get(1);
AttributeType a1 = (AttributeType) e1.getValue();
AttributeType a2 = (AttributeType) e2.getValue();
if (!a1.equals(a2)) {
return (Name) e1.getKey();
}
}