View Javadoc

1   /*
2    * @(#) ContentStatisticsLayer.java May 10, 2005
3    * 
4    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands. All rights reserved.
6    * 
7    * See for project information <a href="http://www.simulation.tudelft.nl/">
8    * www.simulation.tudelft.nl </a>.
9    * 
10   * The source code and binary code of this software are proprietary information
11   * of Delft University of Technology.
12   */
13  package org.gscg.gameactors.statistics;
14  
15  import java.io.Serializable;
16  import java.rmi.RemoteException;
17  import java.util.HashMap;
18  import java.util.HashSet;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  import java.util.TreeSet;
24  
25  import nl.tudelft.simulation.event.Event;
26  import nl.tudelft.simulation.event.EventProducer;
27  import nl.tudelft.simulation.event.EventType;
28  import nl.tudelft.simulation.logger.Logger;
29  
30  import org.gscg.common.gui.statistics.components.XYSeriesComparator;
31  import org.gscg.common.interactionlayer.AnnounceInterface;
32  import org.gscg.singleuser.interactionlayer.SingleUserInteractionLayerInterface;
33  
34  /***
35   * The ContentStatisticsLayer is the layer in between the statistics which are
36   * kept track of for content, and the interaction layers of players. These
37   * content statistics can be region dependent.
38   * <p>
39   * (c) copyright 2005 <a href="http://www.simulation.tudelft.nl">Delft
40   * University of Technology </a>, the Netherlands. <br>
41   * See for project information <a
42   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
43   * 
44   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
45   * Delft, the Netherlands. All rights reserved.
46   * 
47   * See for project information <a href="http://www.simulation.tudelft.nl/">
48   * www.simulation.tudelft.nl </a>.
49   * 
50   * The source code and binary code of this software are proprietary information
51   * of Delft University of Technology.
52   * 
53   * @author <a
54   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
55   *         van Houten </a>
56   * @version $Revision: 1.2 $ $Date: 2005/08/09 15:43:41 $
57   * @since 1.1.3
58   */
59  public class ContentStatisticsLayer implements AnnounceInterface, Serializable
60  {
61  	/*** the serial version uid */
62  	private static final long serialVersionUID = 11L;
63  
64  	/*** the single user interaction layer */
65  	private SingleUserInteractionLayerInterface owner = null;
66  
67  	/*** links a statistic event type to a set of content handlers */
68  	private Map statisticEventTypeToStatiscContentHandler = null;
69  
70  	/***
71  	 * constructs a new ContentStatisticsLayer
72  	 * 
73  	 * @param owner the owner
74  	 */
75  	public ContentStatisticsLayer(
76  			final SingleUserInteractionLayerInterface owner)
77  	{
78  		super();
79  		this.owner = owner;
80  		this.statisticEventTypeToStatiscContentHandler = new HashMap();
81  
82  		Set allRegions = new HashSet();
83  		allRegions.addAll(ManufacturerStatistics.getRegions());
84  		allRegions.addAll(DistributorStatistics.getRegions());
85  
86  		for (Iterator it = allRegions.iterator(); it.hasNext();)
87  		{
88  			RegionInterface region = (RegionInterface) it.next();
89  			List statisticHandlers = region.getStatisticHandlers();
90  
91  			for (int i = 0; i < statisticHandlers.size(); i++)
92  			{
93  				StatisticContentHandlerInterface handler = (StatisticContentHandlerInterface) statisticHandlers
94  						.get(i);
95  				EventType[] eventTypes = handler.getStatisticEventTypes();
96  
97  				for (int ii = 0; ii < eventTypes.length; ii++)
98  				{
99  					if (!this.statisticEventTypeToStatiscContentHandler
100 							.containsKey(eventTypes[ii]))
101 					{
102 						Set handlers = new HashSet();
103 						this.statisticEventTypeToStatiscContentHandler.put(
104 								eventTypes[ii], handlers);
105 					}
106 					Set set = (Set) this.statisticEventTypeToStatiscContentHandler
107 							.get(eventTypes[ii]);
108 					set.add(handler);
109 
110 					// subscribe the owner to the event
111 					((EventProducer) handler)
112 							.addListener(owner, eventTypes[ii]);
113 					try
114 					{
115 						this.owner.addStatisticEventType(eventTypes[ii]);
116 						this.owner.addEventTypeToAnnounceList(eventTypes[ii],
117 								this);
118 					} catch (RemoteException remoteException)
119 					{
120 						Logger.severe(this, "<init>", remoteException);
121 					}
122 				}
123 			}
124 		}
125 	}
126 
127 	/***
128 	 * @see org.gscg.common.interactionlayer.AnnounceInterface#announce(nl.tudelft.simulation.event.EventType,
129 	 *      boolean)
130 	 */
131 	public void announce(EventType eventType, boolean announce)
132 	{
133 		// first get the handlers
134 		Set handlers = (Set) this.statisticEventTypeToStatiscContentHandler
135 				.get(eventType);
136 		try
137 		{
138 			Map map = new HashMap();
139 
140 			for (Iterator it = handlers.iterator(); it.hasNext();)
141 			{
142 				StatisticContentHandlerInterface handler = (StatisticContentHandlerInterface) it
143 						.next();
144 				if (!map.containsKey(handler.getRegion().getName()))
145 				{
146 					Set set = new TreeSet(new XYSeriesComparator());
147 					map.put(handler.getRegion().getName(), set);
148 				}
149 				Set set = (Set) map.get(handler.getRegion().getName());
150 				set.addAll(handler.getSeries());
151 			}
152 			this.owner.notify(new Event(eventType, this, map));
153 		} catch (RemoteException remoteException)
154 		{
155 			Logger.severe(this, "announce", remoteException);
156 		}
157 	}
158 }