Public Member Functions | |
void | init (StepManager pStepManager) |
Initialize a FirmsWithExtraSupply instance. | |
Iterator< Firm > | iterator () |
Returns an iterator over all firms in the sector that have a non empty stock in a random order. | |
Protected Member Functions | |
void | createNewFirmArray () |
Check all firms in the sector and add them to the firms array, if they have some extraSupply left. | |
Private Attributes | |
Firm[] | firms |
The array contain all firms that have a non empty extraSupply while createNewFirmArray is called. | |
int | nextSelectedFirm = NO_FIRM_SELECTED |
The index of the selected firm in the firms array. |
It delivers an iterator that iterate over the firms of the sector in a random order and sort out all firms that have an empty stock.
So it replaces the following code fragment:
random.shuffle(firmList); for (Firm firm : firmList) { if (firm.extraSupply() > 0.) { doStuff } }
with
for (Firm firm : firmsWithExtraSupply) { doStuff }
but when firms are added or increase there extraSupply, the firms array can be incomplete. The StepManager create an actual array before he calls the exchangeStep, so while the exchangeStep the firms array are valid.
Definition at line 1272 of file Sector.java.
void init | ( | StepManager | pStepManager | ) |
Initialize a FirmsWithExtraSupply instance.
It register a callback before the exchangeStep at the StepManager, the callback updates the firms array.
Definition at line 1302 of file Sector.java.
Referenced by Sector.init().
01302 { 01303 pStepManager.registerCallback(Foundation.EXCHANGE_STEP, 01304 StepManager.Timing.PRE, 01305 1, 01306 new StepManager.Callback() { 01307 @Override 01308 public Object getOwner() { 01309 return FirmsWithExtraSupply.this; 01310 } 01311 01312 @Override 01313 public void preStep() { 01314 createNewFirmArray(); 01315 } 01316 }); 01317 }
Iterator<Firm> iterator | ( | ) |
Returns an iterator over all firms in the sector that have a non empty stock in a random order.
Definition at line 1323 of file Sector.java.
01323 { 01324 lastFirmToObserveIndex = lastFirmWithExtraSupplyIndex; 01325 nextSelectedFirm = NO_FIRM_SELECTED; 01326 return FirmsWithExtraSupply.this; 01327 }
void createNewFirmArray | ( | ) | [protected] |
Check all firms in the sector and add them to the firms array, if they have some extraSupply left.
Definition at line 1388 of file Sector.java.
Referenced by Sector.FirmsWithExtraSupply.init().
01388 { 01389 final List<Firm> lFirmsInSector = getFirmList(); 01390 // the maximal length of the array for this period is known 01391 firms = new Firm[lFirmsInSector.size()]; 01392 lastFirmWithExtraSupplyIndex = -1; 01393 01394 for (final Firm lFirm : lFirmsInSector) { 01395 if (lFirm.getExtraSupply() > 0.d) { 01396 firms[++lastFirmWithExtraSupplyIndex] = lFirm; 01397 } 01398 } 01399 }
The array contain all firms that have a non empty extraSupply while createNewFirmArray is called.
The order of the firms changes while the iterator is used. The firm from firms[0] - firms[lastFirmToObersveIndex] are the firms that are left for iterating, the firms from firms[0] - firms[lastFirmWithExtraSupplyIndex] are firms that are used in the next iterator.
Firms that doesn't have extraSupply left, will be removed from the array.
Definition at line 1286 of file Sector.java.
Referenced by Sector.FirmsWithExtraSupply.createNewFirmArray().
int nextSelectedFirm = NO_FIRM_SELECTED [private] |
The index of the selected firm in the firms array.
The index is calculated in hasNext.
Definition at line 1295 of file Sector.java.
Referenced by Sector.FirmsWithExtraSupply.iterator().