Skip to content

Commit

Permalink
refactor(plc4j/codgen): cleanup list handling/revert list entries to …
Browse files Browse the repository at this point in the history
…anonymous logical name
  • Loading branch information
sruehl committed Oct 25, 2021
1 parent 1112c78 commit 02e102c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,17 @@ public class ${type.name}IO implements <#if outputFlavor != "passive">MessageIO<
<#-- If this is a count array, we can directly initialize an array with the given size -->
<#if field.isCountArrayField()>
// Count array
List<${helper.getNonPrimitiveLanguageTypeNameForField(arrayField)}> ${arrayField.name} = readCountArrayField("${arrayField.name}", ${helper.getNonPrimitiveLanguageTypeNameForField(field)}.class, ${helper.getDataReaderCall(arrayField.type)}, ${helper.toParseExpression(arrayField, arrayField.loopExpression, parserArguments)});
List<${helper.getNonPrimitiveLanguageTypeNameForField(arrayField)}> ${arrayField.name} = readCountArrayField("${arrayField.name}", ${helper.getDataReaderCall(arrayField.type)}, ${helper.toParseExpression(arrayField, arrayField.loopExpression, parserArguments)});
<#-- In all other cases do we have to work with a list, that is later converted to an array -->
<#else>
<#-- For a length array, we read data till the read position of the buffer reaches a given position -->
<#if field.isLengthArrayField()>
// Length array
List<${helper.getNonPrimitiveLanguageTypeNameForField(arrayField)}> ${arrayField.name} = readLengthArrayField("${arrayField.name}", ${helper.getNonPrimitiveLanguageTypeNameForField(field)}.class, ${helper.getDataReaderCall(arrayField.type)}, ${helper.toParseExpression(arrayField, arrayField.loopExpression, parserArguments)});
List<${helper.getNonPrimitiveLanguageTypeNameForField(arrayField)}> ${arrayField.name} = readLengthArrayField("${arrayField.name}", ${helper.getDataReaderCall(arrayField.type)}, ${helper.toParseExpression(arrayField, arrayField.loopExpression, parserArguments)});
<#-- A terminated array keeps on reading data as long as the termination expression evaluates to false -->
<#elseif field.isTerminatedArrayField()>
// Terminated array
List<${helper.getNonPrimitiveLanguageTypeNameForField(arrayField)}> ${arrayField.name} = readTerminatedArrayField("${arrayField.name}", ${helper.getNonPrimitiveLanguageTypeNameForField(field)}.class, ${helper.getDataReaderCall(arrayField.type)}, () -> ((boolean) (${helper.toParseExpression(arrayField, arrayField.loopExpression, parserArguments)})));
List<${helper.getNonPrimitiveLanguageTypeNameForField(arrayField)}> ${arrayField.name} = readTerminatedArrayField("${arrayField.name}", ${helper.getDataReaderCall(arrayField.type)}, () -> ((boolean) (${helper.toParseExpression(arrayField, arrayField.loopExpression, parserArguments)})));
</#if>
</#if>
</#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public T readField(String logicalName, DataReader<T> dataReader, WithReaderArgs.
throw new NotImplementedException("use dedicated methods instead");
}

public List<T> readFieldCount(String logicalName, Class<?> type, DataReader<T> dataReader, long count, WithReaderArgs... readerArgs) throws ParseException {
public List<T> readFieldCount(String logicalName, DataReader<T> dataReader, long count, WithReaderArgs... readerArgs) throws ParseException {
if (count > Integer.MAX_VALUE) {
throw new ParseException("Array count of " + count + " exceeds the maximum allowed count of " + Integer.MAX_VALUE);
}
Expand All @@ -49,32 +49,32 @@ public List<T> readFieldCount(String logicalName, Class<?> type, DataReader<T> d
int itemCount = Math.max(0, (int) count);
List<T> result = new ArrayList<>(itemCount);
for (int curItem = 0; curItem < itemCount; curItem++) {
result.add(dataReader.read(type.getSimpleName(), readerArgs));
result.add(dataReader.read("", readerArgs));
}
dataReader.closeContext(logicalName, readerArgs);
return result;
}

public List<T> readFieldLength(String logicalName, Class<?> type, DataReader<T> dataReader, int length, WithReaderArgs... readerArgs) throws ParseException {
public List<T> readFieldLength(String logicalName, DataReader<T> dataReader, int length, WithReaderArgs... readerArgs) throws ParseException {
// Ensure we have the render as list argument present
readerArgs = ArrayUtils.add(readerArgs, WithReaderWriterArgs.WithRenderAsList(true));
dataReader.pullContext(logicalName, readerArgs);
int startPos = dataReader.getPos();
List<T> result = new ArrayList<>();
while (dataReader.getPos() < startPos + length) {
result.add(dataReader.read(type.getSimpleName(), readerArgs));
result.add(dataReader.read("", readerArgs));
}
dataReader.closeContext(logicalName, readerArgs);
return result;
}

public List<T> readFieldTerminated(String logicalName, Class<?> type, DataReader<T> dataReader, Supplier<Boolean> termination, WithReaderArgs... readerArgs) throws ParseException {
public List<T> readFieldTerminated(String logicalName, DataReader<T> dataReader, Supplier<Boolean> termination, WithReaderArgs... readerArgs) throws ParseException {
// Ensure we have the render as list argument present
readerArgs = ArrayUtils.add(readerArgs, WithReaderWriterArgs.WithRenderAsList(true));
dataReader.pullContext(logicalName, readerArgs);
List<T> result = new ArrayList<>();
while (!termination.get()) {
result.add(dataReader.read(type.getSimpleName(), readerArgs));
result.add(dataReader.read("", readerArgs));
}
dataReader.closeContext(logicalName, readerArgs);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.plc4x.java.spi.generation.ParseException;
import org.apache.plc4x.java.spi.generation.WithReaderArgs;

import java.lang.reflect.Array;
import java.util.List;
import java.util.function.Supplier;

Expand All @@ -32,67 +31,32 @@ public static <T> T readAbstractField(String logicalName, DataReader<T> dataRead
return new FieldReaderAbstract<T>().readField(logicalName, dataReader, readerArgs);
}

@Deprecated
@SuppressWarnings("unchecked")
public static <T> T[] readCountArrayFieldArray(String logicalName, Class<T> type, DataReader<T> dataReader, long count, WithReaderArgs... readerArgs) throws ParseException {
List<T> innerResult = readCountArrayField(logicalName, type, dataReader, count, readerArgs);
T[] result = (T[]) Array.newInstance(type, innerResult.size());
for (int curItem = 0; curItem < innerResult.size(); curItem++) {
result[curItem] = innerResult.get(curItem);
}
dataReader.closeContext(logicalName, readerArgs);
return result;
public static <T> List<T> readCountArrayField(String logicalName, DataReader<T> dataReader, long count, WithReaderArgs... readerArgs) throws ParseException {
return new FieldReaderArray<T>().readFieldCount(logicalName, dataReader, count, readerArgs);
}

public static <T> List<T> readCountArrayField(String logicalName, Class<?> type, DataReader<T> dataReader, long count, WithReaderArgs... readerArgs) throws ParseException {
return new FieldReaderArray<T>().readFieldCount(logicalName, type, dataReader, count, readerArgs);
}

@Deprecated
@SuppressWarnings("unchecked")
public static <T> T[] readLengthArrayFieldArray(String logicalName, Class<T> type, DataReader<T> dataReader, int length, WithReaderArgs... readerArgs) throws ParseException {
List<T> innerResult = readLengthArrayField(logicalName, type, dataReader, length, readerArgs);
T[] result = (T[]) Array.newInstance(type, innerResult.size());
for (int curItem = 0; curItem < innerResult.size(); curItem++) {
result[curItem] = innerResult.get(curItem);
}
dataReader.closeContext(logicalName, readerArgs);
return result;
}

public static <T> List<T> readLengthArrayField(String logicalName, Class<T> type, DataReader<T> dataReader, int length, WithReaderArgs... readerArgs) throws ParseException {
return new FieldReaderArray<T>().readFieldLength(logicalName, type, dataReader, length, readerArgs);
public static <T> List<T> readLengthArrayField(String logicalName, DataReader<T> dataReader, int length, WithReaderArgs... readerArgs) throws ParseException {
return new FieldReaderArray<T>().readFieldLength(logicalName, dataReader, length, readerArgs);
}

/**
* In some protocols a long is used as length, but we simply can't address that many bytes,
* so we simply cast it down to int as on java we couldn't even read more bytes as MAX-INT.
* @param logicalName
* @param dataReader
* @param length
* @param readerArgs
* @param <T>
* @return
* @throws ParseException
*
* @param logicalName the logical name of this field
* @param dataReader the dataReader used to retrieve this field
* @param length the length of the array
* @param readerArgs optional read args
* @param <T> the type of the array elements
* @return the read length array
* @throws ParseException if something went wrong parsing
*/
public static <T> List<T> readLengthArrayField(String logicalName, Class<T> type, DataReader<T> dataReader, long length, WithReaderArgs... readerArgs) throws ParseException {
return new FieldReaderArray<T>().readFieldLength(logicalName, type, dataReader, (int) length, readerArgs);
}

@Deprecated
@SuppressWarnings("unchecked")
public static <T> T[] readTerminatedArrayFieldArray(String logicalName, Class<T> type, DataReader<T> dataReader, Supplier<Boolean> termination, WithReaderArgs... readerArgs) throws ParseException {
List<T> innerResult = readTerminatedArrayField(logicalName, type, dataReader, termination, readerArgs);
T[] result = (T[]) Array.newInstance(type, innerResult.size());
for (int curItem = 0; curItem < innerResult.size(); curItem++) {
result[curItem] = innerResult.get(curItem);
}
dataReader.closeContext(logicalName, readerArgs);
return result;
public static <T> List<T> readLengthArrayField(String logicalName, DataReader<T> dataReader, long length, WithReaderArgs... readerArgs) throws ParseException {
return new FieldReaderArray<T>().readFieldLength(logicalName, dataReader, (int) length, readerArgs);
}

public static <T> List<T> readTerminatedArrayField(String logicalName, Class<T> type, DataReader<T> dataReader, Supplier<Boolean> termination, WithReaderArgs... readerArgs) throws ParseException {
return new FieldReaderArray<T>().readFieldTerminated(logicalName, type, dataReader, termination, readerArgs);
public static <T> List<T> readTerminatedArrayField(String logicalName, DataReader<T> dataReader, Supplier<Boolean> termination, WithReaderArgs... readerArgs) throws ParseException {
return new FieldReaderArray<T>().readFieldTerminated(logicalName, dataReader, termination, readerArgs);
}

public static <T> T readAssertField(String logicalName, DataReader<T> dataReader, WithReaderArgs... readerArgs) throws ParseException {
Expand Down

0 comments on commit 02e102c

Please sign in to comment.