1:
17: package ;
18:
19: import ;
20: import ;
21:
22: import ;
23: import ;
24: import ;
25: import ;
26: import ;
27:
28:
33: public final class Comparators
34: {
35: private Comparators()
36: {
37:
38: }
39:
40:
48: public static Comparator chain( final Comparator c0, final Comparator c1 )
49: {
50: return chain( new Comparator[]{c0, c1} );
51: }
52:
53:
62: public static Comparator chain( final Comparator c0, final Comparator c1, final Comparator c2 )
63: {
64: return chain( new Comparator[]{c0, c1, c2} );
65: }
66:
67:
77: public static Comparator chain(
78: final Comparator c0, final Comparator c1, final Comparator c2, final Comparator c3 )
79: {
80: return chain( new Comparator[]{c0, c1, c2, c3} );
81: }
82:
83:
94: public static Comparator chain(
95: final Comparator c0, final Comparator c1, final Comparator c2,
96: final Comparator c3, final Comparator c4 )
97: {
98: return chain( new Comparator[]{c0, c1, c2, c3, c4} );
99: }
100:
101:
108: public static Comparator chain( final List comparators )
109: {
110: return new Chain(
111: (Comparator[]) comparators.toArray(
112: new Comparator[ comparators.size() ] ) );
113: }
114:
115:
123: public static Comparator chain( final Comparator[] comparators )
124: {
125: return new Chain( comparators );
126: }
127:
128:
131: private static class Chain implements Comparator
132: {
133: private final Comparator[] m_chain;
134:
135:
139: public Chain( final Comparator[] chain )
140: {
141: m_chain = new Comparator[ chain.length ];
142: System.arraycopy( chain, 0, m_chain, 0, chain.length );
143: }
144:
145:
151: public int compare( final Object left, final Object right )
152: {
153: int result = 0;
154: for( int i = 0; result == 0 && i < m_chain.length; ++i )
155: {
156: result = m_chain[i].compare( left, right );
157: }
158: return result;
159: }
160: }
161:
162:
169: private static Comparator reverse( final Comparator wrapped )
170: {
171: return new Reverse( wrapped );
172: }
173:
174:
177: private static class Reverse implements Comparator
178: {
179: private final Comparator m_wrapped;
180:
181:
185: public Reverse( final Comparator wrapped )
186: {
187: m_wrapped = wrapped;
188: }
189:
190:
196: public int compare( final Object left, final Object right )
197: {
198: return -m_wrapped.compare( left, right );
199: }
200: }
201:
202:
208: public static Comparator groupFirst()
209: {
210: return new GroupFirst();
211: }
212:
213:
219: public static Comparator groupLast()
220: {
221: return reverse( groupFirst() );
222: }
223:
224:
227: private static class GroupFirst implements Comparator
228: {
229:
235: public int compare( final Object left, final Object right )
236: {
237: final boolean l = left instanceof Group;
238: final boolean r = right instanceof Group;
239:
240: if( l ^ r )
241: {
242: if( l )
243: {
244: return -1;
245: }
246: return 1;
247: }
248: return 0;
249: }
250: }
251:
252:
258: public static Comparator switchFirst()
259: {
260: return new SwitchFirst();
261: }
262:
263:
269: public static Comparator switchLast()
270: {
271: return reverse( switchFirst() );
272: }
273:
274:
277: private static class SwitchFirst implements Comparator
278: {
279:
285: public int compare( final Object left, final Object right )
286: {
287: final boolean l = left instanceof Switch;
288: final boolean r = right instanceof Switch;
289:
290: if( l ^ r )
291: {
292: if( l )
293: {
294: return -1;
295: }
296: return 1;
297: }
298: return 0;
299: }
300: }
301:
302:
308: public static Comparator commandFirst()
309: {
310: return new CommandFirst();
311: }
312:
313:
319: public static Comparator commandLast()
320: {
321: return reverse( commandFirst() );
322: }
323:
324:
327: private static class CommandFirst implements Comparator
328: {
329:
335: public int compare( final Object left, final Object right )
336: {
337: final boolean l = left instanceof Command;
338: final boolean r = right instanceof Command;
339:
340: if( l ^ r )
341: {
342: if( l )
343: {
344: return -1;
345: }
346: return 1;
347: }
348: return 0;
349: }
350: }
351:
352:
358: public static Comparator defaultOptionFirst()
359: {
360: return new DefaultOptionFirst();
361: }
362:
363:
369: public static Comparator defaultOptionLast()
370: {
371: return reverse( defaultOptionFirst() );
372: }
373:
374:
377: private static class DefaultOptionFirst implements Comparator
378: {
379:
385: public int compare( final Object left, final Object right )
386: {
387: final boolean l = left instanceof DefaultOption;
388: final boolean r = right instanceof DefaultOption;
389:
390: if( l ^ r )
391: {
392: if( l )
393: {
394: return -1;
395: }
396: return 1;
397: }
398: return 0;
399: }
400: }
401:
402:
411: public static Comparator namedFirst( final String name )
412: {
413: return new Named( name );
414: }
415:
416:
425: public static Comparator namedLast( final String name )
426: {
427: return reverse( new Named( name ) );
428: }
429:
430:
433: private static class Named implements Comparator
434: {
435: private final String m_name;
436:
437:
441: public Named( final String name )
442: {
443: m_name = name;
444: }
445:
446:
452: public int compare( final Object primary, final Object secondary )
453: {
454: final Option left = (Option) primary;
455: final Option right = (Option) secondary;
456:
457: final boolean l = left.getTriggers().contains( m_name );
458: final boolean r = right.getTriggers().contains( m_name );
459:
460: if( l ^ r )
461: {
462: if( l )
463: {
464: return -1;
465: }
466: return 1;
467: }
468: return 0;
469: }
470: }
471:
472:
478: public static Comparator preferredNameFirst()
479: {
480: return new PreferredName();
481: }
482:
483:
489: public static Comparator preferredNameLast()
490: {
491: return reverse( preferredNameFirst() );
492: }
493:
494:
497: private static class PreferredName implements Comparator
498: {
499:
505: public int compare( final Object primary, final Object secondary )
506: {
507: final Option left = (Option) primary;
508: final Option right = (Option) secondary;
509: return left.getPreferredName().compareTo( right.getPreferredName() );
510: }
511: }
512:
513:
519: public static Comparator requiredFirst()
520: {
521: return new Required();
522: }
523:
524:
530: public static Comparator requiredLast()
531: {
532: return reverse( requiredFirst() );
533: }
534:
535:
538: private static class Required implements Comparator
539: {
540:
546: public int compare( final Object primary, final Object secondary )
547: {
548: final Option left = (Option) primary;
549: final Option right = (Option) secondary;
550:
551: final boolean l = left.isRequired();
552: final boolean r = right.isRequired();
553:
554: if( l ^ r )
555: {
556: if( l )
557: {
558: return -1;
559: }
560: return 1;
561: }
562: return 0;
563: }
564: }
565: }