summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/control/EOSortOrdering.java
blob: e2ce595f09e6766078b1dbf623e40bb1818998b6 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Michael Powers

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.control;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import net.wotonomy.foundation.NSArray;
import net.wotonomy.foundation.NSMutableArray;
import net.wotonomy.foundation.NSSelector;

/**
 * EOSortOrdering defines a sort key and operation. DisplayGroups use lists of
 * EOSortOrdering to determine how to order their items.
 *
 * @author michael@mpowers.net
 * @author $Author: cgruber $
 * @version $Revision: 894 $
 */
public class EOSortOrdering implements Serializable, EOKeyValueArchiving {
	/**
	 * Sorts items in ascending order.
	 */
	public static final NSSelector CompareAscending = new CompareAscendingComparator();

	/**
	 * Sorts items in descending order.
	 */
	public static final NSSelector CompareDescending = new CompareDescendingComparator();

	/**
	 * Sorts items' string representations in ascending order in a case insensitive
	 * manner.
	 */
	public static final NSSelector CompareCaseInsensitiveAscending = new CompareCaseInsensitiveAscendingComparator();

	/**
	 * Sorts items' string representations in descending order in a case insensitive
	 * manner.
	 */
	public static final NSSelector CompareCaseInsensitiveDescending = new CompareCaseInsensitiveDescendingComparator();

	protected String key;
	protected NSSelector selector;

	/**
	 * Factory-style constructor returns a new EOSortOrdering instance with the
	 * specified key and selector. Neither may be null.
	 */
	public static EOSortOrdering sortOrderingWithKey(String key, NSSelector selector) {
		return new EOSortOrdering(key, selector);
	}

	/**
	 * Constructor creates an EOSortOrdering that uses the specified key and
	 * selector. Neither may be null.
	 */
	public EOSortOrdering(String aKey, NSSelector aSelector) {
		key = aKey;
		selector = aSelector;
	}

	/**
	 * Constructor creates an EOSortOrdering that uses the specified key and
	 * comparator. Neither may be null. Not in the spec.
	 */
	public EOSortOrdering(String aKey, Comparator aComparator) {
		key = aKey;
		selector = new NSSelector(aKey, aComparator);
	}

	/**
	 * Returns the property key.
	 */
	public String key() {
		return key;
	}

	/**
	 * Returns the selector.
	 */
	public NSSelector selector() {
		return selector;
	}

	public String toString() {
		return "[EOSortOrdering: key='" + key + "' selector='" + selector + "']";
	}

	public boolean equals(Object anObject) {
		if (anObject instanceof EOSortOrdering) {
			EOSortOrdering x = (EOSortOrdering) anObject;
			if (selector().equals(x.selector())) {
				if (key().equals(x.key())) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * Sorts the specified list in place according to the specified list of
	 * EOSortOrderings. The items will be sorted first by the first ordering, and
	 * items with equal values for that property will be sorted by the next
	 * ordering, and so on.
	 */
	public static void sortArrayUsingKeyOrderArray(List anObjectList, List aSortOrderingList) {
		List keys = new ArrayList(aSortOrderingList);
		Collections.reverse(keys);
		Iterator it = keys.iterator();
		EOSortOrdering sortOrdering;
		while (it.hasNext()) {
			sortOrdering = (EOSortOrdering) it.next();
			Collections.sort(anObjectList, new DelegatingComparator(sortOrdering.key(), sortOrdering.selector()));
		}
	}

	/**
	 * Sorts the specified list in place according to the specified list of
	 * EOSortOrderings. The items will be sorted first by the first ordering, and
	 * items with equal values for that property will be sorted by the next
	 * ordering, and so on.
	 */
	public static NSArray sortedArrayUsingKeyOrderArray(List anObjectList, List aSortOrderingList) {
		NSArray result = new NSMutableArray();
		result.addAll(anObjectList);
		sortArrayUsingKeyOrderArray(result, aSortOrderingList);
		return result;
	}

	public static Object decodeWithKeyValueUnarchiver(EOKeyValueUnarchiver arch) {
		String k = (String) arch.decodeObjectForKey("key");
		String sname = (String) arch.decodeObjectForKey("selectorName");
		NSSelector sel = null;
		if (sname.equals("compareAscending:"))
			sel = CompareAscending;
		else if (sname.equals("compareDescending:"))
			sel = CompareDescending;
		else if (sname.equals("compareCaseInsensitiveAscending:"))
			sel = CompareCaseInsensitiveAscending;
		else if (sname.equals("compareCaseInsensitiveDescending:"))
			sel = CompareCaseInsensitiveAscending;
		else {
			if (sname.endsWith(":"))
				sname = sname.substring(0, sname.length() - 1);
			sel = new NSSelector(sname, new Class[] { Object.class });
		}
		return new EOSortOrdering(k, sel);
	}

	public void encodeWithKeyValueArchiver(EOKeyValueArchiver arch) {
		arch.encodeObject("EOSortOrdering", "class");
		arch.encodeObject(key(), "key");
		if (selector.equals(CompareAscending))
			arch.encodeObject("compareAscending:", "selectorName");
		else if (selector.equals(CompareDescending))
			arch.encodeObject("compareDescending:", "selectorName");
		else if (selector.equals(CompareCaseInsensitiveAscending))
			arch.encodeObject("compareCaseInsensitiveAscending:", "selectorName");
		else if (selector.equals(CompareCaseInsensitiveAscending))
			arch.encodeObject("compareCaseInsensitiveDescending:", "selectorName");
		else
			arch.encodeObject(selector.name() + ":", "selectorName");
	}

	private static class CompareAscendingComparator extends NSSelector {
		public int compare(Object o1, Object o2) {
			if (o1 instanceof Comparable) {
				if (o2 instanceof Comparable) {
					return ((Comparable) o1).compareTo(o2);
				}
			}

			// null handling: null is less than any object

			if (o1 == null) {
				if (o2 == null) {
					return 0;
				} else {
					return -1;
				}
			} else // o1 != null
			if (o2 == null) {
				return 1;
			}

			// fall back on string representation comparison

			return o1.toString().compareTo(o2.toString());
		}

		public boolean equals(Object obj) {
			return (this == obj);
		}
	}

	private static class CompareDescendingComparator extends CompareAscendingComparator {
		public int compare(Object o1, Object o2) {
			return -1 * super.compare(o1, o2);
		}
	}

	private static class CompareCaseInsensitiveAscendingComparator extends NSSelector {
		public int compare(Object o1, Object o2) {
			// null handling: null is less than any object

			if (o1 == null) {
				if (o2 == null) {
					return 0;
				} else {
					return -1;
				}
			} else // o1 != null
			if (o2 == null) {
				return 1;
			}

			return o1.toString().toLowerCase().compareTo(o2.toString().toLowerCase());
		}

		public boolean equals(Object obj) {
			return (this == obj);
		}
	}

	private static class CompareCaseInsensitiveDescendingComparator extends CompareCaseInsensitiveAscendingComparator {
		public int compare(Object o1, Object o2) {
			return -1 * super.compare(o1, o2);
		}
	}

	private static class DelegatingComparator implements Comparator {
		private String key;
		private Comparator comparator;

		public DelegatingComparator(String aKey, Comparator aComparator) {
			key = aKey;
			comparator = aComparator;
		}

		public int compare(Object o1, Object o2) {
			Object v1, v2;
			if (o1 instanceof EOKeyValueCoding) {
				v1 = ((EOKeyValueCoding) o1).valueForKey(key);
			} else {
				v1 = EOKeyValueCodingSupport.valueForKey(o1, key);
			}
			if (o2 instanceof EOKeyValueCoding) {
				v2 = ((EOKeyValueCoding) o2).valueForKey(key);
			} else {
				v2 = EOKeyValueCodingSupport.valueForKey(o2, key);
			}
			return comparator.compare(v1, v2);
		}

		public boolean equals(Object obj) {
			return (this == obj);
		}
	}

}

/*
 * $Log$ Revision 1.2 2006/02/16 16:47:14 cgruber Move some classes in to
 * "internal" packages and re-work imports, etc.
 *
 * Also use UnsupportedOperationExceptions where appropriate, instead of
 * WotonomyExceptions.
 *
 * Revision 1.1 2006/02/16 13:19:57 cgruber Check in all sources in
 * eclipse-friendly maven-enabled packages.
 *
 * Revision 1.14 2003/08/11 19:39:52 chochos now encodes/decodes correctly.
 *
 * Revision 1.13 2003/08/09 01:29:56 chochos implements EOKeyValueArchiving
 *
 * Revision 1.12 2003/08/06 23:07:52 chochos general code cleanup (mostly,
 * removing unused imports)
 *
 * Revision 1.11 2003/02/07 20:23:23 mpowers Provided backwards compatibility
 * for comparators.
 *
 * Revision 1.10 2003/01/18 23:46:58 mpowers EOSortOrdering is now correctly
 * using NSSelectors.
 *
 * Revision 1.9 2003/01/16 22:47:30 mpowers Compatibility changes to support
 * compiling woextensions source. (34 out of 56 classes compile!)
 *
 * Revision 1.8 2002/03/01 20:23:27 mpowers Implemented equals.
 *
 * Revision 1.7 2002/02/06 21:15:04 mpowers Added null handling to
 * CompareCaseInsensitiveAscendingComparator.
 *
 * Revision 1.6 2001/12/01 23:51:24 mpowers Made serializable.
 *
 * Revision 1.5 2001/03/29 03:29:49 mpowers Now using KeyValueCoding and Support
 * instead of Introspector.
 *
 * Revision 1.4 2001/01/24 22:15:52 mpowers Fixed npe when comparing nulls.
 *
 * Revision 1.3 2001/01/12 19:11:56 mpowers Fixed table column click sorting.
 *
 * Revision 1.2 2001/01/12 17:23:46 mpowers Inner classes are now private.
 *
 * Revision 1.1 2001/01/11 20:34:26 mpowers Implemented EOSortOrdering and added
 * support in framework. Added header-click to sort table columns.
 *
 *
 */