Search in sources :

Example 1 with BIterator

use of org.ballerinalang.model.values.BIterator in project ballerina by ballerina-lang.

the class BTestRunner method extractArguments.

/**
 * Extract function arguments from the values sets.
 * @param valueSets user provided value sets
 * @return a list of function arguments
 */
private List<BValue[]> extractArguments(BValue[] valueSets) {
    List<BValue[]> argsList = new ArrayList<>();
    for (BValue value : valueSets) {
        if (value instanceof BRefValueArray) {
            BRefValueArray array = (BRefValueArray) value;
            for (BIterator it = array.newIterator(); it.hasNext(); ) {
                BValue[] vals = it.getNext(0);
                if (vals[1] instanceof BNewArray) {
                    BNewArray bNewArray = (BNewArray) vals[1];
                    BValue[] args = new BValue[(int) bNewArray.size()];
                    for (int j = 0; j < bNewArray.size(); j++) {
                        args[j] = bNewArray.getBValue(j);
                    }
                    argsList.add(args);
                } else {
                // cannot happen due to validations done at annotation processor
                }
            }
        } else if (value instanceof BJSON) {
            BJSON jsonArrayOfArrays = (BJSON) value;
            for (BIterator it = jsonArrayOfArrays.newIterator(); it.hasNext(); ) {
                BValue[] vals = it.getNext(0);
                if (vals[1] instanceof BJSON) {
                    List<BValue> args = new ArrayList<>();
                    BJSON jsonArray = (BJSON) vals[1];
                    for (BIterator it2 = jsonArray.newIterator(); it2.hasNext(); ) {
                        BValue[] vals2 = it2.getNext(0);
                        args.add(vals2[1]);
                    }
                    argsList.add(args.toArray(new BValue[0]));
                } else {
                // cannot happen due to validations done at annotation processor
                }
            }
        } else {
            argsList.add(new BValue[] { value });
        }
    }
    return argsList;
}
Also used : BNewArray(org.ballerinalang.model.values.BNewArray) BValue(org.ballerinalang.model.values.BValue) ArrayList(java.util.ArrayList) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) ArrayList(java.util.ArrayList) List(java.util.List) BIterator(org.ballerinalang.model.values.BIterator) BJSON(org.ballerinalang.model.values.BJSON)

Example 2 with BIterator

use of org.ballerinalang.model.values.BIterator in project ballerina by ballerina-lang.

the class CPU method execIteratorOperation.

private static void execIteratorOperation(WorkerExecutionContext ctx, WorkerData sf, Instruction instruction) {
    int i, j;
    BCollection collection;
    BIterator iterator;
    InstructionIteratorNext nextInstruction;
    switch(instruction.getOpcode()) {
        case InstructionCodes.ITR_NEW:
            // collection
            i = instruction.getOperands()[0];
            // iterator variable (ref) index.
            j = instruction.getOperands()[1];
            collection = (BCollection) sf.refRegs[i];
            if (collection == null) {
                handleNullRefError(ctx);
                return;
            }
            sf.refRegs[j] = collection.newIterator();
            break;
        case InstructionCodes.ITR_HAS_NEXT:
            // iterator
            i = instruction.getOperands()[0];
            // boolean variable index to store has next result
            j = instruction.getOperands()[1];
            iterator = (BIterator) sf.refRegs[i];
            if (iterator == null) {
                sf.intRegs[j] = 0;
                return;
            }
            sf.intRegs[j] = iterator.hasNext() ? 1 : 0;
            break;
        case InstructionCodes.ITR_NEXT:
            nextInstruction = (InstructionIteratorNext) instruction;
            iterator = (BIterator) sf.refRegs[nextInstruction.iteratorIndex];
            if (iterator == null) {
                return;
            }
            BValue[] values = iterator.getNext(nextInstruction.arity);
            copyValuesToRegistries(nextInstruction.typeTags, nextInstruction.retRegs, values, sf);
            break;
    }
}
Also used : BValue(org.ballerinalang.model.values.BValue) InstructionIteratorNext(org.ballerinalang.util.codegen.Instruction.InstructionIteratorNext) BIterator(org.ballerinalang.model.values.BIterator) BCollection(org.ballerinalang.model.values.BCollection)

Example 3 with BIterator

use of org.ballerinalang.model.values.BIterator in project ballerina by ballerina-lang.

the class StructImpl method getArrayField.

@Override
public Value[] getArrayField(String fieldName) {
    final BNewArray refField = (BNewArray) value.getRefField(getFieldIndex(fieldName));
    if (refField == null) {
        return null;
    }
    List<Value> list = new ArrayList<>();
    final BIterator bIterator = refField.newIterator();
    while (bIterator.hasNext()) {
        list.add(ValueImpl.createValue(bIterator.getNext(1)[0]));
    }
    return list.toArray(new Value[0]);
}
Also used : BNewArray(org.ballerinalang.model.values.BNewArray) Value(org.ballerinalang.connector.api.Value) BTypeDescValue(org.ballerinalang.model.values.BTypeDescValue) BValue(org.ballerinalang.model.values.BValue) ArrayList(java.util.ArrayList) BIterator(org.ballerinalang.model.values.BIterator)

Example 4 with BIterator

use of org.ballerinalang.model.values.BIterator in project ballerina by ballerina-lang.

the class StructImpl method getMapField.

@Override
public Map<String, Value> getMapField(String fieldName) {
    final BMap refField = (BMap) value.getRefField(getFieldIndex(fieldName));
    if (refField == null) {
        return null;
    }
    Map<String, Value> valueMap = new LinkedHashMap<>();
    final BIterator bIterator = refField.newIterator();
    while (bIterator.hasNext()) {
        final BValue[] next = bIterator.getNext(2);
        valueMap.put(next[0].stringValue(), ValueImpl.createValue(next[1]));
    }
    return valueMap;
}
Also used : BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue) Value(org.ballerinalang.connector.api.Value) BTypeDescValue(org.ballerinalang.model.values.BTypeDescValue) BValue(org.ballerinalang.model.values.BValue) BIterator(org.ballerinalang.model.values.BIterator) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

BIterator (org.ballerinalang.model.values.BIterator)4 BValue (org.ballerinalang.model.values.BValue)4 ArrayList (java.util.ArrayList)2 Value (org.ballerinalang.connector.api.Value)2 BNewArray (org.ballerinalang.model.values.BNewArray)2 BTypeDescValue (org.ballerinalang.model.values.BTypeDescValue)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 BCollection (org.ballerinalang.model.values.BCollection)1 BJSON (org.ballerinalang.model.values.BJSON)1 BMap (org.ballerinalang.model.values.BMap)1 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)1 InstructionIteratorNext (org.ballerinalang.util.codegen.Instruction.InstructionIteratorNext)1