1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.converters.collections;
28
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Comparator;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.SortedSet;
35 import java.util.TreeSet;
36
37 import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.converters.ConversionException;
38 import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.converters.MarshallingContext;
39 import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.converters.UnmarshallingContext;
40 import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.io.HierarchicalStreamReader;
41 import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.io.HierarchicalStreamWriter;
42 import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.mapper.Mapper;
43
44
45
46
47
48
49
50
51
52
53 public class TreeSetConverter extends CollectionConverter {
54
55 public TreeSetConverter(Mapper mapper) {
56 super(mapper);
57 }
58
59 public boolean canConvert(Class type) {
60 return type.equals(TreeSet.class);
61 }
62
63 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
64 TreeSet treeSet = (TreeSet) source;
65 Comparator comparator = treeSet.comparator();
66 if (comparator == null) {
67 writer.startNode("no-comparator");
68 writer.endNode();
69 } else {
70 writer.startNode("comparator");
71 writer.addAttribute("class", mapper().serializedClass(comparator.getClass()));
72 context.convertAnother(comparator);
73 writer.endNode();
74 }
75 super.marshal(source, writer, context);
76 }
77
78 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
79 reader.moveDown();
80 final SortedSet sortedSet;
81 final TreeSet result;
82 if (reader.getNodeName().equals("comparator")) {
83 String comparatorClass = reader.getAttribute("class");
84 Comparator comparator = (Comparator) context.convertAnother(null, mapper().realClass(comparatorClass));
85 sortedSet = new PresortedSet(comparator);
86 result = new TreeSet(comparator);
87 } else if (reader.getNodeName().equals("no-comparator")) {
88 sortedSet = new PresortedSet();
89 result = new TreeSet();
90 } else {
91 throw new ConversionException("TreeSet does not contain <comparator> element");
92 }
93 reader.moveUp();
94 super.populateCollection(reader, context, sortedSet);
95 result.addAll(sortedSet);
96 return result;
97 }
98
99 private static class PresortedSet implements SortedSet {
100 private final List list = new ArrayList();
101 private final Comparator comparator;
102
103 PresortedSet() {
104 this(null);
105 }
106
107 PresortedSet(Comparator comparator) {
108 this.comparator = comparator;
109 }
110
111 public boolean add(Object e) {
112 return this.list.add(e);
113 }
114
115 public boolean addAll(Collection c) {
116 return this.list.addAll(c);
117 }
118
119 public void clear() {
120 this.list.clear();
121 }
122
123 public boolean contains(Object o) {
124 return this.list.contains(o);
125 }
126
127 public boolean containsAll(Collection c) {
128 return this.list.containsAll(c);
129 }
130
131 public boolean equals(Object o) {
132 return this.list.equals(o);
133 }
134
135 public int hashCode() {
136 return this.list.hashCode();
137 }
138
139 public boolean isEmpty() {
140 return this.list.isEmpty();
141 }
142
143 public Iterator iterator() {
144 return this.list.iterator();
145 }
146
147 public boolean remove(Object o) {
148 return this.list.remove(o);
149 }
150
151 public boolean removeAll(Collection c) {
152 return this.list.removeAll(c);
153 }
154
155 public boolean retainAll(Collection c) {
156 return this.list.retainAll(c);
157 }
158
159 public int size() {
160 return this.list.size();
161 }
162
163 public List subList(int fromIndex, int toIndex) {
164 return this.list.subList(fromIndex, toIndex);
165 }
166
167 public Object[] toArray() {
168 return this.list.toArray();
169 }
170
171 public Object[] toArray(Object[] a) {
172 return this.list.toArray(a);
173 }
174
175 public Comparator comparator() {
176 return comparator;
177 }
178
179 public Object first() {
180 return list.isEmpty() ? null : list.get(0);
181 }
182
183 public SortedSet headSet(Object toElement) {
184 throw new UnsupportedOperationException();
185 }
186
187 public Object last() {
188 return list.isEmpty() ? null : list.get(list.size() - 1);
189 }
190
191 public SortedSet subSet(Object fromElement, Object toElement) {
192 throw new UnsupportedOperationException();
193 }
194
195 public SortedSet tailSet(Object fromElement) {
196 throw new UnsupportedOperationException();
197 }
198 }
199 }