Skip to content

Commit c2c75ec

Browse files
committed
[SWE] Add support for collections of simple types in DataBlockProxy
1 parent 43826d6 commit c2c75ec

File tree

3 files changed

+179
-83
lines changed

3 files changed

+179
-83
lines changed

lib-ogc/swe-common-core/src/main/java/org/vast/data/DataBlockParallel.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,20 @@ public String toString()
176176
buffer.append("PARALLEL: ");
177177
buffer.append('[');
178178

179-
selectBlock(0);
180-
int start = blockIndex;
181-
selectBlock(getAtomCount() - 1);
182-
int stop = blockIndex + 1;
183-
184-
for (int i = start; i < stop; i++)
185-
{
186-
buffer.append(blockArray[i].toString());
187-
if (i < stop - 1)
188-
buffer.append(',');
189-
}
179+
if (atomCount > 0)
180+
{
181+
selectBlock(0);
182+
int start = blockIndex;
183+
selectBlock(getAtomCount() - 1);
184+
int stop = blockIndex + 1;
185+
186+
for (int i = start; i < stop; i++)
187+
{
188+
buffer.append(blockArray[i].toString());
189+
if (i < stop - 1)
190+
buffer.append(',');
191+
}
192+
}
190193

191194
buffer.append(']');
192195
return buffer.toString();

lib-ogc/swe-common-core/src/main/java/org/vast/data/DataBlockProxy.java

Lines changed: 103 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -143,86 +143,42 @@ else if (method.isDefault())
143143
var data = comp.getData();
144144
var retType = method.getReturnType();
145145

146-
if (retType == boolean.class)
147-
return data.getBooleanValue();
148-
else if (retType == byte.class)
149-
return data.getByteValue();
150-
else if (retType == short.class)
151-
return data.getShortValue();
152-
else if (retType == int.class)
153-
return data.getIntValue();
154-
else if (retType == long.class)
155-
return data.getLongValue();
156-
else if (retType == float.class)
157-
return data.getFloatValue();
158-
else if (retType == double.class)
159-
return data.getDoubleValue();
160-
else if (retType == String.class)
161-
return data.getStringValue();
162-
else if (retType == Instant.class)
163-
return data.getTimeStamp();
164-
else if (retType == OffsetDateTime.class)
165-
return data.getDateTime();
166-
else if (Collection.class.isAssignableFrom(retType))
146+
if (Collection.class.isAssignableFrom(retType))
167147
{
168148
assertDataArray(method, comp);
169149

170150
var itemType = (Class<?>)((ParameterizedType)method.getGenericReturnType()).getActualTypeArguments()[0];
171-
if (!IDataAccessor.class.isAssignableFrom(itemType))
172-
throw new IllegalStateException("Collection item type must be an accessor class");
151+
var isSimpleType = isSimpleType(itemType);
152+
Asserts.checkState(isSimpleType || IDataAccessor.class.isAssignableFrom(itemType),
153+
"Collection item type must be a simple type or a IDataAccessor");
173154

174-
@SuppressWarnings({"unchecked"})
175-
var accessorClass = (Class<IDataAccessor>)itemType;
176-
return createCollection(accessorClass, (DataArray)comp);
155+
return createCollection(itemType, isSimpleType, (DataArray)comp);
177156
}
178157
else
179-
throw new IllegalStateException("Unsupported datatype: " + retType);
158+
return getDataValue(retType, data);
180159
}
181160

182161
else if (isSetNumMethod(method))
183162
{
184163
assertDataArray(method, comp);
185164
var arraySize = (int)args[0];
186165
((DataArray)comp).updateSize(arraySize);
187-
return null;
188166
}
189167

190168
else if (isSetMethod(method))
191169
{
192170
var data = comp.getData();
193171
var argType = method.getParameters()[0].getType();
194172
var val = args[0];
195-
196-
if (argType == boolean.class)
197-
data.setBooleanValue((boolean)val);
198-
else if (argType == byte.class)
199-
data.setByteValue((byte)val);
200-
else if (argType == short.class)
201-
data.setShortValue((short)val);
202-
else if (argType == int.class)
203-
data.setIntValue((int)val);
204-
else if (argType == long.class)
205-
data.setLongValue((long)val);
206-
else if (argType == float.class)
207-
data.setFloatValue((float)val);
208-
else if (argType == double.class)
209-
data.setDoubleValue((double)val);
210-
else if (argType == String.class)
211-
data.setStringValue((String)val);
212-
else if (argType == Instant.class)
213-
data.setTimeStamp((Instant)val);
214-
else if (argType == OffsetDateTime.class)
215-
data.setDateTime((OffsetDateTime)val);
216-
else
217-
throw new IllegalStateException("Unsupported datatype: " + argType);
218-
return null;
173+
setDataValue(data, argType, val);
219174
}
220175

221176
else if (isAddMethod(method))
222177
{
223178
var retType = method.getReturnType();
224-
Asserts.checkState(IDataAccessor.class.isAssignableFrom(retType),
225-
"Return type of method " + method.getName() + " must be a IDataAccessor");
179+
var isSimpleType = isSimpleType(retType);
180+
Asserts.checkState(isSimpleType(retType) || IDataAccessor.class.isAssignableFrom(retType),
181+
"Return type of method " + method.getName() + " must be a simple type or a IDataAccessor");
226182

227183
var array = assertDataArray(method, comp);
228184
if (arrayData == null)
@@ -241,17 +197,84 @@ else if (isAddMethod(method))
241197
var parent = ((AbstractDataComponentImpl)array.getParent());
242198
parent.updateAtomCount(newDblk.getAtomCount());
243199

244-
// create accessor for new element
245-
@SuppressWarnings("unchecked")
246-
var accessorClass = (Class<IDataAccessor>)retType;
247-
var accessor = createElementProxy(accessorClass, array.getElementType());
248-
accessor.wrap(newDblk);
249-
250-
return accessor;
200+
// create accessor for new element if complex
201+
if (!isSimpleType)
202+
{
203+
@SuppressWarnings("unchecked")
204+
var accessorClass = (Class<IDataAccessor>)retType;
205+
var accessor = createElementProxy(accessorClass, array.getElementType());
206+
accessor.wrap(newDblk);
207+
return accessor;
208+
}
209+
else
210+
{
211+
var argType = method.getParameters()[0].getType();
212+
var val = args[0];
213+
setDataValue(newDblk, argType, val);
214+
}
251215
}
252216

217+
return null;
218+
}
219+
220+
221+
protected Object getDataValue(Class<?> retType, DataBlock data)
222+
{
223+
if (retType == boolean.class)
224+
return data.getBooleanValue();
225+
else if (retType == byte.class)
226+
return data.getByteValue();
227+
else if (retType == short.class)
228+
return data.getShortValue();
229+
else if (retType == int.class)
230+
return data.getIntValue();
231+
else if (retType == long.class)
232+
return data.getLongValue();
233+
else if (retType == float.class)
234+
return data.getFloatValue();
235+
else if (retType == double.class)
236+
return data.getDoubleValue();
237+
else if (retType == String.class)
238+
return data.getStringValue();
239+
else if (retType == Instant.class)
240+
return data.getTimeStamp();
241+
else if (retType == OffsetDateTime.class)
242+
return data.getDateTime();
253243
else
254-
return null;
244+
throw new IllegalStateException("Unsupported datatype: " + retType);
245+
}
246+
247+
248+
protected void setDataValue(DataBlock data, Class<?> argType, Object val)
249+
{
250+
if (argType == boolean.class)
251+
data.setBooleanValue((boolean)val);
252+
else if (argType == byte.class)
253+
data.setByteValue((byte)val);
254+
else if (argType == short.class)
255+
data.setShortValue((short)val);
256+
else if (argType == int.class)
257+
data.setIntValue((int)val);
258+
else if (argType == long.class)
259+
data.setLongValue((long)val);
260+
else if (argType == float.class)
261+
data.setFloatValue((float)val);
262+
else if (argType == double.class)
263+
data.setDoubleValue((double)val);
264+
else if (argType == String.class)
265+
data.setStringValue((String)val);
266+
else if (argType == Instant.class)
267+
data.setTimeStamp((Instant)val);
268+
else if (argType == OffsetDateTime.class)
269+
data.setDateTime((OffsetDateTime)val);
270+
else
271+
throw new IllegalStateException("Unsupported datatype: " + argType);
272+
}
273+
274+
275+
protected boolean isSimpleType(Class<?> clazz)
276+
{
277+
return clazz.isPrimitive() || clazz == String.class || clazz == Instant.class;
255278
}
256279

257280

@@ -296,8 +319,8 @@ protected boolean isSetMethod(Method m)
296319
protected boolean isAddMethod(Method m)
297320
{
298321
return m.getName().startsWith("add") &&
299-
m.getReturnType() != void.class &&
300-
m.getParameters().length == 0;
322+
((m.getReturnType() != void.class && m.getParameters().length == 0) || // when adding sub object
323+
(m.getReturnType() == void.class && m.getParameters().length == 1)); // when adding scalar
301324
}
302325

303326

@@ -322,14 +345,14 @@ protected IDataAccessor createElementProxy(Class<IDataAccessor> clazz, DataCompo
322345
}
323346

324347

325-
protected Collection<IDataAccessor> createCollection(Class<IDataAccessor> clazz, DataArray array)
348+
protected Collection<Object> createCollection(Class<?> clazz, boolean isSimpleType, DataArray array)
326349
{
327-
return new AbstractCollection<IDataAccessor>() {
350+
return new AbstractCollection<Object>() {
328351

329352
@Override
330-
public Iterator<IDataAccessor> iterator()
353+
public Iterator<Object> iterator()
331354
{
332-
return new Iterator<IDataAccessor>()
355+
return new Iterator<Object>()
333356
{
334357
int idx;
335358

@@ -340,12 +363,20 @@ public boolean hasNext()
340363
}
341364

342365
@Override
343-
public IDataAccessor next()
366+
public Object next()
344367
{
345368
var elt = array.getComponent(idx++);
346-
var accessor = createElementProxy(clazz, array.getElementType());
347-
accessor.wrap(elt.getData().copy());
348-
return accessor;
369+
370+
if (!isSimpleType) {
371+
@SuppressWarnings({"unchecked"})
372+
var accessorClass = (Class<IDataAccessor>)clazz;
373+
var accessor = createElementProxy(accessorClass, array.getElementType());
374+
accessor.wrap(elt.getData().copy());
375+
return accessor;
376+
}
377+
else {
378+
return getDataValue(clazz, elt.getData());
379+
}
349380
}
350381
};
351382
}

lib-ogc/swe-common-core/src/test/java/org/vast/swe/test/TestDataAccessor.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,34 @@ static DataRecord getSchema()
8383
}
8484

8585

86+
interface RecordAccessor3 extends IDataAccessor
87+
{
88+
@SweMapping(path="type")
89+
public String getRecordType();
90+
91+
@SweMapping(path="array")
92+
public Collection<String> getTokens();
93+
94+
@SweMapping(path="array")
95+
public void addToken();
96+
97+
static DataRecord getSchema()
98+
{
99+
var swe = new SWEHelper();
100+
Count sizeComp;
101+
return swe.createRecord()
102+
.addField("type", swe.createCategory())
103+
.addField("count", sizeComp = swe.createCount()
104+
.id("ARRAY_SIZE")
105+
.build())
106+
.addField("array", swe.createArray()
107+
.withSizeComponent(sizeComp)
108+
.withElement("token", swe.createText()))
109+
.build();
110+
}
111+
}
112+
113+
86114
@Test
87115
public void testReadRecordOfScalars()
88116
{
@@ -155,5 +183,39 @@ public void testReadArrayOfRecords()
155183
}
156184

157185
}
186+
187+
188+
@Test
189+
public void testReadArrayOfScalars()
190+
{
191+
var rec = RecordAccessor3.getSchema();
192+
var accessor = DataBlockProxy.generate(rec, RecordAccessor3.class);
193+
194+
String type = "wpts";
195+
String[] codes = {"LCH", "LFT", "AWDAD", "VOODO"};
196+
197+
int i = 0;
198+
var dblk = rec.createDataBlock();
199+
dblk.setStringValue(i++, type);
200+
dblk.setIntValue(i++, codes.length);
201+
((DataBlockMixed)dblk).getUnderlyingObject()[2].resize(codes.length);
202+
for (var r = 0; r < codes.length; r++) {
203+
dblk.setStringValue(i++, codes[r]);
204+
}
205+
206+
accessor.wrap(dblk);
207+
208+
assertEquals(type, accessor.getRecordType());
209+
assertEquals(codes.length, accessor.getTokens().size());
210+
int r = 0;
211+
for (var elt: accessor.getTokens())
212+
{
213+
assertEquals(codes[r], elt);
214+
r++;
215+
216+
System.out.println(elt.toString());
217+
}
218+
219+
}
158220

159221
}

0 commit comments

Comments
 (0)