输出集合对象到语句内容, 集合将被展开成 IN (...) 语法。

PS: IN :varlist --> IN (?, ?, ...)

@param collection - 输出的集合
552501
private void fillCollection(Collection<?> collection) {

        int count = 0;

        // 展开  Collection 容器, 输出逗号分隔以支持 IN (...) 语法
        // "IN :varlist" --> "IN (?, ?, ...)"
        if (collection.isEmpty()) {

            // 输出  "IN (NULL)" 保证不会产生错误
            builder.append(NULL);

        } else {

            // 输出逗号分隔的参数表
            for (Object value : collection) {

                if (value != null) {

                    if (count > 0) {
                        builder.append(COMMA);
                    }

                    // 输出参数内容
                    addArg(value);

                    builder.append(QUESTION);

                    count++;
                }
            }
        }
    }
936625
private List<String> copyToList(Collection<String> collection) {
 
        if ((collection == null) || collection.isEmpty()) {
            return null;
        }

       return Collections.unmodifiableList(new ArrayList<String>(collection));
    }
826994
@SuppressWarnings("unchecked")
    public static <E> void mergeArrayIntoCollection(Object array, Collection<E> collection) {
        if (collection == null) {
            throw new IllegalArgumentException("Collection must not be null");
        }
        Object[] arr = ObjectUtils.toObjectArray(array);
        for (Object elem : arr) {
            collection.add((E)elem);
        }
    }
620675
public static <T> List<T> convertCollectionToList(final Collection<T> collection) {

        if(List.class.isAssignableFrom(collection.getClass())) {
            return (List<T>)collection;
        }

        return new ArrayList<>(collection);

    }
1024709
private void writeOutTypeCollection(BytecodeContext bc) throws TransformerException {

	GeneratorAdapter adapter = bc.getAdapter();

	// VariableReference item=VariableInterpreter.getVariableReference(pc,index);
	int index = -1;
	Attribute attrIndex = getAttribute("index");
	if (attrIndex == null) attrIndex = getAttribute("key");
	if (attrIndex != null) {
	    index = adapter.newLocal(Types.VARIABLE_REFERENCE);
	    adapter.loadArg(0);
	    attrIndex.getValue().writeOut(bc, Expression.MODE_REF);
	    adapter.invokeStatic(Types.VARIABLE_INTERPRETER, GET_VARIABLE_REFERENCE);
	    adapter.storeLocal(index);
	}

	// VariableReference item=VariableInterpreter.getVariableReference(pc,item);
	int item = -1;
	Attribute attrItem = getAttribute("item");
	if (attrItem == null) attrItem = getAttribute("value");
	if (attrItem != null) {
	    item = adapter.newLocal(Types.VARIABLE_REFERENCE);
	    adapter.loadArg(0);
	    attrItem.getValue().writeOut(bc, Expression.MODE_REF);
	    adapter.invokeStatic(Types.VARIABLE_INTERPRETER, GET_VARIABLE_REFERENCE);
	    adapter.storeLocal(item);
	}
	boolean hasIndexAndItem = index != -1 && item != -1;
	boolean hasItem = item != -1;

	WhileVisitor whileVisitor = new WhileVisitor();
	loopVisitor = whileVisitor;
	// java.util.Iterator it=Caster.toIterator(@collection');
	int it = adapter.newLocal(Types.ITERATOR);
	Attribute coll = getAttribute("struct");
	if (coll == null) coll = getAttribute("collection");
	coll.getValue().writeOut(bc, Expression.MODE_REF);

	// item and index
	int entry = -1;
	if (hasIndexAndItem) {
	    entry = adapter.newLocal(Types.MAP_ENTRY);
	    // Caster.toCollection(collection)
	    adapter.invokeStatic(Types.CASTER, Methods_Caster.TO_COLLECTION);
	    // coll.entryIterator();
	    adapter.invokeInterface(Types.COLLECTION, ENTRY_ITERATOR);
	}
	else {
	    // if(hasItem) adapter.invokeStatic(ForEach.FOR_EACH_UTIL,ForEach.FOR_EACH);
	    // else
	    adapter.invokeStatic(ForEach.FOR_EACH_UTIL, ForEach.LOOP_COLLECTION);
	}

	adapter.storeLocal(it);

	// while(it.hasNext()) {
	whileVisitor.visitBeforeExpression(bc);
	adapter.loadLocal(it);
	adapter.invokeInterface(Types.ITERATOR, HAS_NEXT);

	whileVisitor.visitAfterExpressionBeforeBody(bc);
	if (hasIndexAndItem) {
	    // entry=it.next();
	    adapter.loadLocal(it);
	    adapter.invokeInterface(Types.ITERATOR, NEXT);
	    adapter.storeLocal(entry);

	    // keyRef.set(pc,entry.getKey())
	    adapter.loadLocal(index);
	    adapter.loadArg(0);
	    adapter.loadLocal(entry);
	    adapter.invokeInterface(Types.MAP_ENTRY, GET_KEY);
	    adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_STRING);
	    adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
	    adapter.pop();

	    // valueRef.set(pc,entry.getKey())
	    adapter.loadLocal(item);
	    adapter.loadArg(0);
	    adapter.loadLocal(entry);
	    adapter.invokeInterface(Types.MAP_ENTRY, GET_VALUE);
	    adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
	    adapter.pop();

	}
	else {
	    if (index == -1) adapter.loadLocal(item);
	    else adapter.loadLocal(index);

	    adapter.loadArg(0);
	    adapter.loadLocal(it);
	    adapter.invokeInterface(Types.ITERATOR, NEXT);

	    adapter.invokeVirtual(Types.VARIABLE_REFERENCE, SET);
	    adapter.pop();
	}
	getBody().writeOut(bc);
	whileVisitor.visitAfterBody(bc, getEnd());

	// Reset
	adapter.loadLocal(it);
	adapter.invokeStatic(ForEach.FOR_EACH_UTIL, ForEach.RESET);
    }
664369
function _setCollectionFrom(COLLECTION) {
	  _export(_export.S, COLLECTION, { from: function from(source /* , mapFn, thisArg */) {
	      var mapFn = arguments[1];
	      var mapping, A, n, cb;
	      _aFunction(this);
	      mapping = mapFn !== undefined;
	      if (mapping) _aFunction(mapFn);
	      if (source == undefined) return new this();
	      A = [];
	      if (mapping) {
	        n = 0;
	        cb = _ctx(mapFn, arguments[2], 2);
	        _forOf(source, false, function (nextItem) {
	          A.push(cb(nextItem, n++));
	        });
	      } else {
	        _forOf(source, false, A.push, A);
	      }
	      return new this(A);
	    } });
	}
1371403
function(collection, fn) {
        if ( Array.isArray(collection) ) {
          var result = [];

          for (var i = 0; i < collection.length; i++) {
            result[i] = fn(collection[i], i, collection);
          }

          return result;
        }
        else if ( Object.prototype.toString.call(collection) === '[object Object]' ) {
          var result = {};

          for (var key in collection) {
            if ( collection.hasOwnProperty(key) ) {
              result[key] = fn(collection[key]);
            }
          }

          return result;
        }
        else throw new TypeError('Invalid type. First argument must be an array or an object.');
      }
923933
@Override
	protected List<OUT> executeOnCollections(List<IN> inputData, RuntimeContext ctx, ExecutionConfig executionConfig) throws Exception {
		MapFunction<IN, OUT> function = this.userFunction.getUserCodeObject();
		
		FunctionUtils.setFunctionRuntimeContext(function, ctx);
		FunctionUtils.openFunction(function, this.parameters);
		
		ArrayList<OUT> result = new ArrayList<OUT>(inputData.size());

		TypeSerializer<IN> inSerializer = getOperatorInfo().getInputType().createSerializer(executionConfig);
		TypeSerializer<OUT> outSerializer = getOperatorInfo().getOutputType().createSerializer(executionConfig);

		for (IN element : inputData) {
			IN inCopy = inSerializer.copy(element);
			OUT out = function.map(inCopy);
			result.add(outSerializer.copy(out));
		}

		FunctionUtils.closeFunction(function);
		
		return result;
	}
113426
@Override
	protected List<IN> executeOnCollections(List<IN> inputData, RuntimeContext runtimeContext, ExecutionConfig executionConfig) {
		return inputData;
	}
1313134
public static <T> List<T> sub(Collection<T> collection, int start, int end) {
		return sub(collection, start, end, 1);
	}
1316528
public static String toMultiRowString(Collection<?> collection) {
        if (collection == null) {
            return null;
        }
        if (collection.isEmpty()) {
            return "(empty collection)";
        }
        StringBuilder builder = new StringBuilder("\n  - ");
        for (Iterator<?> iterator = collection.iterator(); iterator.hasNext();) {
            builder.append(iterator.next());
            if (iterator.hasNext()) {
                builder.append(",\n  - ");
            }
        }
        return builder.toString();
    }
1091852
public String joinEx(Object collection, String delim, String prefix, String suffix) {
		if (collection == null) {
			return StringUtils.collectionToDelimitedString(Collections.emptyList(), delim, prefix, suffix);
		} else if (collection instanceof Collection) {
			return StringUtils.collectionToDelimitedString((Collection<?>) collection, delim, prefix, suffix);
		} else if (collection.getClass().isArray()) {
			return StringUtils.collectionToDelimitedString(CollectionUtils.arrayToList(collection), delim, prefix, suffix);
		} else {
			return StringUtils.collectionToDelimitedString(Collections.singletonList(collection), delim, prefix, suffix);
		}
	}
926613
public static Object getElementFromCollection(Collection<Object> collection, int index) {
    if (collection.size() < index + 1)
      return null;

    if (collection instanceof List)
      return ((List<Object>) collection).get(index);

    Iterator<Object> iterator = collection.iterator();
    for (int i = 0; i < index; i++) {
      iterator.next();
    }
    return iterator.next();
  }
774282
public TemplateVariables concat(Collection<TemplateVariable> variables) {

		List<TemplateVariable> result = new ArrayList<>(this.variables.size() + variables.size());
		result.addAll(this.variables);

		List<TemplateVariable> filtered = variables.stream() //
				.filter(variable -> !containsEquivalentFor(variable)).collect(Collectors.toList());

		result.addAll(filtered);

		return new TemplateVariables(result);
	}
290332
@Override
	protected List<T> executeOnCollections(List<T> inputData, RuntimeContext ctx, ExecutionConfig executionConfig) throws Exception {
		// make sure we can handle empty inputs
		if (inputData.isEmpty()) {
			return Collections.emptyList();
		}

		ReduceFunction<T> function = this.userFunction.getUserCodeObject();

		UnaryOperatorInformation<T, T> operatorInfo = getOperatorInfo();
		TypeInformation<T> inputType = operatorInfo.getInputType();

		int[] inputColumns = getKeyColumns(0);

		if (!(inputType instanceof CompositeType) && inputColumns.length > 1) {
			throw new InvalidProgramException("Grouping is only possible on composite types.");
		}

		FunctionUtils.setFunctionRuntimeContext(function, ctx);
		FunctionUtils.openFunction(function, this.parameters);

		TypeSerializer<T> serializer = getOperatorInfo().getInputType().createSerializer(executionConfig);

		if (inputColumns.length > 0) {
			boolean[] inputOrderings = new boolean[inputColumns.length];
			TypeComparator<T> inputComparator = inputType instanceof AtomicType
					? ((AtomicType<T>) inputType).createComparator(false, executionConfig)
					: ((CompositeType<T>) inputType).createComparator(inputColumns, inputOrderings, 0, executionConfig);

			Map<TypeComparable<T>, T> aggregateMap = new HashMap<TypeComparable<T>, T>(inputData.size() / 10);

			for (T next : inputData) {
				TypeComparable<T> wrapper = new TypeComparable<T>(next, inputComparator);

				T existing = aggregateMap.get(wrapper);
				T result;

				if (existing != null) {
					result = function.reduce(existing, serializer.copy(next));
				} else {
					result = next;
				}

				result = serializer.copy(result);

				aggregateMap.put(wrapper, result);

			}

			FunctionUtils.closeFunction(function);
			return new ArrayList<T>(aggregateMap.values());
		}
		else {
			T aggregate = inputData.get(0);
			
			aggregate = serializer.copy(aggregate);

			for (int i = 1; i < inputData.size(); i++) {
				T next = function.reduce(aggregate, serializer.copy(inputData.get(i)));
				aggregate = serializer.copy(next);
			}

			FunctionUtils.setFunctionRuntimeContext(function, ctx);
			
			return Collections.singletonList(aggregate);
		}
	}
181837
public static <T> Collection<T> immutableCollectionCopy(Collection<T> collection) {
      if (collection == null) return null;
      if (collection.isEmpty()) return Collections.emptySet();
      if (collection.size() == 1) return Collections.singleton(collection.iterator().next());

      Collection<? extends T> copy = ObjectDuplicator.duplicateCollection(collection);
      if (copy == null)
         copy = attemptCopyConstructor(collection, Collection.class);
      if (copy == null)
         copy = new ArrayList<>(collection);

      return new ImmutableCollectionWrapper<>(copy);
   }
141057
@Override
	public <L extends Collection<E>> L parseCollection(BinderContext context, ParserWrapper parserWrapper, L collection)  throws Exception {
		switch (context.getSupportedFormat()) {
		case XML: {
			throw (new KriptonRuntimeException(context.getSupportedFormat() + " context does not support direct collection persistance"));
		}
		default: {
			JacksonWrapperParser wrapperParser = (JacksonWrapperParser) parserWrapper;
			JsonParser parser = wrapperParser.jacksonParser;

			try {
				collection.clear();

				if (parser.nextToken() != JsonToken.START_ARRAY) {
					throw (new KriptonRuntimeException("Invalid input format"));
				}
				if (context.getSupportedFormat().onlyText) {
					while (parser.nextToken() != JsonToken.END_ARRAY) {
						collection.add(parseOnJacksonAsString(parser));
					}
				} else {
					while (parser.nextToken() != JsonToken.END_ARRAY) {
						collection.add(parseOnJackson(parser));
					}
				}
				return collection;
			} catch (IOException e) {
				e.printStackTrace();
				throw (new KriptonRuntimeException(e));
			}
		}
		}
	}