1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.search;
21
22 import java.util.Arrays;
23 import java.util.Comparator;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.LinkedList;
27 import java.util.List;
28
29 /***
30 *
31 */
32 public class SearchProviderContainer implements SearchProvider {
33
34
35
36
37
38 private String name;
39 private List providers = new LinkedList();
40
41
42
43 public SearchProviderContainer(String name)
44 {
45 this.name = name;
46 }
47
48
49
50 public void add(SearchProvider provider)
51 {
52 providers.add(provider);
53 }
54
55 /***
56 * Returns the average of all supported media types.
57 */
58 public MediaType[] getSupportedMediaTypes()
59 {
60 if (providers.size() > 0) {
61 HashSet common = new HashSet();
62 SearchProvider sp = (SearchProvider)providers.get(0);
63 MediaType[] types = sp.getSupportedMediaTypes();
64 if (types == null) {
65 return null;
66 }
67 common.addAll(Arrays.asList(types));
68
69 for (Iterator i = providers.iterator(); i.hasNext();) {
70 types = ((SearchProvider)i.next()).getSupportedMediaTypes();
71 if (types == null) {
72 return null;
73 }
74 common.retainAll(Arrays.asList(types));
75 }
76
77 MediaType[] array = (MediaType[])common.toArray(new MediaType[0]);
78 Arrays.sort(array, new MediaTypeComparator());
79 return array;
80 }
81
82 return null;
83 }
84
85 /***
86 * Returns the name.
87 */
88 public String getName()
89 {
90 return name;
91 }
92
93 public void remove(SearchProvider provider)
94 {
95 providers.remove(provider);
96 }
97
98 /***
99 * Returns a container that searches all providers.
100 */
101 public Search search(SearchFilter filter)
102 {
103 SearchContainer c = new SearchContainer(filter);
104 for (Iterator i = providers.iterator(); i.hasNext();) {
105 c.add(((SearchProvider)i.next()).search(filter));
106 }
107 return c;
108 }
109
110 public int size()
111 {
112 return providers.size();
113 }
114
115
116
117 public class MediaTypeComparator implements Comparator {
118
119 public int compare(Object o1, Object o2)
120 {
121 if (o1 == SearchManager.MEDIA_ANYTHING) {
122 return -1;
123 }
124 else if (o2 == SearchManager.MEDIA_ANYTHING) {
125 return 1;
126 }
127 else {
128 return ((MediaType)o1).getName().compareTo
129 (((MediaType)o2).getName());
130 }
131 }
132
133 }
134
135 }
136