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.command;
019    
020    import java.util.Set;
021    
022    import org.otfeed.event.IDataDelegate;
023    import org.otfeed.event.OTSymbol;
024    import org.otfeed.protocol.ICommand;
025    
026    /**
027     * Retrieves list of symbols traded on an exchange.
028     * </p>
029     * Allows to get a full list, or search symbols by
030     * prefix or substring. 
031     * <p/>
032     * Generates {@link OTSymbol symbol} events.
033     */
034    public final class ListSymbolsCommand extends CompletionDelegateHolder implements ICommand {
035            
036            /**
037             * Type of string matching: prefix or contains.
038             */
039            public enum MatchStyleEnum {
040                    /** Prefix */
041                    PREFIX, 
042                    
043                    /** Contains */
044                    CONTAINS;
045            }
046    
047            /**
048             * Creates new list symbols command, initializing all
049             * its properties.
050             * 
051             * @param exchangeCode exchange code.
052             * @param symbolCodePattern symbol pattern.
053             * @param types set of types.
054             * @param matchStyle matching style (e.g. PREFIX or CONTAINS).
055             * @param dataDelegate delegate.
056             */
057            public ListSymbolsCommand(
058                            String exchangeCode,
059                            String symbolCodePattern,
060                            Set<ListSymbolEnum> types,
061                            MatchStyleEnum matchStyle,
062                            IDataDelegate<OTSymbol> dataDelegate) {
063                    setExchangeCode(exchangeCode);
064                    setSymbolCodePattern(symbolCodePattern);
065                    setTypes(types);
066                    setMatchStyle(matchStyle);
067                    setDataDelegate(dataDelegate);
068            }
069    
070            /**
071             * Default constructor. Initializes 
072             * {@link #getTypes types} property to the
073             * default value of {@link ListSymbolEnum#ALL ALL}.
074             * Initializes {@link #getSymbolCodePattern symbolCodePattern} property
075             * to the default value of blank (which means 
076             * "any symbol code"). 
077             * You must set other properties explicitly before using 
078             * this command object.
079             */
080            public ListSymbolsCommand() {
081                    this(null, "", ListSymbolEnum.ALL, MatchStyleEnum.PREFIX, null);
082            }
083    
084            /**
085             * Creates new list symbols command for a single
086             * symbol type.
087             * 
088             * @param exchangeCode exchange code.
089             * @param symbolCodePattern symbol pattern.
090             * @param type instrument type.
091             * @param matchStyle matching style (e.g. PREFIX or CONTAINS).
092             * @param dataDelegate data delegate.
093             */
094            public ListSymbolsCommand(
095                            String exchangeCode,
096                            String symbolCodePattern,
097                            ListSymbolEnum type, 
098                            MatchStyleEnum matchStyle,
099                            IDataDelegate<OTSymbol> dataDelegate) {
100                    this(exchangeCode, 
101                                    symbolCodePattern, 
102                                    ListSymbolEnum.combine(type),
103                                    matchStyle,
104                                    dataDelegate);
105            }
106    
107            /**
108             * Creates new list symbols command for a single
109             * symbol type, with wildcard value for the
110             * {@link #getSymbolCodePattern symbolCodePattern} property.
111             * 
112             * @param exchangeCode exchange code.
113             * @param type instrument type.
114             * @param matchStyle matching style (e.g. PREFIX, or CONTAINS).
115             * @param dataDelegate delegate.
116             */
117            public ListSymbolsCommand(
118                            String exchangeCode,
119                            ListSymbolEnum type,
120                            MatchStyleEnum matchStyle,
121                            IDataDelegate<OTSymbol> dataDelegate) {
122                    this(exchangeCode, 
123                                    "", 
124                                    ListSymbolEnum.combine(type),
125                                    matchStyle,
126                                    dataDelegate);
127            }
128    
129            /**
130             * Creates new list symbols command for all
131             * symbol types.
132             * 
133             * @param exchangeCode exchange code.
134             * @param symbolCodePattern symbol pattern.
135             * @param matchStyle matching style (e.g. PREFIX or CONTAINS).
136             * @param dataDelegate delegate.
137             */
138            public ListSymbolsCommand(
139                            String exchangeCode,
140                            String symbolCodePattern,
141                            MatchStyleEnum matchStyle,
142                            IDataDelegate<OTSymbol> dataDelegate) {
143                    this(exchangeCode, 
144                                    symbolCodePattern, 
145                                    ListSymbolEnum.ALL,
146                                    matchStyle,
147                                    dataDelegate);
148            }
149    
150            /**
151             * Creates new list symbols command for a given set of symbol
152             * types, exchangeCode, and symbolCodePattern.
153             * Match type is "prefix".
154             * 
155             * @param exchangeCode exchange code.
156             * @param symbolCodePattern symbol pattern.
157             * @param types set of types.
158             * @param dataDelegate delegate.
159             */
160            public ListSymbolsCommand(
161                            String exchangeCode,
162                            String symbolCodePattern,
163                            Set<ListSymbolEnum> types,
164                            IDataDelegate<OTSymbol> dataDelegate) {
165                    this(exchangeCode, symbolCodePattern, types, 
166                                    MatchStyleEnum.PREFIX, dataDelegate);
167            }
168    
169            /**
170             * Creates new list symbols command for a single
171             * symbol type.
172             * Match type is "prefix".
173             * 
174             * @param exchangeCode exchange code.
175             * @param symbolCodePattern symbol pattern.
176             * @param type instrument type.
177             * @param dataDelegate delegate.
178             */
179            public ListSymbolsCommand(
180                            String exchangeCode,
181                            String symbolCodePattern,
182                            ListSymbolEnum type, 
183                            IDataDelegate<OTSymbol> dataDelegate) {
184                    this(exchangeCode, 
185                                    symbolCodePattern, 
186                                    ListSymbolEnum.combine(type),
187                                    MatchStyleEnum.PREFIX,
188                                    dataDelegate);
189            }
190    
191            /**
192             * Creates new list symbols command for a single
193             * symbol type, with wildcard value for the
194             * {@link #getSymbolCodePattern symbolCodePattern} property.
195             * 
196             * @param exchangeCode exchange code.
197             * @param type instrument type.
198             * @param dataDelegate data delegate.
199             */
200            public ListSymbolsCommand(
201                            String exchangeCode,
202                            ListSymbolEnum type,
203                            IDataDelegate<OTSymbol> dataDelegate) {
204                    this(exchangeCode, 
205                                    "", 
206                                    ListSymbolEnum.combine(type),
207                                    MatchStyleEnum.PREFIX,
208                                    dataDelegate);
209            }
210    
211            /**
212             * Creates new list symbols command for all
213             * symbol types. 
214             * Match type is "prefix".
215             * 
216             * @param exchangeCode exchange code.
217             * @param symbolCodePattern cymbol code.
218             * @param dataDelegate data delegate.
219             */
220            public ListSymbolsCommand(
221                            String exchangeCode,
222                            String symbolCodePattern,
223                            IDataDelegate<OTSymbol> dataDelegate) {
224                    this(exchangeCode, 
225                                    symbolCodePattern, 
226                                    ListSymbolEnum.ALL,
227                                    MatchStyleEnum.PREFIX,
228                                    dataDelegate);
229            }
230    
231            /**
232             * Creates new list symbols command for all
233             * symbol types, with wildcard value for the
234             * {@link #getSymbolCodePattern symbolCodePattern} property.
235             * Match type is "prefix".
236             * 
237             * @param exchangeCode exchange code.
238             * @param dataDelegate data delegate.
239             */
240            public ListSymbolsCommand(
241                            String exchangeCode,
242                            IDataDelegate<OTSymbol> dataDelegate) {
243                    this(exchangeCode, 
244                                    "", 
245                                    ListSymbolEnum.ALL,
246                                    MatchStyleEnum.PREFIX,
247                                    dataDelegate);
248            }
249    
250            private String exchangeCode;
251    
252            /**
253             * Exchange code.
254             * 
255             * @return exchange code.
256             */
257            public String getExchangeCode() {
258                    return exchangeCode;
259            }
260    
261            /**
262             * Sets the exchange code.
263             * 
264             * @param exchangeCode exchange code.
265             */
266            public void setExchangeCode(String exchangeCode) {
267                    this.exchangeCode = exchangeCode;
268            }
269    
270            private String symbolCodePattern;
271    
272            /**
273             * Symbol code. Optional parameter. If not specified, defaults to
274             * an empty string, which will act as a wildcard (all symbols
275             * will be returned). It acts as a match prefix when searching 
276             * for symbols. For example, symbolCodePattern value of "GO" may
277             * return "GOOG", "GOK", and "GOAT".
278             * 
279             * @return symbol code.
280             */
281            public String getSymbolCodePattern() { 
282                    return symbolCodePattern; 
283            }
284    
285            /**
286             * Sets symbol code.
287             * 
288             * @param val symbol code.
289             */
290            public void setSymbolCodePattern(String val) {
291                    symbolCodePattern = val;
292            }
293    
294            private Set<ListSymbolEnum> types = ListSymbolEnum.ALL;
295    
296            /**
297             * Types of symbols to request. Optional property, 
298             * defaults to {@link ListSymbolEnum#ALL}
299             * 
300             * @return set of types.
301             */
302            public Set<ListSymbolEnum> getTypes() {
303                    return types;
304            }
305    
306            /**
307             * Sets types of symbols.
308             * 
309             * @param val set of symbol types.
310             */
311            public void setTypes(Set<ListSymbolEnum> val) {
312                    types = val;
313            }
314            
315            private MatchStyleEnum matchStyle;
316            
317            /**
318             * Determines how {@link #getSymbolCodePattern() symbolCodePattern}
319             * is treated. 
320             * 
321             * By default, <code>matchStyle</code> is <code>PREFIX</code>
322             * that means that <code>symbolCodePattern</code> is matched as a 
323             * prefix: all symbols that start with this string
324             * will be returned. If <code>matchStyle</code> is set to
325             * <code>CONTAINS</code>,
326             * all symbols that contain this string will be returned.
327             * 
328             * @return true, if containsMatch is set, false otherwise.
329             */
330            public MatchStyleEnum getMatchStyle() {
331                    return matchStyle;
332            }
333            
334            /**
335             * Sets match style.
336             * 
337             * @param val matchStyle value.
338             */
339            public void setMatchStyle(MatchStyleEnum val) {
340                    matchStyle = val;
341            }
342    
343            private IDataDelegate<OTSymbol> dataDelegate;
344    
345            /**
346             * Delegate to receive {@link OTSymbol} events.
347             * 
348             * @return delegate.
349             */
350            public IDataDelegate<OTSymbol> getDataDelegate() {
351                    return dataDelegate;
352            }
353    
354            /**
355             * Sets delegate.
356             * 
357             * @param dataDelegate delegate.
358             */
359            public void setDataDelegate(IDataDelegate<OTSymbol> dataDelegate) {
360                    this.dataDelegate = dataDelegate;
361            }
362    }