OpenSplice Java 5 DCPS  v6.x
OpenSplice Java 5 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
DataStateImpl.java
Go to the documentation of this file.
1 /*
2  * Vortex OpenSplice
3  *
4  * This software and documentation are Copyright 2006 to 2021 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.Collection;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.Set;
27 
29 import org.omg.dds.sub.InstanceState;
30 import org.omg.dds.sub.SampleState;
31 import org.omg.dds.sub.Subscriber;
36 import org.omg.dds.sub.ViewState;
37 
38 public class DataStateImpl implements Subscriber.DataState {
39  private OsplServiceEnvironment environment;
40  private HashSet<SampleState> sampleState;
41  private HashSet<ViewState> viewState;
42  private HashSet<InstanceState> instanceState;
43 
45  Collection<SampleState> sampleState,
46  Collection<ViewState> viewState,
47  Collection<InstanceState> instanceState) {
48  this.environment = environment;
49  this.sampleState = new HashSet<SampleState>(sampleState);
50  this.viewState = new HashSet<ViewState>(viewState);
51  this.instanceState = new HashSet<InstanceState>(instanceState);
52  }
53 
54  public DataStateImpl(OsplServiceEnvironment environment) {
55  this.environment = environment;
56  this.sampleState = new HashSet<SampleState>();
57  this.viewState = new HashSet<ViewState>();
58  this.instanceState = new HashSet<InstanceState>();
59  }
60 
62  return (DataStateImpl) new DataStateImpl(env).withAnySampleState()
63  .withAnyViewState().withAnyInstanceState();
64  }
65 
67  int state) {
68  switch (state) {
69  case DDS.READ_SAMPLE_STATE.value:
70  return SampleState.READ;
71  case DDS.NOT_READ_SAMPLE_STATE.value:
72  return SampleState.NOT_READ;
73  default:
74  throw new IllegalArgumentExceptionImpl(env, "Invalid SampleState");
75  }
76  }
77 
79  int state) {
80  switch (state) {
81  case DDS.NEW_VIEW_STATE.value:
82  return ViewState.NEW;
83  case DDS.NOT_NEW_VIEW_STATE.value:
84  return ViewState.NOT_NEW;
85  default:
86  throw new IllegalArgumentExceptionImpl(env, "Invalid ViewState");
87  }
88  }
89 
91  OsplServiceEnvironment env, int state) {
92  switch (state) {
93  case DDS.ALIVE_INSTANCE_STATE.value:
94  return InstanceState.ALIVE;
95  case DDS.NOT_ALIVE_DISPOSED_INSTANCE_STATE.value:
97  case DDS.NOT_ALIVE_NO_WRITERS_INSTANCE_STATE.value:
99  default:
100  throw new IllegalArgumentExceptionImpl(env, "Invalid InstanceState");
101  }
102  }
103 
104  @Override
106  return this.environment;
107  }
108 
109  @Override
110  public Set<SampleState> getSampleStates() {
111  return Collections.unmodifiableSet(this.sampleState);
112  }
113 
114  @Override
115  public Set<ViewState> getViewStates() {
116  return Collections.unmodifiableSet(this.viewState);
117  }
118 
119  @Override
120  public Set<InstanceState> getInstanceStates() {
121  return Collections.unmodifiableSet(this.instanceState);
122  }
123 
124  @Override
125  public DataState with(SampleState state) {
126  HashSet<SampleState> s = new HashSet<SampleState>(this.sampleState);
127  s.add(state);
128 
129  return new DataStateImpl(this.environment, s, this.viewState,
130  this.instanceState);
131  }
132 
133  @Override
134  public DataState with(ViewState state) {
135  HashSet<ViewState> s = new HashSet<ViewState>(this.viewState);
136  s.add(state);
137 
138  return new DataStateImpl(this.environment, this.sampleState, s,
139  this.instanceState);
140  }
141 
142  @Override
143  public DataState with(InstanceState state) {
144  HashSet<InstanceState> s = new HashSet<InstanceState>(
145  this.instanceState);
146  s.add(state);
147 
148  return new DataStateImpl(this.environment, this.sampleState,
149  this.viewState, s);
150  }
151 
152  @Override
154  HashSet<SampleState> s = new HashSet<SampleState>(this.sampleState);
155 
156  s.add(SampleState.READ);
157  s.add(SampleState.NOT_READ);
158 
159  return new DataStateImpl(this.environment, s, this.viewState,
160  this.instanceState);
161  }
162 
163  @Override
165  HashSet<ViewState> s = new HashSet<ViewState>(this.viewState);
166 
167  s.add(ViewState.NEW);
168  s.add(ViewState.NOT_NEW);
169 
170  return new DataStateImpl(this.environment, this.sampleState, s,
171  this.instanceState);
172  }
173 
174  @Override
176  HashSet<InstanceState> s = new HashSet<InstanceState>(
177  this.instanceState);
178 
179  s.add(InstanceState.ALIVE);
182 
183  return new DataStateImpl(this.environment, this.sampleState,
184  this.viewState, s);
185  }
186 
187  @Override
189  HashSet<InstanceState> s = new HashSet<InstanceState>(
190  this.instanceState);
191 
192  s.remove(InstanceState.ALIVE);
195 
196  return new DataStateImpl(this.environment, this.sampleState,
197  this.viewState, s);
198  }
199 
200  public int getOldSampleState() {
201  int result;
202 
203  boolean read = this.sampleState.contains(SampleState.READ);
204  boolean notRead = this.sampleState.contains(SampleState.NOT_READ);
205 
206  if (read && notRead) {
207  result = DDS.ANY_SAMPLE_STATE.value;
208  } else if (read) {
209  result = DDS.READ_SAMPLE_STATE.value;
210  } else if (notRead) {
211  result = DDS.NOT_READ_SAMPLE_STATE.value;
212  } else {
213  throw new IllegalArgumentExceptionImpl(this.environment,
214  "Incomplete DataState: no SampleState set.");
215  }
216  return result;
217  }
218 
219  public void withOldSampleState(int state) {
220  switch (state) {
221  case DDS.ANY_INSTANCE_STATE.value:
222  this.sampleState.add(SampleState.READ);
223  this.sampleState.add(SampleState.NOT_READ);
224  break;
225  case DDS.READ_SAMPLE_STATE.value:
226  this.sampleState.add(SampleState.READ);
227  break;
228  case DDS.NOT_READ_SAMPLE_STATE.value:
229  this.sampleState.add(SampleState.NOT_READ);
230  break;
231  default:
232  throw new IllegalArgumentExceptionImpl(this.environment,
233  "Invalid SampleState");
234  }
235  }
236 
237  public int getOldViewState() {
238  int result;
239 
240  boolean _new = this.viewState.contains(ViewState.NEW);
241  boolean notNew = this.viewState.contains(ViewState.NOT_NEW);
242 
243  if (_new && notNew) {
244  result = DDS.ANY_VIEW_STATE.value;
245  } else if (_new) {
246  result = DDS.NEW_VIEW_STATE.value;
247  } else if (notNew) {
248  result = DDS.NOT_NEW_VIEW_STATE.value;
249  } else {
250  throw new IllegalArgumentExceptionImpl(this.environment,
251  "Incomplete DataState: no ViewState set.");
252  }
253  return result;
254  }
255 
256  public void withOldViewState(int state) {
257  switch (state) {
258  case DDS.ANY_VIEW_STATE.value:
259  this.viewState.add(ViewState.NEW);
260  this.viewState.add(ViewState.NOT_NEW);
261  break;
262  case DDS.NEW_VIEW_STATE.value:
263  this.viewState.add(ViewState.NEW);
264  break;
265  case DDS.NOT_NEW_VIEW_STATE.value:
266  this.viewState.add(ViewState.NOT_NEW);
267  break;
268  default:
269  throw new IllegalArgumentExceptionImpl(this.environment,
270  "Invalid ViewState");
271  }
272  }
273 
274  public int getOldInstanceState() {
275  int result = 0;
276 
277  boolean alive = this.instanceState.contains(InstanceState.ALIVE);
278  boolean disposed = this.instanceState
280  boolean noWriters = this.instanceState
282 
283  if (alive) {
284  result |= DDS.ALIVE_INSTANCE_STATE.value;
285  }
286  if (disposed) {
287  result |= DDS.NOT_ALIVE_DISPOSED_INSTANCE_STATE.value;
288  }
289  if (noWriters) {
290  result |= DDS.NOT_ALIVE_NO_WRITERS_INSTANCE_STATE.value;
291  }
292 
293  if (alive && disposed && noWriters) {
294  result = DDS.ANY_INSTANCE_STATE.value;
295  }
296  if (result == 0) {
297  throw new IllegalArgumentExceptionImpl(this.environment,
298  "Incomplete DataState: no InstanceState set.");
299  }
300  return result;
301  }
302 
303  public void withOldInstanceState(int state) {
304  switch (state) {
305  case DDS.ANY_INSTANCE_STATE.value:
306  this.instanceState.add(InstanceState.ALIVE);
307  this.instanceState.add(InstanceState.NOT_ALIVE_DISPOSED);
308  this.instanceState.add(InstanceState.NOT_ALIVE_NO_WRITERS);
309  break;
310  case DDS.NOT_ALIVE_INSTANCE_STATE.value:
311  this.instanceState.remove(InstanceState.ALIVE);
312  this.instanceState.add(InstanceState.NOT_ALIVE_DISPOSED);
313  this.instanceState.add(InstanceState.NOT_ALIVE_NO_WRITERS);
314  break;
315  case DDS.ALIVE_INSTANCE_STATE.value:
316  this.instanceState.add(InstanceState.ALIVE);
317  break;
318  case DDS.NOT_ALIVE_DISPOSED_INSTANCE_STATE.value:
319  this.instanceState.add(InstanceState.NOT_ALIVE_DISPOSED);
320  break;
321  case DDS.NOT_ALIVE_NO_WRITERS_INSTANCE_STATE.value:
322  this.instanceState.add(InstanceState.NOT_ALIVE_NO_WRITERS);
323  break;
324  default:
325  throw new IllegalArgumentExceptionImpl(this.environment,
326  "Invalid InstanceState");
327  }
328  }
329 
330  public static DataStateImpl any(OsplServiceEnvironment environment) {
331  return (DataStateImpl) new DataStateImpl(environment)
332  .withAnySampleState().withAnyViewState().withAnyInstanceState();
333  }
334 
335  @SuppressWarnings("unchecked")
336  @Override
337  public DataStateImpl clone() {
338  try {
339  DataStateImpl cloned = (DataStateImpl) super.clone();
340 
341  cloned.environment = this.environment;
342  cloned.instanceState = (HashSet<InstanceState>) this.instanceState
343  .clone();
344  cloned.sampleState = (HashSet<SampleState>) this.sampleState
345  .clone();
346  cloned.viewState = (HashSet<ViewState>) this.viewState.clone();
347 
348  return cloned;
349  } catch (CloneNotSupportedException e) {
350  throw new UnsupportedOperationExceptionImpl(this.environment,
351  "Cloning of DataState not supported.");
352  }
353  }
354 
355  @Override
356  public boolean equals(Object other) {
357  if (other == this) {
358  return true;
359  }
360  if (!(other instanceof DataStateImpl)) {
361  return false;
362  }
363  DataStateImpl d = (DataStateImpl) other;
364 
365  if (this.instanceState.size() != d.instanceState.size()) {
366  return false;
367  }
368  if (!d.instanceState.containsAll(this.instanceState)) {
369  return false;
370  }
371  if (this.sampleState.size() != d.sampleState.size()) {
372  return false;
373  }
374  if (!d.sampleState.containsAll(this.sampleState)) {
375  return false;
376  }
377  if (this.viewState.size() != d.viewState.size()) {
378  return false;
379  }
380  if (!d.viewState.containsAll(this.viewState)) {
381  return false;
382  }
383  return true;
384  }
385 
386  @Override
387  public int hashCode() {
388  final int prime = 31;
389  int result = 17;
390 
391  for (InstanceState s : this.instanceState) {
392  result = prime * result + s.hashCode();
393  }
394  for (SampleState s : this.sampleState) {
395  result = prime * result + s.hashCode();
396  }
397  for (ViewState s : this.viewState) {
398  result = prime * result + s.hashCode();
399  }
400  return result;
401  }
402 }
A Subscriber is the object responsible for the actual reception of the data resulting from its subscr...
Definition: Subscriber.java:69
For each instance the Data Distribution Service internally maintains an InstanceState.
For each sample, the Data Distribution Service internally maintains a SampleState specific to each Da...
static InstanceState getInstanceStateFromOld(OsplServiceEnvironment env, int state)
A DataState encapsulates sets of sample states, view states, and instance states as a convenience...
NOT_READ
NOT_READ indicates that the DataReader has not accessed that sample before.
DataStateImpl(OsplServiceEnvironment environment, Collection< SampleState > sampleState, Collection< ViewState > viewState, Collection< InstanceState > instanceState)
DataStateImpl(OsplServiceEnvironment environment)
ALIVE
ALIVE indicates that:
static SampleState getSampleStateFromOld(OsplServiceEnvironment env, int state)
NOT_ALIVE_NO_WRITERS
NOT_ALIVE_NO_WRITERS indicates the instance has been declared as not-alive by the DataReader because ...
DataState with(InstanceState state)
For each instance (identified by the key), the Data Distribution Service internally maintains a ViewS...
Definition: ViewState.java:26
NOT_NEW
NOT_NEW indicates that the DataReader has already accessed samples of the same instance and that the ...
Definition: ViewState.java:40
Set< InstanceState > getInstanceStates()
static DataStateImpl any(OsplServiceEnvironment environment)
static DataStateImpl getAnyStateDataState(OsplServiceEnvironment env)
static ViewState getViewStateFromOld(OsplServiceEnvironment env, int state)
DDS implementations are rooted in this class, a concrete subclass of which can be instantiated based ...
NOT_ALIVE_DISPOSED
NOT_ALIVE_DISPOSED indicates the instance was disposed of by a DataWriter either explicitly by means ...
READ
READ indicates that the DataReader has already accessed that sample by means of org.omg.dds.sub.DataReader#read().
DataState with(SampleState state)
NEW
NEW indicates that either this is the first time that the DataReader has ever accessed samples of tha...
Definition: ViewState.java:35
DataState with(ViewState state)