01: /*
02: * Copyright (C) 2007, 2008 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 19.09.2007 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.converters.reflection;
12:
13: import java.util.Comparator;
14: import java.util.Map;
15: import java.util.TreeMap;
16:
17: /**
18: * Sort the fields in the order of XStream 1.2.x. Fields are returned in their declaration order,
19: * fields of base classes last.
20: *
21: * @author Jörg Schaible
22: * @since 1.3
23: */
24: public class XStream12FieldKeySorter implements FieldKeySorter {
25:
26: public Map sort(final Class type, final Map keyedByFieldKey) {
27: final Map map = new TreeMap(new Comparator() {
28:
29: public int compare(final Object o1, final Object o2) {
30: final FieldKey fieldKey1 = (FieldKey) o1;
31: final FieldKey fieldKey2 = (FieldKey) o2;
32: int i = fieldKey2.getDepth() - fieldKey1.getDepth();
33: if (i == 0) {
34: i = fieldKey1.getOrder() - fieldKey2.getOrder();
35: }
36: return i;
37: }
38: });
39: map.putAll(keyedByFieldKey);
40: return map;
41: }
42:
43: }
|