1
2
3
4
5
6
7 package org.mule.config;
8
9 import java.util.Comparator;
10 import java.util.Iterator;
11
12
13
14
15
16
17 public class PreferredObjectSelector<T>
18 {
19
20 private final Comparator<T> comparator;
21
22 public PreferredObjectSelector()
23 {
24 comparator = new Comparator<T>()
25 {
26 private PreferredComparator preferredComparator = new PreferredComparator();
27
28 public int compare(T threadPoolFactory, T threadPoolFactory1)
29 {
30 final Preferred preferred = threadPoolFactory.getClass().getAnnotation(Preferred.class);
31 final Preferred preferred1 = threadPoolFactory1.getClass().getAnnotation(Preferred.class);
32
33 return preferredComparator.compare(preferred, preferred1);
34 }
35 };
36 }
37
38
39
40
41
42
43
44
45
46
47
48 public T select(Iterator<T> iterator)
49 {
50 T preferred = null;
51
52 if (iterator.hasNext())
53 {
54 preferred = iterator.next();
55
56 while (iterator.hasNext())
57 {
58 T current = iterator.next();
59
60 if (comparator.compare(preferred, current) == -1)
61 {
62 preferred = current;
63 }
64 }
65 }
66
67 return preferred;
68 }
69 }