OpenSplice Java 5 DCPS  v6.x
OpenSplice Java 5 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
SelectorImpl.java
Go to the documentation of this file.
1 /*
2  * Vortex OpenSplice
3  *
4  * This software and documentation are Copyright 2006 to 2024 ADLINK
5  * Technology Limited, its affiliated companies and licensors. All rights
6  * reserved.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 package org.opensplice.dds.sub;
22 
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.List;
27 
30 import org.omg.dds.sub.DataReader;
32 import org.omg.dds.sub.ReadCondition;
33 import org.omg.dds.sub.Sample;
34 import org.omg.dds.sub.Sample.Iterator;
38 
39 public class SelectorImpl<TYPE> implements DataReader.Selector<TYPE> {
40  private final OsplServiceEnvironment environment;
41  private final AbstractDataReader<TYPE> reader;
42  private final int maxSamples;
43  private final InstanceHandle instance;
44  private final boolean retrieveNextInstance;
45  private final DataState dataState;
46  private final ReadConditionImpl<TYPE> condition;
47  private final String queryExpression;
48  private final List<String> queryParameters;
49 
51  AbstractDataReader<TYPE> reader) {
52  this.environment = environment;
53  this.reader = reader;
54  this.maxSamples = DDS.LENGTH_UNLIMITED.value;
55  this.condition = null;
56  this.dataState = DataStateImpl.any(environment);
57  this.queryExpression = null;
58  this.queryParameters = new ArrayList<String>();
59  this.instance = this.environment.getSPI().nilHandle();
60  this.retrieveNextInstance = false;
61  }
62 
63  @SuppressWarnings("unchecked")
64  private SelectorImpl(SelectorImpl<TYPE> s, DataState dataState,
65  String queryExpression, List<String> queryParameters,
66  int maxSamples, InstanceHandle instance,
67  boolean retrieveNextInstance) {
68  this.environment = s.environment;
69  this.reader = s.reader;
70  this.maxSamples = maxSamples;
71  this.instance = instance;
72  this.retrieveNextInstance = retrieveNextInstance;
73  this.dataState = dataState.clone();
74  this.queryExpression = queryExpression;
75 
76  if (queryParameters == null) {
77  throw new IllegalArgumentExceptionImpl(this.environment,
78  "Illegal List of query arguments (null) provided.");
79  }
80  this.queryParameters = new ArrayList<String>(queryParameters);
81 
82  if (this.queryExpression != null) {
83  this.condition = (ReadConditionImpl<TYPE>) this.reader
84  .createQueryCondition(this.dataState, this.queryExpression,
85  this.queryParameters);
86  } else {
87  this.condition = null;
88  }
89  }
90 
91  private SelectorImpl(SelectorImpl<TYPE> s, String queryExpression,
92  List<String> queryParameters) {
93  this(s, s.dataState, queryExpression, queryParameters, s.maxSamples,
94  s.instance, s.retrieveNextInstance);
95  }
96 
97  private SelectorImpl(SelectorImpl<TYPE> s, DataState dataState) {
98  this(s, dataState, s.queryExpression, s.queryParameters, s.maxSamples,
99  s.instance, s.retrieveNextInstance);
100  }
101 
102  private SelectorImpl(SelectorImpl<TYPE> s, int maxSamples) {
103  this(s, s.dataState, s.queryExpression, s.queryParameters, maxSamples,
104  s.instance, s.retrieveNextInstance);
105  }
106 
107  private SelectorImpl(SelectorImpl<TYPE> s, InstanceHandle instance) {
108  this(s, s.dataState, s.queryExpression, s.queryParameters,
109  s.maxSamples, instance, s.retrieveNextInstance);
110  }
111 
112  private SelectorImpl(SelectorImpl<TYPE> s, boolean retrieveNextInstance) {
113  this(s, s.dataState, s.queryExpression, s.queryParameters,
114  s.maxSamples, s.instance, retrieveNextInstance);
115  }
116 
117  @Override
119  return this.environment;
120  }
121 
122  @Override
124  if (handle == null) {
125  throw new IllegalArgumentExceptionImpl(this.environment,
126  "Illegal InstanceHandle (null) provided.");
127  }
128  if (!handle.equals(this.instance)) {
129  return new SelectorImpl<TYPE>(this, handle);
130  }
131  return this;
132  }
133 
134  @Override
135  public Selector<TYPE> nextInstance(boolean retrieveNextInstance) {
136  if (this.retrieveNextInstance != retrieveNextInstance) {
137  return new SelectorImpl<TYPE>(this, retrieveNextInstance);
138  }
139  return this;
140  }
141 
142  private void checkDataStateValidity(DataState state) {
143  if (state.getInstanceStates().size() == 0) {
144  throw new IllegalArgumentExceptionImpl(this.environment,
145  "Illegal instance state.");
146  }
147  if (state.getSampleStates().size() == 0) {
148  throw new IllegalArgumentExceptionImpl(this.environment,
149  "Illegal sample state.");
150  }
151  if (state.getViewStates().size() == 0) {
152  throw new IllegalArgumentExceptionImpl(this.environment,
153  "Illegal view state.");
154  }
155  }
156 
157  @Override
159  if (state == null) {
160  throw new IllegalArgumentExceptionImpl(environment,
161  "Illegal DataState (null) provided.");
162  }
163  checkDataStateValidity(state);
164 
165  if (!state.equals(this.dataState)) {
166  return new SelectorImpl<TYPE>(this, state);
167  }
168  return this;
169  }
170 
171  private boolean isQueryEqualToCurrent(String qe, List<String> qp) {
172  if (qe == null) {
173  if (qp != null) {
174  if (qp.size() > 0) {
175  throw new IllegalArgumentException(
176  "Query parameters for null query expression provided.");
177  }
178  }
179  if (this.queryExpression == null) {
180  if (qp == null) {
181  return false;
182  }
183  return true;
184  }
185  return false;
186  }
187 
188  if (qe.equals(this.queryExpression)) {
189  if (qp == null) {
190  if (this.queryParameters.size() == 0) {
191  return true;
192  }
193  return false;
194  } else if (qp.size() == 0 && this.queryParameters.size() == 0) {
195  return true;
196  } else if (qp.size() != this.queryParameters.size()) {
197  return false;
198  } else {
199  for (int i = 0; i < qp.size(); i++) {
200  if (!qp.get(i).equals(this.queryParameters.get(i))) {
201  return false;
202  }
203  }
204  return true;
205  }
206  }
207  return false;
208  }
209 
210  @Override
211  public Selector<TYPE> Content(String queryExpression,
212  List<String> queryParameters) {
213 
214  if (!isQueryEqualToCurrent(queryExpression, queryParameters)) {
215  return new SelectorImpl<TYPE>(this, queryExpression,
216  queryParameters);
217  }
218  return this;
219  }
220 
221  @Override
222  public Selector<TYPE> Content(String queryExpression,
223  String... queryParameters) {
224  if (queryParameters == null) {
225  return this.Content(queryExpression, new ArrayList<String>());
226  }
227  return this.Content(queryExpression, Arrays.asList(queryParameters));
228  }
229 
230  @Override
231  public Selector<TYPE> maxSamples(int max) {
232  if (max < 1) {
233  throw new IllegalArgumentExceptionImpl(this.environment,
234  "maxSamples < 1 not supported.");
235  }
236  if (this.maxSamples != max) {
237  return new SelectorImpl<TYPE>(this, max);
238  }
239  return this;
240  }
241 
242  @Override
244  return this.instance;
245  }
246 
247  @Override
248  public boolean retrieveNextInstance() {
249  return this.retrieveNextInstance;
250  }
251 
252  @Override
254  return this.dataState.clone();
255 
256  }
257 
258  @Override
259  public String getQueryExpression() {
260  return this.queryExpression;
261 
262  }
263 
264  @Override
265  public List<String> getQueryParameters() {
266  return Collections.unmodifiableList(this.queryParameters);
267  }
268 
269  @Override
270  public int getMaxSamples() {
271  return this.maxSamples;
272  }
273 
274  @Override
276  if (this.condition == null) {
277  return this.reader.createReadCondition(this.dataState);
278  }
279  return this.condition;
280  }
281 
282  @Override
283  public Iterator<TYPE> read() {
284  return this.reader.read(this);
285  }
286 
287  @Override
288  public List<Sample<TYPE>> read(List<Sample<TYPE>> samples) {
289  return this.reader.read(samples, this);
290  }
291 
292  @Override
293  public Iterator<TYPE> take() {
294  return this.reader.take(this);
295  }
296 
297  @Override
298  public List<Sample<TYPE>> take(List<Sample<TYPE>> samples) {
299  return this.reader.take(samples, this);
300  }
301 
302  @Override
303  protected void finalize() {
304  if (this.condition != null) {
305  try {
306  this.condition.close();
307  } catch (Exception e) {
308  // Ignore any exception here.
309  }
310  }
311  }
312 }
A DataReader allows the application (1) to declare the data it wishes to receive (i.e., make a subscription) and (2) to access the data received by the attached org.omg.dds.sub.Subscriber.
A Subscriber is the object responsible for the actual reception of the data resulting from its subscr...
Definition: Subscriber.java:69
A Sample represents an atom of data information (i.e., one value for one instance) as returned by a o...
Definition: Sample.java:116
Selector< TYPE > Content(String queryExpression, List< String > queryParameters)
Selector< TYPE > nextInstance(boolean retrieveNextInstance)
A DataState encapsulates sets of sample states, view states, and instance states as a convenience...
Selector< TYPE > maxSamples(int max)
Selector< TYPE > dataState(DataState state)
Selector< TYPE > instance(InstanceHandle handle)
Selector< TYPE > Content(String queryExpression, String... queryParameters)
Set< SampleState > getSampleStates()
Get the current set of sample states.
An opaque handle that can be used to refer to a local or remote entity.
List< Sample< TYPE > > take(List< Sample< TYPE >> samples)
void close()
Reclaim any resources associated with this condition.
ReadCondition< TYPE > createReadCondition(DataState states)
This operation creates a ReadCondition.
ReadCondition objects are conditions specifically dedicated to read operations and attached to one or...
Iterator< TYPE > take()
This operation accesses a collection of samples from this DataReader.
SelectorImpl(OsplServiceEnvironment environment, AbstractDataReader< TYPE > reader)
Set< ViewState > getViewStates()
Get the current set of view states.
static DataStateImpl any(OsplServiceEnvironment environment)
DDS implementations are rooted in this class, a concrete subclass of which can be instantiated based ...
Selector class encapsulates different ways of selecting samples from a org.omg.dds.sub.DataReader.
Set< InstanceState > getInstanceStates()
Get the current set of instance states.
ReadCondition< TYPE > getCondition()
List< Sample< TYPE > > read(List< Sample< TYPE >> samples)
Iterator< TYPE > read()
This operation accesses a collection of samples from this DataReader.