View Javadoc

1   /*
2    * Copyright (C) 2004-2006 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, visit
19   * http://www.gnu.org/licenses/gpl.html or write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   */
22  package de.fu_berlin.ties;
23  
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  /***
29   * Abstract base clase for a {@link de.fu_berlin.ties.Processor} that collects
30   * all the input arguments and processes the collected arguments when shutting
31   * down. The {@link #close(int)} method delegates to the abstract
32   * {@link #process(List, ContextMap)} method to process the collected arguments.
33   *
34   * <p>Instances of this class are not thread-safe and must be synchronized
35   * externally, if required.
36   *
37   * @author Christian Siefkes
38   * @version $Revision: 1.9 $, $Date: 2006/10/21 16:03:52 $, $Author: siefkes $
39   */
40  public abstract class CollectingProcessor
41      extends ConfigurableProcessor
42      implements Closeable {
43  
44      /***
45       * Used to collect the input arguments.
46       */
47      private final List<String> collectedArgs = new ArrayList<String>();
48  
49      /***
50       * Creates a new instance.
51       *
52       * @param conf used to configure this instance; if <code>null</code>,
53       * the {@linkplain TiesConfiguration#CONF standard configuration} is used
54       */
55      public CollectingProcessor(final TiesConfiguration conf) {
56          super(conf);
57      }
58  
59      /***
60       * {@inheritDoc}
61       */
62      public final void close(final int errorCount)
63              throws IOException, ProcessingException {
64          // delegate to abstract method
65          process(collectedArgs, new ContextMap());
66      }
67  
68      /***
69       * {@inheritDoc}
70       */
71      public final void process(final String input) {
72          // collect in list
73          collectedArgs.add(input);
74      }
75  
76      /***
77       * Processes the collected input arguments.
78       *
79       * @param collected a list of Strings containing the collected input
80       * arguments
81       * @param context a map of objects that are made available for processing;
82       * will be empty when called from the {@link #close(int)} method in this
83       * class
84       * @throws IOException if an I/O error occurs
85       * @throws ProcessingException if an error occurs during processing
86       */
87      public abstract void process(final List<String> collected,
88              final ContextMap context)
89              throws IOException, ProcessingException;
90  
91  }