String (boolean generateTerminalOperation) { StringBuilder optionalCall = new StringBuilder("Optional."); int maxOperations = myEnv.generateValue(Generator.integers(2, 4), null); int curDepth = this.curDepth++; Operation operation = chooseOperation(Operation.SourceOperation.sourceOps()); for (int nOperation = 1; nOperation <= maxOperations; nOperation++) { boolean isTerminalOperation = nOperation == maxOperations && generateTerminalOperation; if (isTerminalOperation) { operation = chooseOperation(Operation.TerminalOperation.terminalOps()); } else if (curDepth >= maxDepth) { while (operation.isNested()) { operation = chooseOperation(operation.possibleNextOperations()); } } optionalCall.append(operation.generate(this)); operation = chooseOperation(operation.possibleNextOperations()); } String code = optionalCall.toString(); if (!generateTerminalOperation) return code; return ((Operation.TerminalOperation)operation).wrapInUsageContext(this, code); }