View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      // --- Constants(s) ---
35  
36      //--- Data Field(s) ---
37  
38      private String name;
39      private List providers = new LinkedList();
40  
41      //--- Constructor(s) ---
42  
43      public SearchProviderContainer(String name)
44      {
45  		this.name = name;
46      }
47  
48      // --- Method(s) ---
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 	//--- Inner Class(es) ---
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