001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.core.collectors;
020
021import java.util.ArrayList;
022
023import org.nuxeo.ecm.automation.OperationContext;
024import org.nuxeo.ecm.automation.OperationException;
025import org.nuxeo.ecm.automation.OutputCollector;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.DocumentModelList;
028
029/**
030 * This implementation collect {@link DocumentModel} objects and return them as a {@link DocumentModelList} object.
031 * <p>
032 * You may use this to automatically iterate over iterable inputs in operation methods that <b>return</b> a
033 * {@link DocumentModel} object
034 *
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class DocumentModelCollector extends ArrayList<DocumentModel> implements DocumentModelList,
038        OutputCollector<DocumentModel, DocumentModelList> {
039
040    private static final long serialVersionUID = 5732663048354570870L;
041
042    @Override
043    public long totalSize() {
044        return size();
045    }
046
047    @Override
048    public void collect(OperationContext ctx, DocumentModel obj) throws OperationException {
049        if (obj != null) {
050            add(obj);
051        }
052    }
053
054    @Override
055    public DocumentModelList getOutput() {
056        return this;
057    }
058}