View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
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 Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.statistics.detailed.processing;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  import ch.elca.el4j.services.statistics.detailed.MeasureItem;
25  import ch.elca.el4j.services.statistics.detailed.cache.LRUCache;
26  import ch.elca.el4j.util.codingsupport.Reject;
27  
28  /**
29   * This class implements a non-persistant MeasureCollectionSerice. This means
30   * that this measure collector service is not backed by any database or file
31   * system. All the collected measurements are only stored in the memory. The
32   * cacheSize determines the maximum number of MeasureIDs, which are stored. If
33   * the no of MeasureIDs, exceeds the cacheSize all MeasureItems belonging to one
34   * MeasureId will be removed. This MeasureId for removal is chosen by a
35   * least-recently-used strategy.
36   *
37   *
38   * @svnLink $Revision: 3881 $;$Date: 2009-08-04 15:22:05 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/detailed_statistics/common/src/main/java/ch/elca/el4j/services/statistics/detailed/processing/NonPersistentMeasureCollectorService.java $
39   *
40   * @author Rashid Waraich (RWA)
41   * @author David Stefan (DST)
42   */
43  
44  public class NonPersistentMeasureCollectorService implements
45  	MeasureCollectorService {
46  
47  	/**
48  	 * Cache for Measurements.
49  	 */
50  	private LRUCache<String, List<MeasureItem>> m_cache;
51  
52  	/**
53  	 * Construtor.
54  	 *
55  	 * @param maxCacheSize
56  	 *            The maximum number of Measurements in the cache.
57  	 */
58  	public NonPersistentMeasureCollectorService(int maxCacheSize) {
59  		Reject.ifCondition(maxCacheSize < 0);
60  		m_cache = new LRUCache<String, List<MeasureItem>>(maxCacheSize);
61  	}
62  
63  	/**
64  	 * Won't do anything, as this is a non-persistant MeasureCollectorService.
65  	 */
66  	public void writeMeasures() {
67  
68  	}
69  
70  	/**
71  	 * {@inheritDoc}
72  	 */
73  	public void add(MeasureItem item) {
74  		List<MeasureItem> list = m_cache.get(item.getID().getFormattedString());
75  
76  		if (list == null) {
77  			list = new LinkedList<MeasureItem>();
78  		}
79  		synchronized (this) {
80  			list.add(item);
81  			m_cache.put(item.getID().getFormattedString(), list);
82  		}
83  	}
84  
85  	/**
86  	 * {@inheritDoc}
87  	 */
88  	public void delete(int amount) {
89  		m_cache.clear();
90  	}
91  
92  	/**
93  	 * {@inheritDoc}
94  	 */
95  	public List<MeasureItem> getAllMeasureItems() {
96  		List<MeasureItem> result = new ArrayList<MeasureItem>();
97  		synchronized (this) {
98  			Iterator<List<MeasureItem>> iter = m_cache.getAll().iterator();
99  			while (iter.hasNext()) {
100 				result = mergeLists(result, iter.next());
101 			}
102 		}
103 		return result;
104 	}
105 	
106 	
107 	/**
108 	 * {@inheritDoc}
109 	 */
110 	public List<MeasureItem> getFirstMeasureItems() {
111 		List<MeasureItem> list = new ArrayList<MeasureItem>();
112 
113 		synchronized (this) {
114 			Iterator<String> iter = m_cache.getKeys().iterator();
115 			while (iter.hasNext()) {
116 				list.add((m_cache.get(iter.next())).get(0));
117 			}
118 		}
119 		return list;
120 	}
121 
122 	
123 	/**
124 	 * Merge two lists.
125 	 *
126 	 * @param list1
127 	 *            First list.
128 	 * @param list2
129 	 *            Second list.
130 	 * @return The resulting list.
131 	 */
132 	private List<MeasureItem> mergeLists(List<MeasureItem> list1,
133 		List<MeasureItem> list2) {
134 
135 		List<MeasureItem> result = new ArrayList<MeasureItem>();
136 		Iterator iter = list1.iterator();
137 		while (iter.hasNext()) {
138 			result.add((MeasureItem) iter.next());
139 		}
140 		iter = list2.iterator();
141 		while (iter.hasNext()) {
142 			result.add((MeasureItem) iter.next());
143 		}
144 		return result;
145 	}
146 }