1 /***
2 * Copyright 2003, 2004, 2005. CodeStreet LLC.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15 package com.codestreet.selector;
16
17 import java.util.Map;
18
19 import com.codestreet.selector.parser.IExpression;
20 import com.codestreet.selector.parser.IValueProvider;
21 import com.codestreet.selector.parser.InvalidSelectorException;
22 import com.codestreet.selector.parser.Result;
23 import com.codestreet.selector.parser.SelectorParser;
24 import com.codestreet.selector.parser.SelectorParser.SelectorParseState;
25
26 /***
27 * Thread safe Selector implementation.
28 *
29 * @author Jawaid Hakim.
30 */
31 public class Selector implements ISelector
32 {
33 /***
34 * Factory.
35 *
36 * @param selector
37 * Selector.
38 * @return Selector instance.
39 * @throws NullPointerException
40 * @see #getInstance(String, boolean)
41 */
42 public static Selector getInstance(final String selector)
43 throws InvalidSelectorException
44 {
45 return getInstance(selector, false);
46 }
47
48 /***
49 * Factory.
50 *
51 * @param selector
52 * Selector.
53 * @param trace
54 * Parser outputs trace if <tt>true</tt> .
55 * @return Selector instance.
56 * @throws NullPointerException
57 * @throws com.codestreet.selector.parser.InvalidSelectorException
58 * @see #getInstance(String)
59 */
60 public static Selector getInstance(final String selector,
61 final boolean trace) throws InvalidSelectorException
62 {
63 if (selector == null)
64 throw new NullPointerException("NULL selector");
65
66 SelectorParseState exp = SelectorParser.doParse(selector, trace);
67 return new Selector(selector, exp.getRoot(), exp.getIdentifiers());
68 }
69
70 /***
71 * Ctor.
72 *
73 * @param selector
74 * Selector.
75 * @param root
76 * Root expression of the parsed selector.
77 * @param identifiers
78 * Identifiers used by the selector. The key into the
79 * <tt>Map</tt> is name of the identifier and the value is an
80 * instance of <tt>Identifier</tt>.
81 */
82 private Selector(final String selector, final IExpression root,
83 final Map identifiers)
84 {
85 selector_ = selector;
86 root_ = root;
87 identifiers_ = java.util.Collections.unmodifiableMap(identifiers);
88 }
89
90 /***
91 * Evaluate the selector.
92 *
93 * @param identifiers
94 * Value for each non-null identifier in the selector.
95 * @return Result of evaluating the selector.
96 * @see #getIdentifiers()
97 */
98 public Result eval(final Map identifiers)
99 {
100 return (Result) root_.eval(identifiers);
101 }
102
103 /***
104 * Evaluate the selector.
105 *
106 * @param provider
107 * Value provider. During evaluation of the selector callbacks
108 * are made on the value provider to get identifier values.
109 * @param corr
110 * Correlation data. Passed as-is to the value provider.
111 * @return Result of evaluating the selector.
112 */
113 public Result eval(final IValueProvider provider, final Object corr)
114 {
115 return (Result) root_.eval(provider, corr);
116 }
117
118 /***
119 * Get identifiers used by the selector. The key into the <tt>Map</tt> is
120 * the name of the identifier and the value is an instance of
121 * <tt>Identifier</tt>.
122 *
123 * @return Readonly <tt>Map</tt> of identifiers that are used within the
124 * selector.
125 * @throws UnsupportedOperationException
126 * @see #eval(Map)
127 */
128 public Map getIdentifiers()
129 {
130 return identifiers_;
131 }
132
133 /***
134 * Get the selector.
135 *
136 * @return Selector.
137 */
138 public String getSelector()
139 {
140 return selector_;
141 }
142
143 /***
144 * Get selector parse tree.
145 *
146 * @return Selector parse tree.
147 */
148 public String toString()
149 {
150 return selector_;
151 }
152
153 private final IExpression root_;
154
155 private final Map identifiers_;
156
157 private final String selector_;
158 }
This page was automatically generated by Maven