01: /*
02: * Created on Oct 15, 2004
03: */
04: package uk.org.ponder.reflect;
05:
06: import java.util.Comparator;
07:
08: import uk.org.ponder.saxalizer.AccessMethod;
09: import uk.org.ponder.saxalizer.MethodAnalyser;
10: import uk.org.ponder.saxalizer.SAXalizerMappingContext;
11: import uk.org.ponder.util.UniversalRuntimeException;
12:
13: /**
14: * Returns a comparator capable of comparing any objects with {valid
15: * SAXalizer mappings for a get accessor method with a given name} by
16: * a member mapped by the accessor.
17: * @author Antranig Basman (antranig@caret.cam.ac.uk)
18: *
19: */
20: public class FieldComparator implements Comparator {
21:
22: public static AccessMethod findSingleGetter(Class objclass,
23: SAXalizerMappingContext context, String tagname) {
24: MethodAnalyser ma = context.getAnalyser(objclass);
25: AccessMethod method = ma.getAccessMethod(tagname);
26: if (!method.canGet() || method.isDenumerable()) {
27: throw new UniversalRuntimeException(
28: "Located access method of unsuitable type for name "
29: + tagname + " in " + objclass);
30: }
31: return method;
32: }
33:
34: private AccessMethod accessor;
35:
36: public FieldComparator(Class objclass,
37: SAXalizerMappingContext context, String fieldname) {
38: accessor = findSingleGetter(objclass, context, fieldname);
39: }
40:
41: public int compare(Object o1, Object o2) {
42: Comparable field1 = (Comparable) accessor.getChildObject(o1);
43: Comparable field2 = (Comparable) accessor.getChildObject(o2);
44: return field1.compareTo(field2);
45: }
46:
47: }
|