001    package market;
002    
003    import data.Catalog;
004    import data.CatalogItem;
005    import data.filters.CatalogFilter;
006    
007    /**
008     * Filters CatalogItems by their category.
009     */
010    public class CFilter extends CatalogFilter {
011    
012        /**
013             * ID for serialization.
014             */
015            private static final long serialVersionUID = 251641215362836429L;
016            
017    /*  not needed atm...
018     * 
019     *  private Catalog source;
020     */     
021        private int filter;
022    
023        /**
024         * @param source the source catalog.
025         * @param filter the filter to be applied:
026         * <ul><li>0: no CatalogItems are filtered</li>
027         *     <li>1: CatalogItems are filtered by their category</li></ul>
028         */
029        public CFilter(Catalog source, int filter) {
030            super(source);
031    //        this.source = source;
032            this.filter = filter;
033        }
034    
035        /**
036         * The actual filtering.
037         * @param ci the CatalogItem to be checked.
038         * @return <code>true</code> if CatalogItem passed the filter successfully, otherwise <code>false</code>.
039         */
040        public boolean match(CatalogItem ci) {
041            String category = ((CIArticle)ci).getCategory();
042            switch (filter) {
043                case 0: return true;
044                default: return category.equals(SMarket.getArticleCategories()[filter]);
045            }
046        }
047    }