Source code: jgift/search/SearchResultGroup.java
1 /*
2 * This file is part of jgiFT.
3 * Copyright (c) 2003, Jason Shobe
4 *
5 * jgiFT is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * jgiFT 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 jgiFT; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 package jgift.search;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Comparator;
24
25 /**
26 * Container for a group of search results for the exact same file in different
27 * locations.
28 *
29 * @author Jason Shobe
30 * @version $Revision: 1.1 $
31 */
32 public class SearchResultGroup extends SearchResult {
33 /**
34 * Create a new instance of SearchResultGroup.
35 *
36 * @param hash the hash of all the results contained in the group.
37 */
38 public SearchResultGroup(String hash) {
39 setHash(hash);
40 }
41
42 /**
43 * Add a search result to this group.
44 *
45 * @param result the search result to add.
46 *
47 * @throws IllegalArgumentException if the result does not have the same
48 * hash as this group.
49 */
50 public void addSearchResult(SearchResult result) {
51 if(!getHash().equals(result.getHash())) {
52 throw new IllegalArgumentException("Different hash");
53 }
54
55 if(results.size() == 0) {
56 setFile(result.getFile());
57 setFileSize(result.getFileSize());
58 setMimeType(result.getMimeType());
59 setServer("");
60 setUrl("");
61 setUser("0");
62 }
63
64 setAvailability(getAvailability() + result.getAvailability());
65 setUser(String.valueOf(Integer.parseInt(getUser()) + 1));
66
67 results.add(result);
68 }
69
70 /**
71 * Get the number of results in this group.
72 *
73 * @return the number of results in this group.
74 */
75 public int getSearchResultCount() {
76 return results.size();
77 }
78
79 /**
80 * Get the search result at the specified location.
81 *
82 * @param index the index of the result.
83 *
84 * @return the requested search result.
85 */
86 public SearchResult getSearchResult(int index) {
87 return (SearchResult) results.get(index);
88 }
89
90 /**
91 * Sort the search results in the specified order.
92 *
93 * @param comparator the Comparator to use to sort the results.
94 */
95 public void sortSearchResults(Comparator comparator) {
96 Collections.sort(results, comparator);
97 }
98
99 private ArrayList results = new ArrayList();
100 }
101