View Javadoc

1   /*
2    * @(#) Region.java May 9, 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.awt.Point;
16  import java.util.ArrayList;
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  
24  import nl.tudelft.simulation.content.HandlerInterface;
25  import nl.tudelft.simulation.language.d3.DirectedPoint;
26  import nl.tudelft.simulation.supplychain.actor.SupplyChainActor;
27  
28  /***
29   * A region is used to specify the geographical bounds. For the actors part of
30   * an region statistics are collected, in this case statistics with regards to
31   * orders. Regions may overlap eachother. Using lat/lon coordinates, a rough
32   * estimation of the North-American region is new Region(new Point(-125, 49),
33   * new Point(-68, 23)).
34   * <p>
35   * (c) copyright 2005 <a href="http://www.simulation.tudelft.nl">Delft
36   * University of Technology </a>, the Netherlands. <br>
37   * See for project information <a
38   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
39   * 
40   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
41   * Delft, the Netherlands. All rights reserved.
42   * 
43   * See for project information <a href="http://www.simulation.tudelft.nl/">
44   * www.simulation.tudelft.nl </a>.
45   * 
46   * The source code and binary code of this software are proprietary information
47   * of Delft University of Technology.
48   * 
49   * @author <a
50   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
51   *         van Houten </a>
52   * @version $Revision: 1.1 $ $Date: 2005/08/03 08:52:49 $
53   * @since 1.1.3
54   */
55  public class Region implements RegionInterface
56  {
57  	/*** the serial version uid */
58  	private static final long serialVersionUID = 11L;
59  
60  	/*** for debugging */
61  	private static final boolean DEBUG = false;
62  
63  	/*** upper left */
64  	private Point upperLeft = null;
65  
66  	/*** down right */
67  	private Point downRight = null;
68  
69  	/*** the set with actors */
70  	private Set actors = null;
71  
72  	/*** the statistic handlers */
73  	private Map statisticHandlers = null;
74  
75  	/*** the name of the region */
76  	private String name = null;
77  
78  	/***
79  	 * constructs a new Region
80  	 * 
81  	 * @param name the name of the region
82  	 * @param upperLeft the upper left of the region
83  	 * @param downRight the down right of the region
84  	 */
85  	public Region(final String name, final Point upperLeft,
86  			final Point downRight)
87  	{
88  		this.upperLeft = upperLeft;
89  		this.downRight = downRight;
90  		this.actors = new HashSet();
91  		this.statisticHandlers = new HashMap();
92  		this.name = name;
93  	}
94  
95  	/***
96  	 * @see org.gscg.gameactors.statistics.RegionInterface#addStatisticHandler(java.lang.Class,
97  	 *      nl.tudelft.simulation.content.HandlerInterface)
98  	 */
99  	public void addStatisticHandler(final Class contentClass,
100 			final HandlerInterface handler)
101 	{
102 		Set handlers = (Set) this.statisticHandlers.get(contentClass);
103 		if (handlers == null)
104 		{
105 			handlers = new HashSet();
106 			this.statisticHandlers.put(contentClass, handlers);
107 		}
108 		handlers.add(handler);
109 	}
110 
111 	/***
112 	 * @see org.gscg.gameactors.statistics.RegionInterface#addSupplyChainActor(nl.tudelft.simulation.supplychain.actor.SupplyChainActor)
113 	 */
114 	public boolean addSupplyChainActor(final SupplyChainActor actor)
115 	{
116 		if (!this.actors.contains(actor))
117 		{
118 			if (this.contains(actor.getLocation()))
119 			{
120 				for (Iterator it = this.statisticHandlers.keySet().iterator(); it
121 						.hasNext();)
122 				{
123 					Class key = (Class) it.next();
124 					Set handlers = (Set) this.statisticHandlers.get(key);
125 					for (Iterator handlerIterator = handlers.iterator(); handlerIterator
126 							.hasNext();)
127 					{
128 						actor.addContentHandler(key,
129 								(HandlerInterface) handlerIterator.next());
130 						if (Region.DEBUG)
131 						{
132 							System.out.println("DEBUG -- Region: " + this.name
133 									+ ": statistic content handler added to: "
134 									+ actor);
135 						}
136 					}
137 				}
138 				return true;
139 			}
140 		}
141 		return false;
142 	}
143 
144 	/***
145 	 * @return retuns the name of the region
146 	 */
147 	public String getName()
148 	{
149 		return this.name;
150 	}
151 
152 	/***
153 	 * checks whether a location is contained by the region
154 	 * 
155 	 * @param point the point to check
156 	 * @return returns false if the location is not part of the region
157 	 */
158 	private boolean contains(final DirectedPoint point)
159 	{
160 		if (point.x >= this.upperLeft.x && point.x <= this.downRight.x)
161 		{
162 			if (point.y <= this.upperLeft.y && point.y >= this.downRight.y)
163 			{
164 				return true;
165 			}
166 		}
167 		return false;
168 	}
169 
170 	/***
171 	 * @see org.gscg.gameactors.statistics.RegionInterface#getStatisticHandlers()
172 	 */
173 	public List getStatisticHandlers()
174 	{
175 		List list = new ArrayList();
176 		for (Iterator it = this.statisticHandlers.keySet().iterator(); it
177 				.hasNext();)
178 		{
179 			Set set = (Set) this.statisticHandlers.get(it.next());
180 			list.addAll(set);
181 		}
182 		return list;
183 	}
184 }