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: */
018:
019: /* $Id: DocumentSet.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.cms.publication.util;
022:
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.List;
026:
027: import org.apache.lenya.cms.publication.Document;
028: import org.apache.lenya.cms.publication.PublicationException;
029:
030: /**
031: * An ordered set of documents without duplicates.
032: */
033: public class DocumentSet {
034:
035: /**
036: * Ctor.
037: */
038: public DocumentSet() {
039: // do nothing
040: }
041:
042: /**
043: * Ctor.
044: *
045: * @param _documents The initial documents.
046: */
047: public DocumentSet(Document[] _documents) {
048: for (int i = 0; i < _documents.length; i++) {
049: add(_documents[i]);
050: }
051: }
052:
053: private List documents = new ArrayList();
054:
055: /**
056: * Returns the list object that stores the documents.
057: *
058: * @return A list.
059: */
060: protected List getList() {
061: return this .documents;
062: }
063:
064: /**
065: * Returns the documents contained in this set.
066: *
067: * @return An array of documents.
068: */
069: public Document[] getDocuments() {
070: return (Document[]) this .documents
071: .toArray(new Document[this .documents.size()]);
072: }
073:
074: /**
075: * Adds a document to this set.
076: *
077: * @param document The document to add.
078: * @throws IllegalArgumentException if the document is <code>null</code> or already contained.
079: */
080: public void add(Document document) {
081: if (document == null) {
082: throw new IllegalArgumentException("The document is null!");
083: }
084: if (this .documents.contains(document)) {
085: throw new IllegalArgumentException("The document ["
086: + document + "] is already contained!");
087: }
088: this .documents.add(document);
089: }
090:
091: /**
092: * Adds a document set to this set.
093: *
094: * @param set The documents to add.
095: */
096: public void addAll(DocumentSet set) {
097: assert set != null;
098: Document[] documents = set.getDocuments();
099: for (int i = 0; i < documents.length; i++) {
100: if (!contains(documents[i])) {
101: add(documents[i]);
102: }
103: }
104: }
105:
106: /**
107: * @param document The document.
108: * @return if the document is contained.
109: */
110: public boolean contains(Document document) {
111: return getList().contains(document);
112: }
113:
114: /**
115: * Checks if this set is empty.
116: *
117: * @return A boolean value.
118: */
119: public boolean isEmpty() {
120: return getList().isEmpty();
121: }
122:
123: /**
124: * Visits the set.
125: *
126: * @param visitor The visitor.
127: * @throws PublicationException if an error occurs during visiting.
128: */
129: public void visit(DocumentVisitor visitor)
130: throws PublicationException {
131: Document[] resources = getDocuments();
132: for (int i = 0; i < resources.length; i++) {
133: resources[i].accept(visitor);
134: }
135: }
136:
137: /**
138: * Removes a document.
139: *
140: * @param resource The document.
141: * @throws PublicationException if an error occurs.
142: */
143: public void remove(Document resource) throws PublicationException {
144: if (resource == null) {
145: throw new IllegalArgumentException("The resource is null!");
146: }
147: if (!getList().contains(resource)) {
148: throw new IllegalArgumentException("The resource ["
149: + resource + "] is not contained!");
150: }
151: getList().remove(resource);
152: }
153:
154: /**
155: * Removes all documents in a set from this set.
156: * @param set The set.
157: * @throws PublicationException if an error occurs.
158: */
159: public void removeAll(DocumentSet set) throws PublicationException {
160: Document[] documents = set.getDocuments();
161: for (int i = 0; i < documents.length; i++) {
162: remove(documents[i]);
163: }
164: }
165:
166: /**
167: * Removes all documents.
168: */
169: public void clear() {
170: getList().clear();
171: }
172:
173: /**
174: * Reverses the document order.
175: */
176: public void reverse() {
177: Collections.reverse(getList());
178: }
179:
180: }
|