Sector.FirmsWithInventory Class Reference

The FirmWithInventory class exist only because of optimization issues. More...

Collaboration diagram for Sector.FirmsWithInventory:

Collaboration graph
[legend]

List of all members.

Public Member Functions

void init (StepManager pStepManager)
 Initialize a FirmsWithInventory instance.
Iterator< Firmiterator ()
 Returns an iterator over all firms in the sector that have a non empty stock in a random order.
boolean hasNext ()
 Part of the Sector.FirmsWithInventory.iterator.
Firm next ()
 Part of the Sector.FirmsWithInventory.iterator.
void remove ()
 Part of the Sector.FirmsWithInventory.iterator.

Protected Member Functions

void createNewFirmArray ()
 Check all firms in the sector and add them to the firms array, if they have some inventory left.

Private Attributes

Firm[] firms
 The array contain all firms that have a non empty inventory while createNewFirmArray is called.
int lastFirmToObserveIndex
 
See also:
Sector.FirmsWithInventory.firms

int lastFirmWithInventoryIndex
 
See also:
Sector.FirmsWithInventory.firms

int nextSelectedFirm = NO_FIRM_SELECTED
 The index of the selected firm in the firms array.


Detailed Description

The FirmWithInventory class exist only because of optimization issues.

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.inventory() > 0.) { doStuff } }

with

for (Firm firm : firmsWithInventory) { doStuff }

but when firms are added or increase there inventory, 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 1112 of file Sector.java.


Member Function Documentation

void init ( StepManager  pStepManager  ) 

Initialize a FirmsWithInventory instance.

It register a callback before the exchangeStep at the StepManager, the callback updates the firms array.

Definition at line 1144 of file Sector.java.

Referenced by Sector.init().

01144                                                {
01145         pStepManager.registerCallback(Foundation.EXCHANGE_STEP,
01146                                          StepManager.Timing.PRE,
01147                                          1,
01148                                          new StepManager.Callback() {
01149         @Override
01150         public Object getOwner() {
01151           return FirmsWithInventory.this;
01152         }
01153 
01154         @Override
01155         public void preStep() {
01156           createNewFirmArray();
01157         }
01158       });
01159     }

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 1165 of file Sector.java.

01165                                      {
01166       lastFirmToObserveIndex = lastFirmWithInventoryIndex;
01167       nextSelectedFirm = NO_FIRM_SELECTED;
01168       return FirmsWithInventory.this;
01169     }

boolean hasNext (  ) 

Part of the Sector.FirmsWithInventory.iterator.

Definition at line 1172 of file Sector.java.

01172                              {
01173       if (nextSelectedFirm != NO_FIRM_SELECTED) {
01174         // a firm is already selected (hasNext was already called), so we know
01175         // that we have an additional firm.
01176         return true;
01177       }
01178 
01179       while (lastFirmToObserveIndex >= 0) {
01180         nextSelectedFirm = random.nextInt(lastFirmToObserveIndex + 1);
01181         // after a firms has selled all of his stock, he is still in the firms array
01182         // because there is no callback to the iterator. So we must check that
01183         // the selected firm has still something to sell
01184         if (firms[nextSelectedFirm].getInventory() == 0) {
01185           // we overwrite the firm without inventory with the last from the
01186           // "left to observe" part of the array ...
01187           firms[nextSelectedFirm] = firms[lastFirmToObserveIndex];
01188           // ... and overwrite this firm with the last from the "firms that have
01189           // still inventory (as far as we know it)" part of the array ...
01190           firms[lastFirmToObserveIndex] = firms[lastFirmWithInventoryIndex];
01191           // ... and decrement the counters (we have one firm less to observe ...
01192           lastFirmToObserveIndex--;
01193           // ... and one firm less with inventory)
01194           lastFirmWithInventoryIndex--;
01195         } else {
01196           // the randomly selected firm has inventory. So it moves to the
01197           // lastFirmToObserveIndex position of the firms array ...
01198           ArrayTools.swap(firms, nextSelectedFirm, lastFirmToObserveIndex);
01199           // ... we store the array position of the selected firm in nextSelectFirm
01200           // which is used in next() ...
01201           nextSelectedFirm = lastFirmToObserveIndex;
01202           // ... and decrement the lastFirmToObserveIndex so that the firm that
01203           // is selected now will not be selected in this iterator again.
01204           lastFirmToObserveIndex--;
01205           return true;
01206         }
01207       }
01208 
01209       // we have no firms left in our firms array, so we return false
01210       return false;
01211     }

Firm next (  ) 

Part of the Sector.FirmsWithInventory.iterator.

Definition at line 1214 of file Sector.java.

01214                        {
01215       if (nextSelectedFirm == NO_FIRM_SELECTED) {
01216         throw new NoSuchElementException();
01217       } else {
01218         final Firm lNextFirm = firms[nextSelectedFirm];
01219         nextSelectedFirm = NO_FIRM_SELECTED;
01220         return lNextFirm;
01221       }
01222     }

void remove (  ) 

Part of the Sector.FirmsWithInventory.iterator.

Definition at line 1225 of file Sector.java.

01225                          {
01226       throw new UnsupportedOperationException();
01227     }

void createNewFirmArray (  )  [protected]

Check all firms in the sector and add them to the firms array, if they have some inventory left.

Definition at line 1233 of file Sector.java.

Referenced by Sector.FirmsWithInventory.init().

01233                                         {
01234       final List<Firm> lFirmsInSector = getFirmList();
01235       // the maximal length of the array for this period is known
01236       firms = new Firm[lFirmsInSector.size()];
01237       lastFirmWithInventoryIndex = -1;
01238 
01239       for (final Firm lFirm : lFirmsInSector) {
01240         if (lFirm.getInventory() > 0.d) {
01241           firms[++lastFirmWithInventoryIndex] = lFirm;
01242         }
01243       }
01244     }


Member Data Documentation

Firm [] firms [private]

The array contain all firms that have a non empty inventory while createNewFirmArray is called.

The order of the firms changes while the iterator is used. The firm from firms[0] - firms[lastFirmToObserveIndex] are the firms that are left for iterating, the firms from firms[0] - firms[lastFirmWithInventoryIndex] are firms that are used in the next iterator.

Firms that doesn't have inventory left, will be removed from the array.

Definition at line 1126 of file Sector.java.

Referenced by Sector.FirmsWithInventory.createNewFirmArray(), Sector.FirmsWithInventory.hasNext(), and Sector.FirmsWithInventory.next().

int lastFirmToObserveIndex [private]

See also:
Sector.FirmsWithInventory.firms

Definition at line 1128 of file Sector.java.

Referenced by Sector.FirmsWithInventory.hasNext(), and Sector.FirmsWithInventory.iterator().

int lastFirmWithInventoryIndex [private]

See also:
Sector.FirmsWithInventory.firms

Definition at line 1130 of file Sector.java.

Referenced by Sector.FirmsWithInventory.createNewFirmArray(), Sector.FirmsWithInventory.hasNext(), and Sector.FirmsWithInventory.iterator().

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 1137 of file Sector.java.

Referenced by Sector.FirmsWithInventory.hasNext(), Sector.FirmsWithInventory.iterator(), and Sector.FirmsWithInventory.next().


The documentation for this class was generated from the following file:
Generated on Tue Sep 14 11:11:49 2010 for lagom_generiC by  doxygen 1.5.4