View Javadoc

1   /*
2    * Copyright 2004 Sun Microsystems, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package com.sun.syndication.fetcher.impl;
18  
19  import java.io.Serializable;
20  import java.net.URL;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  
26  /***
27   * <p>A very simple implementation of the {@link com.sun.syndication.fetcher.impl.FeedFetcherCacheI} interface.</p>
28   * 
29   * <p>This implementation uses a HashMap to cache retrieved feeds. This implementation is
30   * most suitible for sort term (client aggregator?) use, as the memory usage will increase
31   * over time as the number of feeds in the cache increases.</p>
32   * 
33   * @author Nick Lothian
34   *
35   */
36  public class HashMapFeedInfoCache implements FeedFetcherCacheI, Serializable {
37  	static HashMapFeedInfoCache _instance;
38  	
39  	private Map infoCache;
40  	
41  	/***
42  	 * <p>Constructor for HashMapFeedInfoCache</p>
43  	 * 
44  	 * <p>Only use this if you want multiple instances of the cache. 
45  	 * Usually getInstance() is more appropriate.</p>
46  	 *
47  	 */
48  	public HashMapFeedInfoCache() {
49  		setInfoCache(Collections.synchronizedMap(new HashMap()));
50  	}
51  
52  	/***
53  	 * Get the global instance of the cache
54  	 * @return an implementation of FeedFetcherCacheI
55  	 */
56  	public static synchronized FeedFetcherCacheI getInstance() {
57  		if (_instance == null) {
58  			_instance = new HashMapFeedInfoCache();			
59  		}
60  		return _instance;
61  	}
62  
63  	protected Object get(Object key) {
64  		return infoCache.get(key);
65  	}
66  
67  	/***
68  	 * @see extensions.io.FeedFetcherCacheI#getFeedInfo(java.net.URL)
69  	 */
70  	public SyndFeedInfo getFeedInfo(URL feedUrl) {
71  		return (SyndFeedInfo) get(feedUrl);
72  	}
73  
74  	protected void put(Object key, Object value) {
75  		infoCache.put(key, value);
76  	}
77  
78  	/***
79  	 * @see extensions.io.FeedFetcherCacheI#setFeedInfo(java.net.URL, extensions.io.SyndFeedInfo)
80  	 */
81  	public void setFeedInfo(URL feedUrl, SyndFeedInfo syndFeedInfo) {
82  		put(feedUrl, syndFeedInfo);		
83  	}
84  
85  	protected Map getInfoCache() {
86  		return infoCache;
87  	}
88  
89  	protected void setInfoCache(Map map) {
90  		infoCache = map;
91  	}
92  
93  }