Java Code Examples for com.sun.codemodel.JBlock#forEach()

The following examples show how to use com.sun.codemodel.JBlock#forEach() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FastSerializerGenerator.java    From avro-util with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void processMap(final Schema mapSchema, JExpression mapExpr, JBlock body) {
  final JClass mapClass = schemaAssistant.classFromSchema(mapSchema);
  JClass keyClass = schemaAssistant.findStringClass(mapSchema);

  body.invoke(JExpr.direct(ENCODER), "writeMapStart");

  final JExpression emptyMapCondition = mapExpr.eq(JExpr._null()).cor(JExpr.invoke(mapExpr, "isEmpty"));
  final JConditional emptyMapIf = body._if(emptyMapCondition);
  final JBlock emptyMapBlock = emptyMapIf._then();
  emptyMapBlock.invoke(JExpr.direct(ENCODER), "setItemCount").arg(JExpr.lit(0));

  final JBlock nonEmptyMapBlock = emptyMapIf._else();
  nonEmptyMapBlock.invoke(JExpr.direct(ENCODER), "setItemCount").arg(JExpr.invoke(mapExpr, "size"));

  final JForEach mapKeysLoop = nonEmptyMapBlock.forEach(keyClass, getUniqueName("key"),
      JExpr.invoke(JExpr.cast(mapClass, mapExpr), "keySet"));

  final JBlock forBody = mapKeysLoop.body();
  forBody.invoke(JExpr.direct(ENCODER), "startItem");

  JVar keyStringVar;
  if (SchemaAssistant.hasStringableKey(mapSchema)) {
    keyStringVar =
        forBody.decl(codeModel.ref(String.class), getUniqueName("keyString"), mapKeysLoop.var().invoke("toString"));
  } else {
    keyStringVar = mapKeysLoop.var();
  }

  final Schema valueSchema = mapSchema.getValueType();

  forBody.invoke(JExpr.direct(ENCODER), "writeString").arg(keyStringVar);

  JVar containerVar;
  if (SchemaAssistant.isComplexType(valueSchema)) {
    containerVar = declareValueVar(valueSchema.getName(), valueSchema, forBody);
    forBody.assign(containerVar, JExpr.invoke(JExpr.cast(mapClass, mapExpr), "get").arg(mapKeysLoop.var()));

    processComplexType(valueSchema, containerVar, forBody);
  } else {
    processSimpleType(valueSchema, mapExpr.invoke("get").arg(mapKeysLoop.var()), forBody);
  }
  body.invoke(JExpr.direct(ENCODER), "writeMapEnd");
}
 
Example 2
Source File: FastSerializerGenerator.java    From avro-fastserde with Apache License 2.0 4 votes vote down vote up
private void processMap(final Schema mapSchema, JExpression mapExpr, JBlock body) {

        final JClass mapClass = schemaAssistant.classFromSchema(mapSchema);
        JClass keyClass = schemaAssistant.keyClassFromMapSchema(mapSchema);

        body.invoke(JExpr.direct(ENCODER), "writeMapStart");

        final JExpression emptyMapCondition = mapExpr.eq(JExpr._null())
                .cor(JExpr.invoke(mapExpr, "isEmpty"));
        final JConditional emptyMapIf = body._if(emptyMapCondition);
        final JBlock emptyMapBlock = emptyMapIf._then();
        emptyMapBlock.invoke(JExpr.direct(ENCODER), "setItemCount").arg(JExpr.lit(0));

        final JBlock nonEmptyMapBlock = emptyMapIf._else();
        nonEmptyMapBlock.invoke(JExpr.direct(ENCODER), "setItemCount")
                .arg(JExpr.invoke(mapExpr, "size"));

        final JForEach mapKeysLoop = nonEmptyMapBlock.forEach(keyClass, getVariableName("key"),
                JExpr.invoke(JExpr.cast(mapClass, mapExpr), "keySet"));

        final JBlock forBody = mapKeysLoop.body();
        forBody.invoke(JExpr.direct(ENCODER), "startItem");

        JVar keyStringVar;
        if (SchemaAssistant.hasStringableKey(mapSchema)) {
            keyStringVar = forBody.decl(string, getVariableName("keyString"),
                    mapKeysLoop.var().invoke("toString"));
        } else {
            keyStringVar = mapKeysLoop.var();
        }

        final Schema valueSchema = mapSchema.getValueType();

        forBody.invoke(JExpr.direct(ENCODER), "writeString").arg(keyStringVar);

        JVar containerVar;
        if (SchemaAssistant.isComplexType(valueSchema)) {
            containerVar = declareValueVar(valueSchema.getName(), valueSchema, forBody);
            forBody.assign(containerVar, JExpr.invoke(JExpr.cast(mapClass, mapExpr), "get").arg(mapKeysLoop.var()));

            processComplexType(valueSchema, containerVar, forBody);
        } else {
            processSimpleType(valueSchema, mapExpr.invoke("get").arg(mapKeysLoop.var()), forBody);
        }
        body.invoke(JExpr.direct(ENCODER), "writeMapEnd");
    }
 
Example 3
Source File: OperationProcessor.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private void createFormatMethod(JDefinedClass clazz, JPrimitiveType type){
	JCodeModel codeModel = clazz.owner();

	JClass numberClazz = codeModel.ref(Number.class);
	JClass stringBuilderClazz = codeModel.ref(StringBuilder.class);

	JMethod method = clazz.method(JMod.STATIC | JMod.PRIVATE, String.class, "format");

	JVar valuesParameter = method.varParam(numberClazz, "values");

	JBlock body = method.body();

	JVar sbVariable = body.decl(stringBuilderClazz, "sb", JExpr._new(stringBuilderClazz).arg(valuesParameter.ref("length").mul(JExpr.lit(32))));

	JForEach forStatement = body.forEach(numberClazz, "value", valuesParameter);

	JBlock forBody = forStatement.body();

	forBody.add(createReportInvocation(clazz, sbVariable, "${0}", Collections.singletonList(forStatement.var()), type));

	body._return(sbVariable.invoke("toString"));
}