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