001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     */
017    
018    package org.otfeed.support;
019    
020    import org.otfeed.event.ICompletionDelegate;
021    import org.otfeed.event.IDataDelegate;
022    import org.otfeed.event.OTError;
023    
024    /**
025     * Common delegate implements all delegate intefaces
026     * of the org.otfeed API. Hence, it can be passed
027     * as <code>xxxDelegate</code> paramater to any command
028     * object.
029     * <p/>
030     * This object delegates actual output of the data
031     * to the {@link #getDataWriter() dataWriter} object, that defaults
032     * to {@link SimpleDataWriter}.
033     * <p/>
034     * Arbitrary id string can be (optionally) associated with
035     * this object, allowing to use multiple instancies of
036     * this object with a single {@link #getDataWriter() dataWriter}. 
037     * In this case id strings help to identify the source
038     * of the record in the <code>dataWriter</code>.
039     *
040     * @param <T> delegate type (the type of event object).
041     */
042    public class CommonDelegate<T> 
043                    implements IDataDelegate<T>, ICompletionDelegate {
044    
045            /**
046             * Creates new CommonDelegate, with no <code>id</code>,
047             * and <code>null</code> dataWriter.
048             *
049             */
050            public CommonDelegate() { }
051            
052            /**
053             * Creates new CommonDelegate with the given 
054             * <code>id</code> string.
055             * 
056             * @param id arbitrary id string. Identifies
057             * this listener. 
058             */
059            public CommonDelegate(String id) {
060                    setId(id);
061            }
062            
063            /**
064             * Creates new CommonDelegate with the given id string, and
065             * given output destination.
066             * 
067             * @param id id string.
068             * @param writer output destination.
069             */
070            public CommonDelegate(String id, IDataWriter writer) {
071                    setId(id);
072                    setDataWriter(writer);
073            }
074    
075            /**
076             * Creates new CommonDelegate with no id, and given 
077             * output destination.
078             * 
079             * @param writer destination.
080             */
081            public CommonDelegate(IDataWriter writer) {
082                    setDataWriter(writer);
083            }
084    
085            private String id = null;
086            
087            /**
088             * Arbitrary string, identifying this listener.
089             * If set, it will be passed to the output writer.
090             * This allows to use a single output writer for multiple
091             * listener instencies, and still be able to distinguish
092             * which listener produced a particular record.
093             *
094             * @return id string.
095             */
096            public String getId() {
097                    return id;
098            }
099            
100            /**
101             * Sets id string.
102             * 
103             * @param val id string.
104             */
105            public void setId(String val) {
106                    id = val;
107            }
108            
109            private IDataWriter writer = new SimpleDataWriter();
110            
111            /**
112             * Ouptut destination.
113             * 
114             * @return output writer.
115             */
116            public IDataWriter getDataWriter() { 
117                    return writer; 
118            }
119            
120            /**
121             * Sets output writer.
122             * 
123             * @param val writer.
124             */
125            public void setDataWriter(IDataWriter val) {
126                    writer = val;
127            }
128            
129            public void onData(T data) {
130                    writer.writeData(id, data);
131            }
132    
133            public void onDataEnd(OTError error) {
134                    if(error != null) {
135                            System.err.println("ERROR: " + error);
136                    }
137                    writer.close();
138            }
139    }