001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.collectors;
013
014import java.util.ArrayList;
015
016import org.nuxeo.ecm.automation.OperationContext;
017import org.nuxeo.ecm.automation.OperationException;
018import org.nuxeo.ecm.automation.OutputCollector;
019import org.nuxeo.ecm.core.api.DocumentRef;
020import org.nuxeo.ecm.core.api.DocumentRefList;
021
022/**
023 * This implementation collect {@link DocumentRef} objects and return them as a {@link DocumentRefList} object.
024 * <p>
025 * You may use this to automatically iterate over iterable inputs in operation methods that <b>return</b> a
026 * {@link DocumentRef} object.
027 *
028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
029 */
030public class DocumentRefCollector extends ArrayList<DocumentRef> implements DocumentRefList,
031        OutputCollector<DocumentRef, DocumentRefList> {
032
033    private static final long serialVersionUID = 5732663048354570870L;
034
035    @Override
036    public long totalSize() {
037        return size();
038    }
039
040    @Override
041    public void collect(OperationContext ctx, DocumentRef ref) throws OperationException {
042        if (ref != null) {
043            add(ref);
044        }
045    }
046
047    @Override
048    public DocumentRefList getOutput() {
049        return this;
050    }
051}