View Javadoc

1   /*
2    * $Id: MergingParameterAdapter.java,v 1.2 2004/11/27 13:20:02 pah Exp $
3    * 
4    * Created on 23-Sep-2004 by Paul Harrison (pah@jb.man.ac.uk)
5    * Copyright 2004 AstroGrid. All rights reserved.
6    *
7    * This software is published under the terms of the AstroGrid 
8    * Software License version 1.2, a copy of which has been included 
9    * with this distribution in the LICENSE.txt file.  
10   *
11   */
12  
13  package org.astrogrid.applications.commandline;
14  
15  import java.util.ArrayList;
16  import java.util.Iterator;
17  import java.util.List;
18  
19  import org.astrogrid.applications.CeaException;
20  import org.astrogrid.applications.beans.v1.parameters.ParameterValue;
21  import org.astrogrid.applications.description.ApplicationInterface;
22  import org.astrogrid.applications.parameter.protocol.ExternalValue;
23  
24  /***
25   * Special parameter adapter that will gather all the inputs into a single list.
26   * The container that the results are collected in is passed in as a @link MergingParameterAdaptor.Concentrator. The same Concentrator should be shared by a set of MergingParameterAdaptors that wish to merge their inputs. 
27   * 
28   *
29   * @author Paul Harrison (pah@jb.man.ac.uk) 23-Sep-2004
30   * @version $Name:  $
31   * @since iteration6
32   */
33  public class MergingParameterAdapter extends DefaultCommandLineParameterAdapter {
34  
35     /***
36      * @param appInterface
37      * @param pval
38      * @param desc
39      * @param indirect
40      * @param env
41      * @param concentrator
42      *           A single concentrator object should be shared between instances
43      *           of the
44      * @link MergingParameterAdapter that wish to merge their output.
45      */
46     public MergingParameterAdapter(ApplicationInterface appInterface,
47           ParameterValue pval, CommandLineParameterDescription desc,
48           ExternalValue indirect, CommandLineApplicationEnvironment env,
49           Concentrator concentrator) {
50        super(appInterface, pval, desc, indirect, env);
51        this.concentrator = concentrator;
52     }
53  
54     public static class Concentrator {
55  
56        private List gatheredValues = new ArrayList();
57  
58        private boolean haveListedSwitches = false;
59  
60        private final String separator;
61  
62        /***
63         *  
64         */
65        public Concentrator(String theSeparator) {
66  
67           separator = theSeparator;
68        }
69  
70        public boolean haveListedSwitches() {
71           return haveListedSwitches;
72        }
73  
74        public String getSeparator() {
75           return separator;
76        }
77  
78        public void setHaveListedSwitches(boolean haveListedSwitches) {
79           this.haveListedSwitches = haveListedSwitches;
80        }
81  
82        /***
83         * @return
84         */
85        public Iterator getIterator() {
86           return gatheredValues.iterator();
87        }
88  
89        /***
90         * @param o
91         */
92        public void add(Object o) {
93           gatheredValues.add(o);
94        }
95     }
96  
97     /***
98      * A single concentrator object should be shared between instances of the
99      * 
100     * @link MergingParameterAdapter that wish to merge their output.
101     */
102    private Concentrator concentrator;
103 
104    /*
105     * (non-Javadoc)
106     * 
107     * @see org.astrogrid.applications.parameter.CommandLineParameterAdapter#addSwitches()
108     */
109    public List addSwitches() throws CeaException {
110       if (concentrator.haveListedSwitches()) {
111          return null;
112       }
113       else {
114          //append all the gathered strings
115          StringBuffer output = new StringBuffer();
116          for (Iterator iter = concentrator.getIterator(); iter.hasNext();) {
117             String val = (String)iter.next();
118             output.append(val);
119             if (iter.hasNext()) {
120                output.append(concentrator.getSeparator());
121 
122             }
123          }
124          concentrator.setHaveListedSwitches(true);
125          return cmdParamDesc.addCmdlineAdornment(output.toString());
126       }
127    }
128 
129    /*
130     * (non-Javadoc)
131     * 
132     * @see org.astrogrid.applications.parameter.ParameterAdapter#process()
133     */
134    public Object process() throws CeaException {
135       Object o = super.process();
136       concentrator.add(o);
137       return o;
138    }
139 }