OpenSplice Java 5 DCPS  v6.x
OpenSplice Java 5 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
PreAllocatorProtobuf.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.lang.reflect.Array;
24 import java.lang.reflect.Field;
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 import org.omg.dds.sub.Sample;
33 
34 import DDS.SampleInfoSeqHolder;
35 
36 public class PreAllocatorProtobuf<PROTOBUF_TYPE, DDS_TYPE> implements
37  PreAllocator<PROTOBUF_TYPE> {
38  private final OsplServiceEnvironment environment;
39  private final Class<?> dataSeqHolderClaz;
40  private final Field dataSeqHolderValueField;
41  private final TypeSupportProtobuf<PROTOBUF_TYPE, DDS_TYPE> typeSupport;
42 
43  private SampleInfoSeqHolder infoSeqHolder;
44  private Object dataSeqHolder;
45  private List<Sample<PROTOBUF_TYPE>> sampleList;
46 
47  @SuppressWarnings("unchecked")
49  DataReaderProtobuf<PROTOBUF_TYPE, DDS_TYPE> reader,
50  Class<?> dataSeqHolderClaz, Field dataSeqHolderValueField,
51  List<Sample<PROTOBUF_TYPE>> preAllocated) {
52  this.environment = environment;
53  this.sampleList = preAllocated;
54  this.dataSeqHolderClaz = dataSeqHolderClaz;
55  this.dataSeqHolderValueField = dataSeqHolderValueField;
56  this.typeSupport = (TypeSupportProtobuf<PROTOBUF_TYPE, DDS_TYPE>) reader
57  .getTopicDescription().getTypeSupport();
58  this.setSampleList(preAllocated);
59  }
60 
61  @Override
62  public void setSampleList(List<Sample<PROTOBUF_TYPE>> preAllocated) {
63 
64  try {
65  if (preAllocated == null) {
66  this.sampleList = new ArrayList<Sample<PROTOBUF_TYPE>>();
67  } else {
68  this.sampleList = preAllocated;
69  this.sampleList.clear();
70  }
71  this.infoSeqHolder = new SampleInfoSeqHolder();
72  this.dataSeqHolder = dataSeqHolderClaz.newInstance();
73  } catch (InstantiationException e) {
74  throw new DDSExceptionImpl(this.environment, "Internal error ("
75  + e.getMessage() + ").");
76  } catch (IllegalAccessException e) {
77  throw new DDSExceptionImpl(this.environment, "Internal error ("
78  + e.getMessage() + ").");
79  } catch (ClassCastException ce) {
80  throw new IllegalArgumentExceptionImpl(this.environment,
81  "Usage of non-OpenSplice Sample implementation is not supported.");
82  }
83  }
84 
85  @SuppressWarnings("unchecked")
86  @Override
87  public void updateReferences() {
88  try {
89  Object dataValue = this.dataSeqHolderValueField
90  .get(this.dataSeqHolder);
91 
92  for (int i = 0; i < this.infoSeqHolder.value.length; i++) {
93  if (this.infoSeqHolder.value[i].valid_data) {
94  this.sampleList.add(new SampleImpl<PROTOBUF_TYPE>(
95  this.environment,
96  this.typeSupport.ddsToProtobuf((DDS_TYPE)Array.get(dataValue, i)),
97  this.infoSeqHolder.value[i]));
98  } else {
99  this.sampleList.add(new SampleImpl<PROTOBUF_TYPE>(
100  this.environment,
101  this.typeSupport.ddsKeyToProtobuf((DDS_TYPE)Array.get(dataValue, i)),
102  this.infoSeqHolder.value[i]));
103  }
104  }
105 
106  } catch (IllegalArgumentException e) {
107  throw new DDSExceptionImpl(this.environment, "Internal error ("
108  + e.getMessage() + ").");
109  } catch (IllegalAccessException e) {
110  throw new DDSExceptionImpl(this.environment, "Internal error ("
111  + e.getMessage() + ").");
112  }
113  }
114 
115  @Override
116  public Object getDataSeqHolder() {
117  return this.dataSeqHolder;
118  }
119 
120  @Override
121  public SampleInfoSeqHolder getInfoSeqHolder() {
122  return this.infoSeqHolder;
123  }
124 
125  @Override
126  public List<Sample<PROTOBUF_TYPE>> getSampleList() {
127  return this.sampleList;
128  }
129 
130 }
A Sample represents an atom of data information (i.e., one value for one instance) as returned by a o...
Definition: Sample.java:116
void setSampleList(List< Sample< PROTOBUF_TYPE >> preAllocated)
List< Sample< PROTOBUF_TYPE > > getSampleList()