001    package market;
002    
003    import market.event.OfferEventListener;
004    import data.DataBasket;
005    import data.DataBasketEntry;
006    import data.StockItem;
007    import data.ooimpl.CatalogImpl;
008    import data.ooimpl.StoringStockImpl;
009    
010    /**
011     * A StoringStock that can be used as a queue for tills and warehouse,
012     * uses {@link SICustomer}s as StockItems and handles offer-events on it.
013     */
014    public class SSListenable extends StoringStockImpl{
015    
016        /**
017         * Array with all SPCustomers used to fire events to.
018         */
019        private OfferEventListener[] oel = new OfferEventListener[0];
020    
021        /**
022         * @param sName the name of the new Stock.
023         * @param ciRef the Catalog that is being referenced by the Stock.
024         */
025        public SSListenable(String sName, CatalogImpl ciRef) {
026            super(sName, ciRef);
027        }
028    
029        /**
030         * Adds an OfferEventListener to the array of listeners.
031         *
032         * @param e the OfferEventListener that will be added.
033         */
034        public void addEventListener(OfferEventListener e){
035            int len = oel.length;
036            boolean exists = false;
037            for (int i = 0; i < len; i++) {
038                exists = exists || (oel[i] == e);
039            }
040            if (!exists) {
041                OfferEventListener[] temp = new OfferEventListener[len+1];
042                System.arraycopy(oel, 0, temp, 0, len);
043                temp[len] = e;
044                oel = temp;
045            }
046        }
047    
048        /**
049         * Removes an OfferEventListener from the array of listeners.
050         *
051         * @param e the OfferEventListener that will be removed.
052         */
053        public void removeEventListener(OfferEventListener e){
054            for (int i = 0; i < oel.length; i++) {
055                if (oel[i] == e) {
056                    OfferEventListener[] temp = new OfferEventListener[oel.length-1];
057                    if (i > 0) System.arraycopy(oel,0,temp,0,i);
058                    if (i < oel.length-1) System.arraycopy(oel,i+1,temp,i,oel.length-1-i);
059                    oel = temp;
060                    break;
061                }
062            }
063        }
064    
065        /**
066         * Fires an event to all listeners: this article is empty.
067         *
068         * @param articleKey the key of the unavaible article.
069         */
070        public void fireOfferIsEmpty(String articleKey) {
071            for (int i = 0; i < oel.length; i++) {
072                if (oel[i] != null) oel[i].offerEmpty(articleKey);
073            }
074        }
075    
076        /**
077         * Fires an event to all listeners: a delivery arrived at the market.
078         */
079        public void fireWakeUpOrders() {
080            for (int i = 0; i < oel.length; i++) {
081                if (oel[i] != null) oel[i].wakeUpOrders();
082            }
083        }
084    
085        /**
086         * Fires an event to all listeners: count this article.
087         *
088         * @param articleKey the key of the article to count.
089         * @param spw the SProcessWorker which has sended the request.
090         */
091        public void fireCountArticles(String articleKey, SProcessWorker spw){
092            for (int i = 0; i < oel.length; i++) {
093                if (oel[i] != null) oel[i].countArticles(articleKey, spw);
094            }
095        }
096    
097        /**
098         * Adds a StockItem to this Stock, and adds it to the array of listeners,
099         * if it`s a SICustomer.
100         *
101         * @param si the item to be added.
102         * @param db the DataBasket relative to which the item will be added.
103         */
104        public void add(StockItem si, DataBasket db) {
105            if(si instanceof SICustomer)addEventListener(((SICustomer)si));
106            super.add(si, db);
107        }
108    
109        /**
110        * Commit the removal of a StockItem, removes it from the array of listeners.
111        */
112        public void commitRemove(DataBasket db, DataBasketEntry dbe) {
113            SICustomer sic = (SICustomer) dbe.getValue();
114            removeEventListener(sic);
115            super.commitRemove(db, dbe);
116        }
117    }