KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > archive > collection > Collection


1 /*
2  * Created on Jul 1, 2006
3  */

4 package com.openedit.archive.collection;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 import com.openedit.store.Product;
11
12 public class Collection
13 {
14     protected String JavaDoc fieldId;
15     protected String JavaDoc fieldName;
16     protected List JavaDoc fieldCollectionItems;
17     
18     public Collection()
19     {
20         
21     }
22     
23     public String JavaDoc getId()
24     {
25         return fieldId;
26     }
27     public void setId(String JavaDoc inId)
28     {
29         fieldId = inId;
30     }
31     public String JavaDoc getName()
32     {
33         return fieldName;
34     }
35     public void setName(String JavaDoc inName)
36     {
37         fieldName = inName;
38     }
39     public boolean isEmpty()
40     {
41         return fieldCollectionItems == null || fieldCollectionItems.size() == 0;
42     }
43     public boolean hasItems()
44     {
45         return fieldCollectionItems != null && fieldCollectionItems.size() > 0;
46     }
47     
48     public List JavaDoc getCollectionItems()
49     {
50         if (fieldCollectionItems == null)
51         {
52             fieldCollectionItems = new ArrayList JavaDoc();
53             
54         }
55
56         return fieldCollectionItems;
57     }
58     public void setCollectionItems(List JavaDoc inCollectionItems)
59     {
60         fieldCollectionItems = inCollectionItems;
61     }
62     public void addItem(CollectionItem inItem)
63     {
64         getCollectionItems().add(inItem);
65     }
66     public boolean containsProduct(String JavaDoc inId)
67     {
68         if( fieldCollectionItems == null)
69         {
70             return false;
71         }
72         for (Iterator JavaDoc iter = getCollectionItems().iterator(); iter.hasNext();)
73         {
74             CollectionItem item = (CollectionItem) iter.next();
75             Product prod = item.getProduct();
76             
77             if( prod != null && prod.getId().equals(inId))
78             {
79                 return true;
80             }
81         }
82         return false;
83     }
84     public boolean containsProduct(Product inProduct)
85     {
86         return containsProduct(inProduct.getId());
87     }
88     public void removeItem(String JavaDoc inProductid)
89     {
90         CollectionItem found = null;
91         for (Iterator JavaDoc iter = getCollectionItems().iterator(); iter.hasNext();)
92         {
93             CollectionItem item = (CollectionItem) iter.next();
94             if( item.getProduct() == null || item.getProduct().getId().equals(inProductid))
95             {
96                 found = item;
97                 break;
98             }
99         }
100         if( found != null)
101         {
102             removeItem(found);
103         }
104     }
105     public void removeItem(CollectionItem inFound)
106     {
107         getCollectionItems().remove(inFound);
108     }
109     public void clear()
110     {
111         getCollectionItems().clear();
112     }
113 }
114
Popular Tags