1 package net.sf.invicta.project; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import java.util.Map ; 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 18 public class ProductContainerImpl implements ProductContainer { 19 20 public interface ProductSelector { 21 public boolean select(Product product); 22 }; 23 24 protected Map products = new SequencedHashMap(); 25 26 29 public ProductContainerImpl() { 30 super(); 31 } 32 33 37 public void insert(Product product) { 38 products.put(product.getKey(), product); 39 } 40 41 46 public void removeByProduct(Product product) { 47 products.remove(product.getKey()); 48 } 49 50 54 public void removeByKey(String key) { 55 products.remove(key); 56 } 57 58 62 public List findAll() { 63 return (List )new ArrayList (products.values()); 64 } 65 66 67 73 public ProductContainer findByName(final String 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 89 public ProductContainer findByType(final String type) { 90 return finder 91 (new ProductSelector() { 92 public boolean select(Product product) { 93 return type.equals(product.getType()); 94 } 95 }); 96 } 97 98 105 public ProductContainer findByTypeAndName(final String name, final String 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 120 public Product findByKey(String key) { 121 return (Product)products.get(key); 122 } 123 124 128 public int size() { 129 return this.products.size(); 130 } 131 132 136 public void insertAll(ProductContainer productContainer) { 137 this.products.putAll(((ProductContainerImpl)productContainer).products); 138 } 139 140 145 public void removeAll(ProductContainer productContainer) { 146 for (Iterator iter = productContainer.iterator(); iter.hasNext();) { 148 Product product = (Product) iter.next(); 149 this.removeByProduct(product); 150 } 151 } 152 153 158 public boolean containsProduct(Product product) { 159 return this.products.containsKey(product.getKey()); 160 } 161 162 167 public boolean containsKey(String key) { 168 return this.products.containsKey(key); 169 } 170 171 176 public boolean containsName(String name) { 177 return findByName(name).size() > 0; 178 } 179 180 185 public Iterator iterator() { 186 return this.products.values().iterator(); 187 } 188 189 194 private ProductContainer finder(ProductSelector selector) { 195 ProductContainer productContainer = new ProductContainerImpl(); 196 for (Iterator 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 |