KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > coplets > basket > ContentStore


1 /*
2  * Copyright 2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.coplets.basket;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23
24 /**
25  * This is a per user store that can contain any object.
26  * Make a subclass to add your specific functionality.
27  *
28  * @version CVS $Id: Basket.java 30941 2004-07-29 19:56:58Z vgritsenko $
29  */

30 public abstract class ContentStore implements Serializable JavaDoc {
31     
32     /** The ordered list of items */
33     protected final List JavaDoc items = new ArrayList JavaDoc();
34     
35     /** The id */
36     protected final String JavaDoc id;
37     
38     /**
39      * The constructor
40      */

41     public ContentStore(String JavaDoc id) {
42         this.id = id;
43     }
44     
45     /**
46      * @return Returns the id.
47      */

48     public String JavaDoc getId() {
49         return this.id;
50     }
51     
52     /**
53      * Get an item at the index
54      */

55     public Object JavaDoc getItem(int index) {
56         return this.items.get(index);
57     }
58     
59     /**
60      * Add an item
61      */

62     public void addItem(Object JavaDoc item) {
63         this.items.add(item);
64     }
65     
66     /**
67      * Get the iterator
68      */

69     public Iterator JavaDoc getIterator() {
70         return this.items.iterator();
71     }
72     
73     /**
74      * Remove an item
75      */

76     public void removeItem(Object JavaDoc item) {
77         this.items.remove(item);
78     }
79     
80     /**
81      * Number of items in the basket
82      */

83     public int size() {
84         return this.items.size();
85     }
86
87     /**
88      * Calculate the size of a basket
89      */

90     public int contentSize() {
91         int size = 0;
92         Iterator JavaDoc i = this.items.iterator();
93         while (i.hasNext()) {
94             Object JavaDoc item = i.next();
95             if ( item instanceof ContentItem ) {
96                 int v = ((ContentItem)item).size();
97                 if ( v != -1 ) {
98                     size += v;
99                 }
100             }
101         }
102         return size;
103     }
104     
105     /**
106      * Get an item with the id
107      */

108     public Object JavaDoc getItem(long id) {
109         Iterator JavaDoc i = this.items.iterator();
110         while (i.hasNext()) {
111             Object JavaDoc item = i.next();
112             if ( item instanceof AbstractItem ) {
113                 if (((AbstractItem)item).getId() == id ) {
114                     return item;
115                 }
116             }
117         }
118         return null;
119     }
120 }
121
122
Popular Tags