private MethodDeclaration createSetup() { Map<FlowElementInput, Segment> segments = Maps.create(); List<Statement> statements = Lists.create(); // shuffle outputs for (Map.Entry<ShuffleModel.Segment, SimpleName> entry : shuffleNames.entrySet()) { ShuffleModel.Segment segment = entry.getKey(); SimpleName name = entry.getValue(); Name shuffleTypeName = segment.getCompiled().getCombineOutputType().getQualifiedName(); statements.add( new ExpressionBuilder(factory, factory.newThis()) .field(name) .assignFrom( new TypeBuilder(factory, importer.toType(shuffleTypeName)) .newObject(context) .toExpression()) .toStatement()); segments.put(segment.getPort(), segment); } // rendezvous for (Map.Entry<Fragment, SimpleName> entry : rendezvousNames.entrySet()) { Fragment fragment = entry.getKey(); Type rendezvousType = importer.toType(fragment.getCompiled().getQualifiedName()); List<Expression> arguments = Lists.create(); for (FlowElementInput input : fragment.getInputPorts()) { Segment segment = segments.get(input); assert segment != null; SimpleName shuffleName = shuffleNames.get(segment); assert shuffleName != null; arguments.add( new ExpressionBuilder(factory, factory.newThis()).field(shuffleName).toExpression()); } SimpleName name = entry.getValue(); statements.add( new ExpressionBuilder(factory, factory.newThis()) .field(name) .assignFrom( new TypeBuilder(factory, rendezvousType).newObject(arguments).toExpression()) .toStatement()); } return factory.newMethodDeclaration( null, new AttributeBuilder(factory).annotation(t(Override.class)).Public().toAttributes(), Collections.<TypeParameterDeclaration>emptyList(), t(void.class), factory.newSimpleName("setup"), Collections.singletonList( factory.newFormalParameterDeclaration( factory.newNamedType(factory.newSimpleName("Context")), context)), 0, Arrays.asList(t(IOException.class), t(InterruptedException.class)), factory.newBlock(statements)); }
private MethodDeclaration createGetConfiguration() { List<Statement> statements = Lists.create(); List<Expression> arguments = Lists.create(); arguments.add( new TypeBuilder(f, context.resolve(Charset.class)) .method("forName", Models.toLiteral(f, conf.getCharsetName())) .toExpression()); if (conf.isEnableHeader()) { SimpleName headers = f.newSimpleName("headers"); statements.add( new TypeBuilder(f, context.resolve(ArrayList.class)) .parameterize(context.resolve(String.class)) .newObject() .toLocalVariableDeclaration( new TypeBuilder(f, context.resolve(List.class)) .parameterize(context.resolve(String.class)) .toType(), headers)); for (PropertyDeclaration property : model.getDeclaredProperties()) { if (isValueField(property)) { String fieldName = CsvFieldTrait.getFieldName(property); statements.add( new ExpressionBuilder(f, headers) .method("add", Models.toLiteral(f, fieldName)) .toStatement()); } } arguments.add(headers); } else { arguments.add( new TypeBuilder(f, context.resolve(CsvConfiguration.class)) .field("DEFAULT_HEADER_CELLS") .toExpression()); } arguments.add(Models.toLiteral(f, conf.getTrueFormat())); arguments.add(Models.toLiteral(f, conf.getFalseFormat())); arguments.add(Models.toLiteral(f, conf.getDateFormat())); arguments.add(Models.toLiteral(f, conf.getDateTimeFormat())); statements.add( new TypeBuilder(f, context.resolve(CsvConfiguration.class)) .newObject(arguments) .toReturnStatement()); return f.newMethodDeclaration( new JavadocBuilder(f) .text("Returns this CSV format configuration.") .returns() .text("CSV format configuration") .toJavadoc(), new AttributeBuilder(f).Protected().toAttributes(), context.resolve(CsvConfiguration.class), f.newSimpleName(METHOD_CONFIG), Collections.<FormalParameterDeclaration>emptyList(), statements); }
private MethodDeclaration createReadFieldsMethod() { SimpleName in = factory.newSimpleName("in"); // $NON-NLS-1$ Expression segmentId = new ExpressionBuilder(factory, factory.newThis()) .field(SEGMENT_ID_FIELD_NAME) .toExpression(); List<Statement> statements = Lists.create(); statements.add( new ExpressionBuilder(factory, segmentId) .assignFrom( new ExpressionBuilder(factory, in) .method("readInt") // $NON-NLS-1$ .toExpression()) .toStatement()); List<Statement> cases = Lists.create(); for (Segment segment : model.getSegments()) { cases.add(factory.newSwitchCaseLabel(v(segment.getPortId()))); String fieldName = createPropertyName(segment); cases.add( segment .getTarget() .createReader( new ExpressionBuilder(factory, factory.newThis()) .field(fieldName) .toExpression(), in)); cases.add(factory.newBreakStatement()); } cases.add(factory.newSwitchDefaultLabel()); cases.add( new TypeBuilder(factory, t(AssertionError.class)) .newObject(segmentId) .toThrowStatement()); statements.add(factory.newSwitchStatement(segmentId, cases)); return factory.newMethodDeclaration( null, new AttributeBuilder(factory).annotation(t(Override.class)).Public().toAttributes(), Collections.<TypeParameterDeclaration>emptyList(), t(void.class), factory.newSimpleName("readFields"), // $NON-NLS-1$ Collections.singletonList(factory.newFormalParameterDeclaration(t(DataInput.class), in)), 0, Collections.singletonList(t(IOException.class)), factory.newBlock(statements)); }
private MethodDeclaration createGetRendezvous() { Map<FlowElement, SimpleName> fragments = Maps.create(); for (Map.Entry<Fragment, SimpleName> entry : rendezvousNames.entrySet()) { fragments.put(entry.getKey().getFactors().get(0).getElement(), entry.getValue()); } List<Statement> cases = Lists.create(); for (List<ShuffleModel.Segment> group : ShuffleEmiterUtil.groupByElement(shuffle)) { for (ShuffleModel.Segment segment : group) { cases.add(factory.newSwitchCaseLabel(v(segment.getPortId()))); } FlowElement element = group.get(0).getPort().getOwner(); SimpleName rendezvousName = fragments.get(element); if (rendezvousName == null) { cases.add( new ExpressionBuilder(factory, Models.toNullLiteral(factory)).toReturnStatement()); } else { cases.add( new ExpressionBuilder(factory, factory.newThis()) .field(rendezvousName) .toReturnStatement()); } } cases.add(factory.newSwitchDefaultLabel()); cases.add(new TypeBuilder(factory, t(AssertionError.class)).newObject().toThrowStatement()); SimpleName argument = names.create("nextKey"); List<Statement> statements = Lists.create(); statements.add( factory.newSwitchStatement( new ExpressionBuilder(factory, argument) .method(SegmentedWritable.ID_GETTER) .toExpression(), cases)); return factory.newMethodDeclaration( null, new AttributeBuilder(factory).annotation(t(Override.class)).Protected().toAttributes(), importer.resolve( factory.newParameterizedType( Models.toType(factory, Rendezvous.class), importer.toType(shuffle.getCompiled().getValueTypeName()))), factory.newSimpleName(SegmentedReducer.GET_RENDEZVOUS), Collections.singletonList( factory.newFormalParameterDeclaration( importer.toType(shuffle.getCompiled().getKeyTypeName()), argument)), statements); }
private List<FieldDeclaration> createProperties() { List<FieldDeclaration> results = Lists.create(); for (Segment segment : model.getSegments()) { results.add(createProperty(segment)); } return results; }
private MethodDeclaration createGetter(Segment segment) { assert segment != null; String methodName = Naming.getShuffleValueGetter(segment.getPortId()); List<Statement> statements = Lists.create(); statements.add( factory.newIfStatement( new ExpressionBuilder(factory, factory.newThis()) .field(SEGMENT_ID_FIELD_NAME) .apply(InfixOperator.NOT_EQUALS, v(segment.getPortId())) .toExpression(), new TypeBuilder(factory, t(AssertionError.class)).newObject().toThrowStatement(), null)); statements.add( new ExpressionBuilder(factory, factory.newThis()) .field(createPropertyName(segment)) .toReturnStatement()); return factory.newMethodDeclaration( new JavadocBuilder(factory) .text( "Return a data model object for {0}#{1}.", //$NON-NLS-1$ segment.getPort().getOwner().getDescription().getName(), segment.getPort().getDescription().getName()) .toJavadoc(), new AttributeBuilder(factory).Public().toAttributes(), t(segment.getTarget().getType()), factory.newSimpleName(methodName), Collections.<FormalParameterDeclaration>emptyList(), statements); }
private TypeDeclaration createType() { SimpleName name = factory.newSimpleName(Naming.getCombineClass()); importer.resolvePackageMember(name); List<TypeBodyDeclaration> members = Lists.create(); members.addAll(prepareFields()); members.add(createSetup()); members.add(createCleanup()); members.add(createGetRendezvous()); return factory.newClassDeclaration( new JavadocBuilder(factory) .text("ステージ{0}の処理を担当するコンバイナープログラム。", shuffle.getStageBlock().getStageNumber()) .toJavadoc(), new AttributeBuilder(factory) .annotation(t(SuppressWarnings.class), v("deprecation")) .Public() .Final() .toAttributes(), name, Collections.<TypeParameterDeclaration>emptyList(), importer.resolve( factory.newParameterizedType( Models.toType(factory, SegmentedCombiner.class), Arrays.asList( importer.toType(shuffle.getCompiled().getKeyTypeName()), importer.toType(shuffle.getCompiled().getValueTypeName())))), Collections.<Type>emptyList(), members); }
private List<FieldDeclaration> prepareFields() { List<FieldDeclaration> fields = Lists.create(); // shufles for (ShuffleModel.Segment segment : shuffle.getSegments()) { SimpleName shuffleName = names.create("shuffle"); shuffleNames.put(segment, shuffleName); Name shuffleTypeName = segment.getCompiled().getCombineOutputType().getQualifiedName(); fields.add( factory.newFieldDeclaration( null, new AttributeBuilder(factory).Private().toAttributes(), importer.toType(shuffleTypeName), shuffleName, null)); } // rendezvous for (ReduceUnit unit : reduceUnits) { if (unit.canCombine() == false) { continue; } Fragment first = unit.getFragments().get(0); SimpleName rendezvousName = names.create("combine"); rendezvousNames.put(first, rendezvousName); fields.add( factory.newFieldDeclaration( null, new AttributeBuilder(factory).Private().toAttributes(), importer.toType(first.getCompiled().getQualifiedName()), rendezvousName, null)); } return fields; }
private MethodDeclaration createCleanup() { List<Statement> statements = Lists.create(); for (SimpleName name : shuffleNames.values()) { statements.add( new ExpressionBuilder(factory, factory.newThis()) .field(name) .assignFrom(Models.toNullLiteral(factory)) .toStatement()); } for (SimpleName name : rendezvousNames.values()) { statements.add( new ExpressionBuilder(factory, factory.newThis()) .field(name) .assignFrom(Models.toNullLiteral(factory)) .toStatement()); } return factory.newMethodDeclaration( null, new AttributeBuilder(factory).annotation(t(Override.class)).Public().toAttributes(), Collections.<TypeParameterDeclaration>emptyList(), t(void.class), factory.newSimpleName("cleanup"), Collections.singletonList( factory.newFormalParameterDeclaration( factory.newNamedType(factory.newSimpleName("Context")), context)), 0, Arrays.asList(t(IOException.class), t(InterruptedException.class)), factory.newBlock(statements)); }
private MethodDeclaration createSlotNames() { SimpleName resultName = factory.newSimpleName("results"); List<Statement> statements = Lists.create(); statements.add( new TypeBuilder(factory, importer.toType(String.class)) .array(1) .newArray(getSlotCount()) .toLocalVariableDeclaration(importer.toType(String[].class), resultName)); for (ResolvedSlot slot : slots) { Expression outputName; if (slot.getSortProperties().isEmpty() && ParallelSortClientEmitter.legacy(environment) == false) { outputName = Models.toNullLiteral(factory); } else { outputName = Models.toLiteral(factory, slot.getSource().getOutputName()); } statements.add( new ExpressionBuilder(factory, resultName) .array(slot.getSlotNumber()) .assignFrom(outputName) .toStatement()); } statements.add(new ExpressionBuilder(factory, resultName).toReturnStatement()); return factory.newMethodDeclaration( null, new AttributeBuilder(factory) .annotation(importer.toType(Override.class)) .Protected() .toAttributes(), importer.toType(String[].class), factory.newSimpleName(SlotSorter.NAME_GET_OUTPUT_NAMES), Collections.<FormalParameterDeclaration>emptyList(), statements); }
private MethodDeclaration createSlotObjects() { SimpleName resultName = factory.newSimpleName("results"); List<Statement> statements = Lists.create(); statements.add( new TypeBuilder(factory, importer.toType(Writable.class)) .array(1) .newArray(getSlotCount()) .toLocalVariableDeclaration(importer.toType(Writable[].class), resultName)); for (ResolvedSlot slot : slots) { Expression object; if (slot.getSortProperties().isEmpty() && ParallelSortClientEmitter.legacy(environment) == false) { object = Models.toNullLiteral(factory); } else { DataClass slotClass = slot.getValueClass(); object = slotClass.createNewInstance(importer.toType(slotClass.getType())); } statements.add( new ExpressionBuilder(factory, resultName) .array(slot.getSlotNumber()) .assignFrom(object) .toStatement()); } statements.add(new ExpressionBuilder(factory, resultName).toReturnStatement()); return factory.newMethodDeclaration( null, new AttributeBuilder(factory) .annotation(importer.toType(Override.class)) .Protected() .toAttributes(), importer.toType(Writable[].class), factory.newSimpleName(SlotSorter.NAME_CREATE_SLOT_OBJECTS), Collections.<FormalParameterDeclaration>emptyList(), statements); }
private List<MethodDeclaration> createAccessors() { List<MethodDeclaration> results = Lists.create(); for (Segment segment : model.getSegments()) { results.add(createGetter(segment)); results.add(createSetter(segment)); } return results; }
private List<TypeBodyDeclaration> createMembers() { List<TypeBodyDeclaration> results = Lists.create(); results.add(createGetConfiguration()); results.add(createGetSupportedType()); results.add(createCreateReader()); results.add(createCreateWriter()); results.add(createReaderClass()); results.add(createWriterClass()); return results; }
private List<Source> sources(ModelDescription model, String... names) { List<Source> results = Lists.create(); Set<String> targets = Sets.create(); Collections.addAll(targets, names); for (Source s : model.getPropertiesAsSources()) { if (targets.contains(s.getName())) { results.add(s); } } return results; }
@Override public List<FieldDeclaration> getFields(EmitContext context, ModelDeclaration model) throws IOException { if (isTarget(model) == false) { return Collections.emptyList(); } CacheSupportTrait trait = model.getTrait(CacheSupportTrait.class); assert trait != null; List<FieldDeclaration> results = Lists.create(); if (trait.getDeleteFlagValue() != null) { results.add(createDeleteFlagValueField(context, model, trait.getDeleteFlagValue())); } return results; }
@Override public List<MethodDeclaration> getMethods(EmitContext context, ModelDeclaration model) throws IOException { if (isTarget(model) == false) { return Collections.emptyList(); } CacheSupportTrait trait = model.getTrait(CacheSupportTrait.class); assert trait != null; List<MethodDeclaration> results = Lists.create(); results.add(createModelVersionMethod(context, model, trait)); results.add(createTimestampColumnMethod(context, model, trait.getTimestamp())); results.add(createSystemIdMethod(context, model, trait.getSid())); results.add(createDeletedMethod(context, model, trait.getDeleteFlag())); return results; }
private AstRecord generateTerm() { List<AstPropertyDefinition> properties = Lists.create(); for (ModelProperty property : model.getProperties()) { properties.add( new AstPropertyDefinition( null, AstBuilder.getDesciption("{0}", property.getName()), Arrays.asList( new AstAttribute[] { AstBuilder.getOriginalName(property.getName()), }), AstBuilder.toName(property), AstBuilder.toType(property.getType()))); } return new AstRecordDefinition(null, properties); }
private MethodDeclaration createTimestampColumnMethod( EmitContext context, ModelDeclaration model, PropertySymbol timestamp) { assert context != null; assert model != null; assert timestamp != null; ModelFactory f = context.getModelFactory(); String name = OriginalNameEmitter.getOriginalName(timestamp.findDeclaration()); List<Statement> statements = Lists.create(); statements.add(new ExpressionBuilder(f, Models.toLiteral(f, name)).toReturnStatement()); return f.newMethodDeclaration( null, new AttributeBuilder(f).annotation(context.resolve(Override.class)).Public().toAttributes(), context.resolve(String.class), f.newSimpleName("__tgc__TimestampColumn"), Collections.<FormalParameterDeclaration>emptyList(), statements); }
private MethodDeclaration createModelVersionMethod( EmitContext context, ModelDeclaration model, CacheSupportTrait trait) { assert context != null; assert model != null; assert trait != null; ModelFactory f = context.getModelFactory(); List<Statement> statements = Lists.create(); statements.add( new ExpressionBuilder(f, Models.toLiteral(f, computeModelVersion(context, model, trait))) .toReturnStatement()); return f.newMethodDeclaration( null, new AttributeBuilder(f).annotation(context.resolve(Override.class)).Public().toAttributes(), context.resolve(long.class), f.newSimpleName("__tgc__DataModelVersion"), Collections.<FormalParameterDeclaration>emptyList(), statements); }
private MethodDeclaration createSystemIdMethod( EmitContext context, ModelDeclaration model, PropertySymbol sid) { assert context != null; assert model != null; assert sid != null; ModelFactory f = context.getModelFactory(); List<Statement> statements = Lists.create(); statements.add( new ExpressionBuilder(f, f.newThis()) .method(context.getValueGetterName(sid.findDeclaration())) .toReturnStatement()); return f.newMethodDeclaration( null, new AttributeBuilder(f).annotation(context.resolve(Override.class)).Public().toAttributes(), context.resolve(long.class), f.newSimpleName("__tgc__SystemId"), Collections.<FormalParameterDeclaration>emptyList(), statements); }
private MethodDeclaration createCreateWriter() { SimpleName path = f.newSimpleName("path"); SimpleName stream = f.newSimpleName("stream"); List<Statement> statements = Lists.create(); statements.add(createNullCheck(path)); statements.add(createNullCheck(stream)); SimpleName emitter = f.newSimpleName("emitter"); statements.add( new TypeBuilder(f, context.resolve(CsvEmitter.class)) .newObject( stream, path, new ExpressionBuilder(f, f.newThis()).method(METHOD_CONFIG).toExpression()) .toLocalVariableDeclaration(context.resolve(CsvEmitter.class), emitter)); statements.add( new TypeBuilder(f, f.newNamedType(f.newSimpleName(NAME_WRITER))) .newObject(emitter) .toReturnStatement()); MethodDeclaration decl = f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(context.resolve(Override.class)) .Public() .toAttributes(), Collections.<TypeParameterDeclaration>emptyList(), context.resolve( f.newParameterizedType( context.resolve(DataModelWriter.class), context.resolve(model.getSymbol()))), f.newSimpleName("createWriter"), Arrays.asList( f.newFormalParameterDeclaration(context.resolve(String.class), path), f.newFormalParameterDeclaration(context.resolve(OutputStream.class), stream)), 0, Arrays.asList(context.resolve(IOException.class)), f.newBlock(statements)); return decl; }
private TypeDeclaration createType() { SimpleName name = factory.newSimpleName(Naming.getShuffleValueClass()); importer.resolvePackageMember(name); List<TypeBodyDeclaration> members = Lists.create(); members.addAll(createSegmentDistinction()); members.addAll(createProperties()); members.addAll(createAccessors()); members.addAll(createWritables()); return factory.newClassDeclaration( createJavadoc(), new AttributeBuilder(factory) .annotation(t(SuppressWarnings.class), v("deprecation")) // $NON-NLS-1$ .Public() .Final() .toAttributes(), name, Collections.<TypeParameterDeclaration>emptyList(), null, Collections.singletonList(t(SegmentedWritable.class)), members); }
/** * 列挙をポート記述にマッピングして返す。 * * @param enumType マッピングする列挙 * @param ports マッピング先のポート一覧 * @return マッピング結果 * @throws IllegalArgumentException 引数に{@code null}が指定された場合 */ public static List<Tuple2<Enum<?>, FlowElementPortDescription>> extractConstants( Class<?> enumType, Collection<FlowElementPortDescription> ports) { Precondition.checkMustNotBeNull(enumType, "enumType"); // $NON-NLS-1$ Precondition.checkMustNotBeNull(ports, "ports"); // $NON-NLS-1$ Enum<?>[] constants = (Enum<?>[]) enumType.getEnumConstants(); if (constants == null) { throw new IllegalArgumentException( MessageFormat.format( "Cannot extract constants from {0}", //$NON-NLS-1$ enumType)); } Map<String, FlowElementPortDescription> portNames = Maps.create(); for (FlowElementPortDescription port : ports) { portNames.put(port.getName(), port); } List<Tuple2<Enum<?>, FlowElementPortDescription>> results = Lists.create(); for (Enum<?> constant : constants) { String name = JavaName.of(constant.name()).toMemberName(); FlowElementPortDescription port = portNames.get(name); if (port == null) { throw new IllegalStateException( MessageFormat.format( "Cannot extract {0} (in {1})", //$NON-NLS-1$ constant.name(), portNames)); } results.add(Tuples.<Enum<?>, FlowElementPortDescription>of(constant, port)); } if (ports.size() > results.size()) { throw new IllegalArgumentException( MessageFormat.format( "Cannot map constants to ports ({0} -> {1})", //$NON-NLS-1$ Arrays.asList(constants), ports)); } return results; }
private MethodDeclaration createDeletedMethod( EmitContext context, ModelDeclaration model, PropertySymbol deleteFlagOrNull) { assert context != null; assert model != null; ModelFactory f = context.getModelFactory(); List<Statement> statements = Lists.create(); if (deleteFlagOrNull == null) { statements.add(new ExpressionBuilder(f, Models.toLiteral(f, false)).toReturnStatement()); } else { statements.add( new ExpressionBuilder(f, f.newThis()) .method(context.getOptionGetterName(deleteFlagOrNull.findDeclaration())) .method("has", f.newSimpleName(FIELD_DELETE_FLAG_VALUE)) .toReturnStatement()); } return f.newMethodDeclaration( null, new AttributeBuilder(f).annotation(context.resolve(Override.class)).Public().toAttributes(), context.resolve(boolean.class), f.newSimpleName("__tgc__Deleted"), Collections.<FormalParameterDeclaration>emptyList(), statements); }
/** * Returns the corresponded DMDL model definition. * * @param model the thundergate model * @param extra extra attributes * @return the converted model * @throws IllegalArgumentException if some parameters were {@code null} * @since 0.2.3 */ public static AstModelDefinition<AstRecord> generate( TableModelDescription model, AstAttribute... extra) { if (model == null) { throw new IllegalArgumentException("model must not be null"); // $NON-NLS-1$ } if (extra == null) { throw new IllegalArgumentException("extra must not be null"); // $NON-NLS-1$ } List<AstAttribute> attrs = Lists.create(); attrs.add(AstBuilder.getAutoProjection()); attrs.add(AstBuilder.getNamespace(AstBuilder.toDmdlName(Constants.SOURCE_TABLE))); attrs.add(AstBuilder.getOriginalName(model.getReference().getSimpleName())); attrs.add(AstBuilder.getPrimaryKey(model)); Collections.addAll(attrs, extra); return new AstModelDefinition<>( null, ModelDefinitionKind.RECORD, AstBuilder.getDesciption("テーブル{0}", model.getReference().getSimpleName()), attrs, AstBuilder.toName(model.getReference()), new RecordModelGenerator(model).generateExpression()); }
private MethodDeclaration createSetter(Segment segment) { assert segment != null; String methodName = Naming.getShuffleValueSetter(segment.getPortId()); DataClass type = segment.getTarget(); SimpleName argument = factory.newSimpleName("model"); // $NON-NLS-1$ List<Statement> statements = Lists.create(); statements.add( new ExpressionBuilder(factory, factory.newThis()) .field(factory.newSimpleName(SEGMENT_ID_FIELD_NAME)) .assignFrom(v(segment.getPortId())) .toStatement()); statements.add( type.assign( new ExpressionBuilder(factory, factory.newThis()) .field(createPropertyName(segment)) .toExpression(), argument)); return factory.newMethodDeclaration( new JavadocBuilder(factory) .text( "Sets a data model object for " //$NON-NLS-1$ + "the successing operator input <code>{0}#{1}</code>.", //$NON-NLS-1$ segment.getPort().getOwner().getDescription().getName(), segment.getPort().getDescription().getName()) .param(argument) .text("the target data model object") // $NON-NLS-1$ .toJavadoc(), new AttributeBuilder(factory).Public().toAttributes(), t(void.class), factory.newSimpleName(methodName), Collections.singletonList( factory.newFormalParameterDeclaration(t(type.getType()), argument)), statements); }
private ClassDeclaration createReaderClass() { SimpleName parser = f.newSimpleName("parser"); List<TypeBodyDeclaration> members = Lists.create(); members.add(createPrivateField(CsvParser.class, parser)); List<ExpressionStatement> constructorStatements = Lists.create(); constructorStatements.add(mapField(parser)); if (hasFileName()) { members.add(createPrivateField(StringOption.class, f.newSimpleName(FIELD_PATH_NAME))); constructorStatements.add( new ExpressionBuilder(f, f.newSimpleName(FIELD_PATH_NAME)) .assignFrom( new TypeBuilder(f, context.resolve(StringOption.class)) .newObject( new ExpressionBuilder(f, parser).method("getPath").toExpression()) .toExpression()) .toStatement()); } members.add( f.newConstructorDeclaration( null, new AttributeBuilder(f).toAttributes(), f.newSimpleName(NAME_READER), Arrays.asList( f.newFormalParameterDeclaration(context.resolve(CsvParser.class), parser)), constructorStatements)); SimpleName object = f.newSimpleName("object"); List<Statement> statements = Lists.create(); statements.add( f.newIfStatement( new ExpressionBuilder(f, parser) .method("next") .apply(InfixOperator.EQUALS, Models.toLiteral(f, false)) .toExpression(), f.newBlock( new ExpressionBuilder(f, Models.toLiteral(f, false)).toReturnStatement()))); for (PropertyDeclaration property : model.getDeclaredProperties()) { switch (CsvFieldTrait.getKind(property, Kind.VALUE)) { case VALUE: statements.add( new ExpressionBuilder(f, parser) .method( "fill", new ExpressionBuilder(f, object) .method(context.getOptionGetterName(property)) .toExpression()) .toStatement()); break; case FILE_NAME: statements.add( new ExpressionBuilder(f, object) .method(context.getOptionSetterName(property), f.newSimpleName(FIELD_PATH_NAME)) .toStatement()); break; case LINE_NUMBER: statements.add( new ExpressionBuilder(f, object) .method( context.getValueSetterName(property), new ExpressionBuilder(f, parser) .method("getCurrentLineNumber") .toExpression()) .toStatement()); break; case RECORD_NUMBER: statements.add( new ExpressionBuilder(f, object) .method( context.getValueSetterName(property), new ExpressionBuilder(f, parser) .method("getCurrentRecordNumber") .toExpression()) .toStatement()); break; default: // ignored break; } } statements.add(new ExpressionBuilder(f, parser).method("endRecord").toStatement()); statements.add(new ExpressionBuilder(f, Models.toLiteral(f, true)).toReturnStatement()); members.add( f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(context.resolve(Override.class)) .Public() .toAttributes(), Collections.<TypeParameterDeclaration>emptyList(), context.resolve(boolean.class), f.newSimpleName("readTo"), Arrays.asList( f.newFormalParameterDeclaration(context.resolve(model.getSymbol()), object)), 0, Arrays.asList(context.resolve(IOException.class)), f.newBlock(statements))); return f.newClassDeclaration( null, new AttributeBuilder(f).Private().Static().Final().toAttributes(), f.newSimpleName(NAME_READER), null, Arrays.asList( f.newParameterizedType( context.resolve(DataModelReader.class), context.resolve(model.getSymbol()))), members); }
private ClassDeclaration createWriterClass() { SimpleName emitter = f.newSimpleName("emitter"); List<TypeBodyDeclaration> members = Lists.create(); members.add(createPrivateField(CsvEmitter.class, emitter)); members.add( f.newConstructorDeclaration( null, new AttributeBuilder(f).toAttributes(), f.newSimpleName(NAME_WRITER), Arrays.asList( f.newFormalParameterDeclaration(context.resolve(CsvEmitter.class), emitter)), Arrays.asList(mapField(emitter)))); SimpleName object = f.newSimpleName("object"); List<Statement> statements = Lists.create(); for (PropertyDeclaration property : model.getDeclaredProperties()) { if (isValueField(property)) { statements.add( new ExpressionBuilder(f, emitter) .method( "emit", new ExpressionBuilder(f, object) .method(context.getOptionGetterName(property)) .toExpression()) .toStatement()); } } statements.add(new ExpressionBuilder(f, emitter).method("endRecord").toStatement()); members.add( f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(context.resolve(Override.class)) .Public() .toAttributes(), Collections.<TypeParameterDeclaration>emptyList(), context.resolve(void.class), f.newSimpleName("write"), Arrays.asList( f.newFormalParameterDeclaration(context.resolve(model.getSymbol()), object)), 0, Arrays.asList(context.resolve(IOException.class)), f.newBlock(statements))); members.add( f.newMethodDeclaration( null, new AttributeBuilder(f) .annotation(context.resolve(Override.class)) .Public() .toAttributes(), Collections.<TypeParameterDeclaration>emptyList(), context.resolve(void.class), f.newSimpleName("flush"), Collections.<FormalParameterDeclaration>emptyList(), 0, Arrays.asList(context.resolve(IOException.class)), f.newBlock(new ExpressionBuilder(f, emitter).method("flush").toStatement()))); return f.newClassDeclaration( null, new AttributeBuilder(f).Private().Static().Final().toAttributes(), f.newSimpleName(NAME_WRITER), null, Arrays.asList( f.newParameterizedType( context.resolve(DataModelWriter.class), context.resolve(model.getSymbol()))), members); }
private List<TypeBodyDeclaration> createSegmentDistinction() { List<TypeBodyDeclaration> results = Lists.create(); results.add(createSegmentIdField()); results.add(createSegmentIdGetter()); return results; }
private List<TypeBodyDeclaration> createMembers() { List<TypeBodyDeclaration> results = Lists.create(); results.add(createGetModelType()); results.add(createGetStreamSupport()); return results; }