KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > project > ProductContainerImpl


1 package net.sf.invicta.project;
2  
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import net.sf.invicta.api.Product;
9 import net.sf.invicta.api.ProductContainer;
10
11 import org.apache.commons.collections.SequencedHashMap;
12
13
14 /**
15  *
16  *
17  */

18 public class ProductContainerImpl implements ProductContainer {
19     
20     public interface ProductSelector {
21         public boolean select(Product product);
22     };
23     
24     protected Map JavaDoc products = new SequencedHashMap();
25
26     /**
27      * Constructor for ProductContainer.
28      */

29     public ProductContainerImpl() {
30         super();
31     }
32     
33     /**
34      * Inserts a new product into the container.
35      * @param product Product to insert.
36      */

37     public void insert(Product product) {
38         products.put(product.getKey(), product);
39     }
40     
41     /**
42      * Removes a product from the container that equals to the given
43      * product.
44      * @param product Product to remove.
45      */

46     public void removeByProduct(Product product) {
47         products.remove(product.getKey());
48     }
49
50     /**
51      * Removes a product with the given key from the container.
52      * @param key of the product to remove.
53      */

54     public void removeByKey(String JavaDoc key) {
55         products.remove(key);
56     }
57     
58     /**
59      * Returns all products of this container as a list.
60      * @return List of Product objects.
61      */

62     public List JavaDoc findAll() {
63         return (List JavaDoc)new ArrayList JavaDoc(products.values());
64     }
65     
66         
67     /**
68      * Returns a product container with all products of this container that
69      * have the given name.
70      * @param name The name of the products to find.
71      * @return ProductContainer
72      */

73     public ProductContainer findByName(final String JavaDoc name) {
74         return finder
75             (new ProductSelector() {
76                     public boolean select(Product product) {
77                         return name.equals(product.getName());
78                     }
79             });
80                             
81     }
82
83     /**
84      * Returns a product container with all products of this container that
85      * are of the given type.
86      * @param type The type of the products to find.
87      * @return ProductContainer
88      */

89     public ProductContainer findByType(final String JavaDoc type) {
90         return finder
91                 (new ProductSelector() {
92                         public boolean select(Product product) {
93                             return type.equals(product.getType());
94                         }
95                 });
96     }
97
98     /**
99      * Returns a product container with all products of this container that
100      * are of the given type and name.
101      * @param name The name of the products to find.
102      * @param type The type of the products to find.
103      * @return ProductContainer
104      */

105     public ProductContainer findByTypeAndName(final String JavaDoc name, final String JavaDoc type) {
106         return finder
107                         (new ProductSelector() {
108                                 public boolean select(Product product) {
109                                     return (type.equals(product.getType()) && name.equals(product.getName()));
110                                 }
111                         });
112                                 
113     }
114
115     /**
116      * Returns a product of this container that has the given key.
117      * @param key The key of the product to find.
118      * @return Product
119      */

120     public Product findByKey(String JavaDoc key) {
121         return (Product)products.get(key);
122     }
123
124     /**
125      * Returns the number of products in this container.
126      * @return int. Number of products.
127      */

128     public int size() {
129         return this.products.size();
130     }
131
132     /**
133      * Inserts all products of the given container into this container.
134      * @param productContainer container of products to insert.
135      */

136     public void insertAll(ProductContainer productContainer) {
137         this.products.putAll(((ProductContainerImpl)productContainer).products);
138     }
139     
140     /**
141      * Remove all products of the given container that exist in this
142      * container.
143      * @param productContainer container of products to remove.
144      */

145     public void removeAll(ProductContainer productContainer) {
146         // Remove the products of the given container from this
147
for (Iterator JavaDoc iter = productContainer.iterator(); iter.hasNext();) {
148             Product product = (Product) iter.next();
149             this.removeByProduct(product);
150         }
151     }
152
153     /**
154      * Returns whether the given product exists in this container.
155      * @param product
156      * @return boolean
157      */

158     public boolean containsProduct(Product product) {
159         return this.products.containsKey(product.getKey());
160     }
161         
162     /**
163      * Returns whether a product with the given key exists in this container.
164      * @param key
165      * @return boolean
166      */

167     public boolean containsKey(String JavaDoc key) {
168         return this.products.containsKey(key);
169     }
170     
171     /**
172      * Returns whether a product with the given name exists in this container.
173      * @param name
174      * @return boolean
175      */

176     public boolean containsName(String JavaDoc name) {
177         return findByName(name).size() > 0;
178     }
179
180     /**
181      * Returns an iterator to the beginning of the list of products of this
182      * container.
183      * @return Iterator
184      */

185     public Iterator JavaDoc iterator() {
186         return this.products.values().iterator();
187     }
188
189     /**
190      *
191      * @param selector
192      * @return ProductContainer
193      */

194     private ProductContainer finder(ProductSelector selector) {
195         ProductContainer productContainer = new ProductContainerImpl();
196         for (Iterator JavaDoc iter = this.products.values().iterator(); iter.hasNext();) {
197             Product product = (Product) iter.next();
198             if (selector.select(product))
199                 productContainer.insert(product);
200         }
201         
202         return productContainer;
203     }
204 }
205
Popular Tags