throws CALExecutorException {
checkInputRecordImplementation(executor);
// The two arguments in evaluated form will be on top of the stack, in reverse order.
final NRecordValue recordDictionary = (NRecordValue) executor.internalEvaluate(arguments[0]);
//inputMap, but with the keys sorted in FieldName order.
final SortedMap<?, ?> fieldNameSortedInputMap;
{
final Map<?, ?> inputMap = (Map<?, ?>)((NValObject)executor.internalEvaluate(arguments[1])).getValue();
//In the case when inputMap is in fact a SortedMap that is using the comparator on the keys, then we can just use it directly.
//We still need to verify that the keys are in fact FieldNames, but that will be done later.
//Otherwise we need to copy the map to get a proper iteration order.
if (inputMap instanceof SortedMap && ((SortedMap<?, ?>)inputMap).comparator() == null) {
fieldNameSortedInputMap = (SortedMap<?, ?>)inputMap;
} else {
fieldNameSortedInputMap = new TreeMap<Object, Object>(inputMap);
}
}
final List<String> fieldNames = recordDictionary.fieldNames();
final int nFields = fieldNames.size();
//check that the number of fields in the input map is the same as the number of fields in the record.
//without this check it is possible that input map could have more elements than the size of the record and still succeed.
//This would still "work" but this check is useful to alert clients to potential bugs in their code.
if (nFields != fieldNameSortedInputMap.size()) {
throw new IllegalArgumentException("A Java list of size " + fieldNameSortedInputMap.size() + " cannot be input to a record with " + nFields + " fields.");
}
final Iterator<?> iterator = fieldNameSortedInputMap.entrySet().iterator();
// Create a record value.
final NRecordValue newRecord = new NRecordValue (nFields);
// The order of iteration over the fields is only import to check that fieldnames from the input map and from the record dictionary
//(i.e. the CAL type) are the same. Evaluation is not being done. We're just pushing suspensions into the NRecordValue node.
for (int i = 0; i < nFields; ++i) {
final Map.Entry<?, ?> entry = (Map.Entry<?, ?>)iterator.next();
final String fieldNameFromInputMap = ((FieldName)entry.getKey()).getCalSourceForm();
final String fieldName = fieldNames.get(i);
if (!fieldNameFromInputMap.equals(fieldName)) {
throw new IllegalArgumentException("The field names of the input map and target record must match exactly.");
}
newRecord.putValue (fieldName, recordDictionary.getValue(fieldName).apply (new NValObject(entry.getValue())));
}
return newRecord;
}