View Javadoc

1   /*
2    * @(#)SupplyChainHandler.java Mar 12, 2004
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 is proprietary information
11   * of Delft University of Technology.
12   */
13  
14  package nl.tudelft.simulation.supplychain.handlers;
15  
16  import java.io.Serializable;
17  import java.util.HashSet;
18  import java.util.List;
19  import java.util.Set;
20  
21  import nl.tudelft.simulation.content.Handler;
22  import nl.tudelft.simulation.logger.Logger;
23  import nl.tudelft.simulation.supplychain.actor.SupplyChainActor;
24  import nl.tudelft.simulation.supplychain.content.Content;
25  import nl.tudelft.simulation.supplychain.content.InternalDemand;
26  import nl.tudelft.simulation.supplychain.product.Product;
27  
28  /***
29   * SupplyChainHandler is the SupplyChainActor specific abstract Handler class.
30   * It has a SupplyChainActor as owner, making it unnecessary to cast the Actor
31   * all the time to a SupplyChainActor. <br>
32   * The generic SupplyChainHandler already has the methods to check whether the
33   * content is of the right type, and methods to do basic filtering on product
34   * and on the partner with whom the owner is dealing. This makes it very easy to
35   * have different handlers for e.g. production orders and for purchase orders;
36   * it can be done on the basis of the message sender (in case of production
37   * orders the owner itself), or on the basis of the product type. <br>
38   * <br>
39   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
40   * Delft, the Netherlands. All rights reserved.
41   * 
42   * See for project information <a href="http://www.simulation.tudelft.nl/">
43   * www.simulation.tudelft.nl </a>.
44   * 
45   * The source code and binary code of this software is proprietary information
46   * of Delft University of Technology.
47   * 
48   * @author <a
49   *         href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
50   *         Verbraeck </a>
51   * @version $$Revision: 1.2 $$ $$Date: 2005/04/08 11:50:53 $$
52   */
53  public abstract class SupplyChainHandler extends Handler
54  {
55  	/*** the owner of the handler */
56  	protected SupplyChainActor owner;
57  
58  	/*** the products for which this handler is valid */
59  	protected Set validProducts = new HashSet();
60  
61  	/*** the partner actors for which this handler is valid */
62  	protected Set validPartners = new HashSet();
63  
64  	/***
65  	 * @param owner the a that 'owns' the handler
66  	 */
67  	public SupplyChainHandler(final SupplyChainActor owner)
68  	{
69  		super(owner);
70  		this.owner = owner;
71  	}
72  
73  	/***
74  	 * Check whether the content is of the right type for this handler
75  	 * 
76  	 * @param content the content to check
77  	 * @return whether type is right or not
78  	 */
79  	protected abstract boolean checkContentClass(final Serializable content);
80  
81  	/***
82  	 * Method checkContent.
83  	 * 
84  	 * @param serContent the content to check
85  	 * @return returns a bill
86  	 */
87  	protected Content checkContent(final Serializable serContent)
88  	{
89  		if (!checkContentClass(serContent))
90  		{
91  			Logger.warning(this, "checkContent",
92  					"Wrong content type for actor " + super.owner
93  							+ ", handler " + this.getClass() + ": "
94  							+ serContent.getClass());
95  			return null;
96  		}
97  		Content content = (Content) serContent;
98  		if (content.getReceiver() != super.owner)
99  		{
100 			Logger.warning(this, "checkContent", "Bill for actor "
101 					+ content.getSender() + " sent to actor " + super.owner);
102 			return null;
103 		}
104 		return content;
105 	}
106 
107 	/***
108 	 * Add a valid product to the list of products to handle with this handler.
109 	 * 
110 	 * @param product a new valid product to use
111 	 */
112 	public void addValidProduct(final Product product)
113 	{
114 		this.validProducts.add(product);
115 	}
116 
117 	/***
118 	 * @return Returns the valid products.
119 	 */
120 	public Set getValidProducts()
121 	{
122 		return this.validProducts;
123 	}
124 
125 	/***
126 	 * Replace the current set of valid products. If you want to ADD a set, use
127 	 * addValidProduct per product instead.
128 	 * 
129 	 * @param validProducts A new set of valid products
130 	 */
131 	public void setValidProducts(final Set validProducts)
132 	{
133 		this.validProducts = validProducts;
134 	}
135 
136 	/***
137 	 * Check whether the product is of the right type for this handler
138 	 * 
139 	 * @param content the content to check
140 	 * @return whether type is right or not
141 	 */
142 	private boolean checkValidProduct(final Content content)
143 	{
144 		if (this.validProducts == null)
145 		{
146 			return true;
147 		}
148 		if (this.validProducts.size() == 0)
149 		{
150 			return true;
151 		}
152 		if (content instanceof InternalDemand)
153 		{
154 			return (this.validProducts.contains(((InternalDemand) (content))
155 					.getProduct()));
156 		}
157 		Serializable id = content.getInternalDemandID();
158 		// get the internal demand to retrieve the product
159 		List storedIDs = this.owner.getContentStore().getContentList(id,
160 				InternalDemand.class);
161 		if (storedIDs.size() == 0)
162 		{
163 			return false;
164 		}
165 		InternalDemand internalDemand = (InternalDemand) storedIDs.get(0);
166 		return (this.validProducts.contains(internalDemand.getProduct()));
167 	}
168 
169 	/***
170 	 * Add a valid partner to the list of supply chain partners to handle with
171 	 * this handler.
172 	 * 
173 	 * @param partner a new valid partner to use
174 	 */
175 	public void addValidPartner(final SupplyChainActor partner)
176 	{
177 		this.validPartners.add(partner);
178 	}
179 
180 	/***
181 	 * @return Returns the valid partners.
182 	 */
183 	public Set getValidPartners()
184 	{
185 		return this.validPartners;
186 	}
187 
188 	/***
189 	 * Replace the current set of valid partners. If you want to ADD a set, use
190 	 * addValidPartner per partner instead.
191 	 * 
192 	 * @param validPartners A new set of valid partners.
193 	 */
194 	public void setValidPartners(final Set validPartners)
195 	{
196 		this.validPartners = validPartners;
197 	}
198 
199 	/***
200 	 * Check whether the partner actor is one that this handler can handle
201 	 * 
202 	 * @param content the content to check
203 	 * @return whether partner is right or not
204 	 */
205 	private boolean checkValidPartner(final Content content)
206 	{
207 		if (this.validPartners == null)
208 		{
209 			return true;
210 		}
211 		if (this.validPartners.size() == 0)
212 		{
213 			return true;
214 		}
215 		return (this.validPartners.contains(content.getSender()));
216 	}
217 
218 	/***
219 	 * Check partner and content for validity for this handler.
220 	 * 
221 	 * @param content the content to check
222 	 * @return boolean indicating whether the content can be handled by this
223 	 *         handler
224 	 */
225 	protected boolean isValidContent(final Content content)
226 	{
227 		if (content == null)
228 		{
229 			Logger.warning(this, "isValidContent", "Content = null");
230 			return false;
231 		}
232 		return (checkValidProduct(content) & checkValidPartner(content));
233 	}
234 }