001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.coplets.basket;
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: /**
025: * This is a per user store that can contain any object.
026: * Make a subclass to add your specific functionality.
027: *
028: * @version CVS $Id: Basket.java 30941 2004-07-29 19:56:58Z vgritsenko $
029: */
030: public abstract class ContentStore implements Serializable {
031:
032: /** The ordered list of items */
033: protected final List items = new ArrayList();
034:
035: /** The id */
036: protected final String id;
037:
038: /**
039: * The constructor
040: */
041: public ContentStore(String id) {
042: this .id = id;
043: }
044:
045: /**
046: * @return Returns the id.
047: */
048: public String getId() {
049: return this .id;
050: }
051:
052: /**
053: * Get an item at the index
054: */
055: public Object getItem(int index) {
056: return this .items.get(index);
057: }
058:
059: /**
060: * Add an item
061: */
062: public void addItem(Object item) {
063: this .items.add(item);
064: }
065:
066: /**
067: * Get the iterator
068: */
069: public Iterator getIterator() {
070: return this .items.iterator();
071: }
072:
073: /**
074: * Remove an item
075: */
076: public void removeItem(Object item) {
077: this .items.remove(item);
078: }
079:
080: /**
081: * Number of items in the basket
082: */
083: public int size() {
084: return this .items.size();
085: }
086:
087: /**
088: * Calculate the size of a basket
089: */
090: public int contentSize() {
091: int size = 0;
092: Iterator i = this .items.iterator();
093: while (i.hasNext()) {
094: Object item = i.next();
095: if (item instanceof ContentItem) {
096: int v = ((ContentItem) item).size();
097: if (v != -1) {
098: size += v;
099: }
100: }
101: }
102: return size;
103: }
104:
105: /**
106: * Get an item with the id
107: */
108: public Object getItem(long id) {
109: Iterator i = this .items.iterator();
110: while (i.hasNext()) {
111: Object 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: }
|