KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > syndication > fetcher > impl > HashMapFeedInfoCache


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 JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25
26 /**
27  * <p>A very simple implementation of the {@link com.sun.syndication.fetcher.impl.FeedFetcherCache} 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 FeedFetcherCache, Serializable JavaDoc {
37     static HashMapFeedInfoCache _instance;
38     
39     private Map JavaDoc 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 JavaDoc()));
50     }
51
52     /**
53      * Get the global instance of the cache
54      * @return an implementation of FeedFetcherCache
55      */

56     public static synchronized FeedFetcherCache getInstance() {
57         if (_instance == null) {
58             _instance = new HashMapFeedInfoCache();
59         }
60         return _instance;
61     }
62
63     protected Object JavaDoc get(Object JavaDoc key) {
64         return infoCache.get(key);
65     }
66
67     /**
68      * @see extensions.io.FeedFetcherCache#getFeedInfo(java.net.URL)
69      */

70     public SyndFeedInfo getFeedInfo(URL JavaDoc feedUrl) {
71         return (SyndFeedInfo) get(feedUrl);
72     }
73
74     protected void put(Object JavaDoc key, Object JavaDoc value) {
75         infoCache.put(key, value);
76     }
77
78     /**
79      * @see extensions.io.FeedFetcherCache#setFeedInfo(java.net.URL, extensions.io.SyndFeedInfo)
80      */

81     public void setFeedInfo(URL JavaDoc feedUrl, SyndFeedInfo syndFeedInfo) {
82         put(feedUrl, syndFeedInfo);
83     }
84
85     protected Map JavaDoc getInfoCache() {
86         return infoCache;
87     }
88
89     protected void setInfoCache(Map JavaDoc map) {
90         infoCache = map;
91     }
92
93 }
94
Popular Tags