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.Iterator;
23  import java.util.LinkedList;
24  
25  import javax.swing.Icon;
26  
27  import org.xnap.peer.Peer;
28  import org.xnap.plugin.Plugin;
29  
30  /***
31   * Stores a list of {@link SearchResult} objects. Acts as a proxy for the 
32   * first added result.
33   */
34  public abstract class AbstractSearchResultContainer 
35      implements SearchResultContainer {
36  
37      //--- Constant(s) ---
38  
39      //--- Data field(s) ---
40  
41  	/***
42  	 * A list of {@link SearchResult} objects.
43  	 */
44      protected LinkedList results = new LinkedList();
45  
46      private SearchResult defaultResult;
47      private int availability;
48  
49      //--- Constructor(s) ---
50  
51      public AbstractSearchResultContainer()
52      {
53      }
54  
55      //--- Method(s) ---
56  
57      /***
58       * Adds <code>result</code> to the container.
59       */
60      public boolean add(SearchResult result)
61      {
62  		if (defaultResult == null) {
63  			defaultResult = result;
64  		}
65  
66  		results.add(result);
67  		availability += result.getAvailability();
68  
69  		return true;
70      }
71  
72      public boolean canGroup(SearchResult result)
73      {
74  		return defaultResult.canGroup(result);
75      }
76  
77      /***
78       * This should not be invoked.
79       */
80      public SearchResultContainer createContainer()
81      {
82  		return null;
83      }
84  
85      /***
86       * Returns the sum of the availability of all results.
87       */
88      public int getAvailability()
89      {
90  		return availability;
91      }
92  
93      public Object get(String key)
94      {
95  		return defaultResult.get(key);
96      }
97  
98  	public int getChildCount()
99  	{
100 		return results.size();
101 	}
102 
103     public String getFilename()
104     {
105 		return defaultResult.getFilename();
106     }
107 
108     public long getFilesize()
109     {
110 		return defaultResult.getFilesize();
111     }
112 
113     public SearchResult getFirst()
114     {
115 		return (!results.isEmpty()) ? (SearchResult)results.getFirst() : null;
116     }
117 
118 	public Object getHash()
119 	{
120 		return defaultResult.getHash();
121 	}
122 
123     /***
124      * Returns null;
125      */
126     public Icon getIcon()
127     {
128 		return defaultResult.getIcon();
129     }
130 
131     public String[] getPath()
132     {
133 		return defaultResult.getPath();
134     }
135 
136     public Plugin getPlugin()
137     {
138 		return defaultResult.getPlugin();
139     }
140 
141     public String getShortFilename()
142     {
143 		return defaultResult.getShortFilename();
144     }
145 
146     /***
147      * Returns null.
148      */
149     public Peer getPeer()
150     {
151 		return null;
152     }
153 
154 	/***
155 	 * Returns the number of children.
156 	 */
157 	public int getSourcesCount()
158 	{
159 		return results.size();
160 	}
161 
162     /***
163      * Returns an iterator over all contained {@link SearchResult} objects.
164      */
165     public Iterator iterator()
166     {
167 		return results.iterator();
168     }
169 
170     public Iterator keys()
171     {
172 		return defaultResult.keys();
173     }
174 
175 
176     /***
177 	 * Sets all properties of this container to <code>result's</code> 
178      * properties. 
179      */
180 	public void setPropertiesFrom(SearchResult result)
181 	{
182 		if (results.contains(result)) {
183 			defaultResult = result;
184 		}
185     }
186 
187 }