OpenSplice Java 5 DCPS  v6.x
OpenSplice Java 5 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
PreAllocatorImpl.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;
32 
33 import DDS.SampleInfo;
34 import DDS.SampleInfoSeqHolder;
35 
36 public class PreAllocatorImpl<TYPE> implements PreAllocator<TYPE> {
37  private final OsplServiceEnvironment environment;
38  private final Class<?> dataSeqHolderClaz;
39  private final Class<TYPE> dataClaz;
40  private final Field dataSeqHolderValueField;
41 
42  private SampleInfoSeqHolder infoSeqHolder;
43  private Object dataSeqHolder;
44  private List<Sample<TYPE>> sampleList;
45  private int lastLength;
46 
48  Class<?> dataSeqHolderClaz, Field dataSeqHolderValueField,
49  Class<TYPE> dataClaz, List<Sample<TYPE>> preAllocated) {
50  this.environment = environment;
51  this.sampleList = preAllocated;
52  this.dataSeqHolderClaz = dataSeqHolderClaz;
53  this.dataClaz = dataClaz;
54  this.dataSeqHolderValueField = dataSeqHolderValueField;
55  this.lastLength = -1;
56  this.setSampleList(preAllocated);
57  }
58 
59  @Override
60  public void setSampleList(List<Sample<TYPE>> preAllocated) {
61  try {
62  if (preAllocated == null) {
63  this.sampleList = new ArrayList<Sample<TYPE>>();
64  this.infoSeqHolder = new SampleInfoSeqHolder();
65  this.dataSeqHolder = dataSeqHolderClaz.newInstance();
66  } else if (preAllocated == this.sampleList) {
67  if (this.lastLength != preAllocated.size()) {
68  if (this.lastLength == -1) {
69  this.infoSeqHolder = new SampleInfoSeqHolder();
70  this.dataSeqHolder = dataSeqHolderClaz.newInstance();
71  }
72  this.infoSeqHolder.value = new SampleInfo[preAllocated
73  .size()];
74  this.dataSeqHolderValueField.set(this.dataSeqHolder,
75  Array.newInstance(dataClaz, preAllocated.size()));
76  this.copyData();
77  }
78  } else {
79  this.sampleList = preAllocated;
80  this.infoSeqHolder = new SampleInfoSeqHolder();
81  this.dataSeqHolder = dataSeqHolderClaz.newInstance();
82  this.copyData();
83  }
84  this.lastLength = this.sampleList.size();
85  } catch (InstantiationException e) {
86  throw new DDSExceptionImpl(this.environment, "Internal error ("
87  + e.getMessage() + ").");
88  } catch (IllegalAccessException e) {
89  throw new DDSExceptionImpl(this.environment, "Internal error ("
90  + e.getMessage() + ").");
91  } catch (ClassCastException ce) {
92  throw new IllegalArgumentExceptionImpl(this.environment,
93  "Usage of non-OpenSplice Sample implementation is not supported.");
94  }
95  }
96 
97  private void copyData() {
98  int i = 0;
99 
100  try {
101  this.infoSeqHolder.value = new SampleInfo[this.sampleList.size()];
102  this.dataSeqHolderValueField.set(this.dataSeqHolder,
103  Array.newInstance(dataClaz, this.sampleList.size()));
104  Object dataValue = this.dataSeqHolderValueField
105  .get(this.dataSeqHolder);
106 
107  for (Sample<TYPE> sample : this.sampleList) {
108  this.infoSeqHolder.value[i] = ((SampleImpl<TYPE>) sample)
109  .getInfo();
110  Array.set(dataValue, i++, sample.getData());
111  }
112  } catch (IllegalAccessException e) {
113  throw new DDSExceptionImpl(this.environment, "Internal error ("
114  + e.getMessage() + ").");
115  } catch (ClassCastException ce) {
116  throw new IllegalArgumentExceptionImpl(this.environment,
117  "Usage of non-OpenSplice Sample implementation is not supported.");
118  }
119  }
120 
121  @Override
122  public void updateReferences() {
123  this.updateReferencesImproved();
124  }
125 
126  @SuppressWarnings("unchecked")
127  private void updateReferencesImproved() {
128  int index;
129 
130  assert (this.lastLength == this.sampleList.size());
131 
132  if (this.infoSeqHolder.value.length > this.lastLength) {
133  try {
134  Object dataValue = this.dataSeqHolderValueField
135  .get(this.dataSeqHolder);
136  index = this.lastLength;
137 
138  while (index < this.infoSeqHolder.value.length) {
139  this.sampleList.add(new SampleImpl<TYPE>(
140  this.environment,
141  (TYPE)Array.get(dataValue, index),
142  this.infoSeqHolder.value[index]));
143  index++;
144  }
145  } catch (IllegalArgumentException e) {
146  throw new DDSExceptionImpl(this.environment, "Internal error ("
147  + e.getMessage() + ").");
148  } catch (IllegalAccessException e) {
149  throw new DDSExceptionImpl(this.environment, "Internal error ("
150  + e.getMessage() + ").");
151  }
152  } else if (this.infoSeqHolder.value.length < this.lastLength) {
153  index = this.infoSeqHolder.value.length;
154  int curLength = this.lastLength;
155 
156  while (index < curLength) {
157  this.sampleList.remove(index);
158  curLength--;
159  }
160  }
161  this.lastLength = this.sampleList.size();
162  }
163 
164  @Override
165  public Object getDataSeqHolder() {
166  return this.dataSeqHolder;
167  }
168 
169  @Override
170  public SampleInfoSeqHolder getInfoSeqHolder() {
171  return this.infoSeqHolder;
172  }
173 
174  @Override
175  public List<Sample<TYPE>> getSampleList() {
176  return this.sampleList;
177  }
178 
179 }
A Sample represents an atom of data information (i.e., one value for one instance) as returned by a o...
Definition: Sample.java:116
PreAllocatorImpl(OsplServiceEnvironment environment, Class<?> dataSeqHolderClaz, Field dataSeqHolderValueField, Class< TYPE > dataClaz, List< Sample< TYPE >> preAllocated)
void setSampleList(List< Sample< TYPE >> preAllocated)