Search in sources :

Example 6 with StringJoiner

use of java.util.StringJoiner in project requery by requery.

the class EntityGenerator method generateConstructors.

private void generateConstructors(TypeSpec.Builder builder) {
    // copy the existing constructors
    for (ExecutableElement constructor : ElementFilter.constructorsIn(typeElement.getEnclosedElements())) {
        // constructor params
        List<? extends VariableElement> parameters = constructor.getParameters();
        if (!parameters.isEmpty()) {
            MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder();
            constructorBuilder.addModifiers(constructor.getModifiers());
            List<String> parameterNames = new ArrayList<>();
            for (VariableElement parameter : parameters) {
                Modifier[] modifiers = parameter.getModifiers().toArray(new Modifier[parameter.getModifiers().size()]);
                String parameterName = parameter.getSimpleName().toString();
                parameterNames.add(parameterName);
                ParameterSpec.Builder parameterBuilder = ParameterSpec.builder(TypeName.get(parameter.asType()), parameterName, modifiers);
                constructorBuilder.addParameter(parameterBuilder.build());
            }
            // super parameter/arguments
            StringJoiner joiner = new StringJoiner(",", "(", ")");
            parameterNames.forEach(joiner::add);
            constructorBuilder.addStatement("super" + joiner.toString());
            builder.addMethod(constructorBuilder.build());
        }
    }
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) ArrayList(java.util.ArrayList) StringJoiner(java.util.StringJoiner)

Example 7 with StringJoiner

use of java.util.StringJoiner in project requery by requery.

the class EntityMetaGenerator method generateType.

private void generateType(TypeSpec.Builder builder, TypeName targetName) {
    CodeBlock.Builder block = CodeBlock.builder().add("new $T<$T>($T.class, $S)\n", TypeBuilder.class, targetName, targetName, entity.tableName());
    block.add(".setBaseType($T.class)\n", ClassName.get(typeElement)).add(".setCacheable($L)\n", entity.isCacheable()).add(".setImmutable($L)\n", entity.isImmutable()).add(".setReadOnly($L)\n", entity.isReadOnly()).add(".setStateless($L)\n", entity.isStateless()).add(".setView($L)\n", entity.isView());
    String factoryName = entity.classFactoryName();
    if (!Names.isEmpty(factoryName)) {
        block.add(".setFactory(new $L())\n", ClassName.bestGuess(factoryName));
    } else if (entity.isImmutable()) {
        // returns this class as the builder
        TypeSpec.Builder supplier = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Supplier.class, typeName));
        supplier.addMethod(CodeGeneration.overridePublicMethod("get").returns(typeName).addStatement("return new $T()", typeName).build());
        block.add(".setBuilderFactory($L)\n", supplier.build());
        MethodSpec.Builder applyMethod = CodeGeneration.overridePublicMethod("apply").addParameter(typeName, "value").returns(targetName);
        // add embedded builder calls
        entity.attributes().values().stream().filter(AttributeDescriptor::isEmbedded).forEach(attribute -> graph.embeddedDescriptorOf(attribute).ifPresent(embedded -> embedded.builderType().ifPresent(type -> {
            String fieldName = attribute.fieldName() + "Builder";
            String methodName = attribute.setterName();
            applyMethod.addStatement("value.builder.$L(value.$L.build())", methodName, fieldName);
        })));
        applyMethod.addStatement(entity.builderType().isPresent() ? "return value.builder.build()" : "return value.build()");
        TypeSpec.Builder buildFunction = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Function.class, typeName, targetName)).addMethod(applyMethod.build());
        block.add(".setBuilderFunction($L)\n", buildFunction.build());
    } else {
        TypeSpec.Builder typeFactory = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Supplier.class, targetName)).addMethod(CodeGeneration.overridePublicMethod("get").addStatement("return new $T()", targetName).returns(targetName).build());
        block.add(".setFactory($L)\n", typeFactory.build());
    }
    ParameterizedTypeName proxyType = parameterizedTypeName(EntityProxy.class, targetName);
    TypeSpec.Builder proxyProvider = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Function.class, targetName, proxyType));
    MethodSpec.Builder proxyFunction = CodeGeneration.overridePublicMethod("apply").addParameter(targetName, "entity").returns(proxyType);
    if (entity.isImmutable() || entity.isUnimplementable()) {
        proxyFunction.addStatement("return new $T(entity, $L)", proxyType, TYPE_NAME);
    } else {
        proxyFunction.addStatement("return entity.$L", PROXY_NAME);
    }
    proxyProvider.addMethod(proxyFunction.build());
    block.add(".setProxyProvider($L)\n", proxyProvider.build());
    if (entity.tableAttributes().length > 0) {
        StringJoiner joiner = new StringJoiner(",", "new String[] {", "}");
        for (String attribute : entity.tableAttributes()) {
            joiner.add("\"" + attribute + "\"");
        }
        block.add(".setTableCreateAttributes($L)\n", joiner.toString());
    }
    if (entity.tableUniqueIndexes().length > 0) {
        StringJoiner joiner = new StringJoiner(",", "new String[] {", "}");
        for (String attribute : entity.tableUniqueIndexes()) {
            joiner.add("\"" + attribute + "\"");
        }
        block.add(".setTableUniqueIndexes($L)\n", joiner.toString());
    }
    attributeNames.forEach(name -> block.add(".addAttribute($L)\n", name));
    expressionNames.forEach(name -> block.add(".addExpression($L)\n", name));
    block.add(".build()");
    ParameterizedTypeName type = parameterizedTypeName(Type.class, targetName);
    builder.addField(FieldSpec.builder(type, TYPE_NAME, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer("$L", block.build()).build());
}
Also used : EntityProxy(io.requery.proxy.EntityProxy) Modifier(javax.lang.model.element.Modifier) BooleanProperty(io.requery.proxy.BooleanProperty) FieldSpec(com.squareup.javapoet.FieldSpec) LongProperty(io.requery.proxy.LongProperty) VariableElement(javax.lang.model.element.VariableElement) ClassName(com.squareup.javapoet.ClassName) QueryAttribute(io.requery.meta.QueryAttribute) TypeBuilder(io.requery.meta.TypeBuilder) TypeElement(javax.lang.model.element.TypeElement) ReferentialAction(io.requery.ReferentialAction) HashSet(java.util.HashSet) Function(io.requery.util.function.Function) ByteProperty(io.requery.proxy.ByteProperty) Diagnostic(javax.tools.Diagnostic) Type(io.requery.meta.Type) FloatProperty(io.requery.proxy.FloatProperty) ElementFilter(javax.lang.model.util.ElementFilter) LinkedList(java.util.LinkedList) CodeBlock(com.squareup.javapoet.CodeBlock) Property(io.requery.proxy.Property) MethodSpec(com.squareup.javapoet.MethodSpec) IntProperty(io.requery.proxy.IntProperty) ExecutableElement(javax.lang.model.element.ExecutableElement) Supplier(io.requery.util.function.Supplier) Set(java.util.Set) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) IOException(java.io.IOException) Attribute(io.requery.meta.Attribute) PropertyState(io.requery.proxy.PropertyState) TypeSpec(com.squareup.javapoet.TypeSpec) Order(io.requery.query.Order) QueryExpression(io.requery.meta.QueryExpression) TypeKind(javax.lang.model.type.TypeKind) CascadeAction(io.requery.CascadeAction) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Cardinality(io.requery.meta.Cardinality) DoubleProperty(io.requery.proxy.DoubleProperty) StringJoiner(java.util.StringJoiner) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) ShortProperty(io.requery.proxy.ShortProperty) MethodSpec(com.squareup.javapoet.MethodSpec) TypeBuilder(io.requery.meta.TypeBuilder) CodeBlock(com.squareup.javapoet.CodeBlock) Function(io.requery.util.function.Function) Supplier(io.requery.util.function.Supplier) StringJoiner(java.util.StringJoiner) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 8 with StringJoiner

use of java.util.StringJoiner in project goci by EBISPOT.

the class StudyUpdateService method generateUpdateDescription.

/**
     * Generate a description of update made
     *
     * @param existingStudy Study based on whats stored in database
     * @param study         Updated study
     */
private String generateUpdateDescription(Study existingStudy, Study study) {
    String updateDescription = null;
    List<String> updateDetails = new ArrayList<>();
    // Check disease trait for changes
    String existingDiseaseTraitName = null;
    if (existingStudy.getDiseaseTrait() != null && existingStudy.getDiseaseTrait().getTrait() != null) {
        existingDiseaseTraitName = existingStudy.getDiseaseTrait().getTrait();
    }
    String updatedDiseaseTraitName = null;
    if (study.getDiseaseTrait() != null && study.getDiseaseTrait().getTrait() != null) {
        updatedDiseaseTraitName = study.getDiseaseTrait().getTrait();
    }
    String diseaseTraitUpdateDescription = checkForDiseaseTraitUpdate(existingDiseaseTraitName, updatedDiseaseTraitName);
    if (diseaseTraitUpdateDescription != null) {
        updateDetails.add(diseaseTraitUpdateDescription);
    }
    // Check EFO trait for changes
    List<String> existingStudyEfoTraits = new ArrayList<>();
    List<String> updatedStudyEfoTraits = new ArrayList<>();
    existingStudyEfoTraits = existingStudy.getEfoTraits().stream().filter(efoTrait -> efoTrait.getTrait() != null).filter(efoTrait -> !efoTrait.getTrait().isEmpty()).map(efoTrait -> efoTrait.getTrait()).collect(Collectors.toList());
    if (study.getEfoTraits() != null) {
        updatedStudyEfoTraits = study.getEfoTraits().stream().filter(efoTrait -> !efoTrait.getTrait().isEmpty()).map(efoTrait -> efoTrait.getTrait()).collect(Collectors.toList());
    }
    String efoTraitUpdateDescription = checkForEfoTraitUpdate(existingStudyEfoTraits, updatedStudyEfoTraits);
    if (efoTraitUpdateDescription != null) {
        updateDetails.add(efoTraitUpdateDescription);
    }
    StringJoiner updateDetailsJoiner = new StringJoiner(", ");
    if (!updateDetails.isEmpty()) {
        updateDetails.forEach(s -> updateDetailsJoiner.add(s));
        updateDescription = updateDetailsJoiner.toString();
    }
    return updateDescription;
}
Also used : Logger(org.slf4j.Logger) EventType(uk.ac.ebi.spot.goci.model.EventType) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Housekeeping(uk.ac.ebi.spot.goci.model.Housekeeping) Collectors(java.util.stream.Collectors) TrackingOperationService(uk.ac.ebi.spot.goci.service.TrackingOperationService) ArrayList(java.util.ArrayList) List(java.util.List) Service(org.springframework.stereotype.Service) StringJoiner(java.util.StringJoiner) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Study(uk.ac.ebi.spot.goci.model.Study) StudyRepository(uk.ac.ebi.spot.goci.repository.StudyRepository) SecureUser(uk.ac.ebi.spot.goci.model.SecureUser) ArrayList(java.util.ArrayList) StringJoiner(java.util.StringJoiner)

Example 9 with StringJoiner

use of java.util.StringJoiner in project jdk8u_jdk by JetBrains.

the class MergeTest method testSimple.

public void testSimple() {
    StringJoiner sj = new StringJoiner(",", "{", "}");
    StringJoiner other = new StringJoiner(",", "[", "]");
    Stream.of("a", "b", "c").forEachOrdered(sj::add);
    Stream.of("d", "e", "f").forEachOrdered(other::add);
    sj.merge(other);
    assertEquals(sj.toString(), "{a,b,c,d,e,f}");
}
Also used : StringJoiner(java.util.StringJoiner)

Example 10 with StringJoiner

use of java.util.StringJoiner in project jdk8u_jdk by JetBrains.

the class MergeTest method testCascadeEmpty.

public void testCascadeEmpty() {
    StringJoiner sj = new StringJoiner(",", "{", "}");
    StringJoiner o1 = new StringJoiner(":", "[", "]").setEmptyValue("Empty1");
    StringJoiner o2 = new StringJoiner(",", "<", ">").setEmptyValue("Empty2");
    o1.merge(o2);
    assertEquals(o1.toString(), "Empty1");
    sj.merge(o1);
    assertEquals(sj.toString(), "{}");
}
Also used : StringJoiner(java.util.StringJoiner)

Aggregations

StringJoiner (java.util.StringJoiner)94 ArrayList (java.util.ArrayList)20 List (java.util.List)10 HashSet (java.util.HashSet)6 Map (java.util.Map)6 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Collectors (java.util.stream.Collectors)4 ClassName (com.squareup.javapoet.ClassName)3 FieldSpec (com.squareup.javapoet.FieldSpec)3 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)3 TypeName (com.squareup.javapoet.TypeName)3 TypeSpec (com.squareup.javapoet.TypeSpec)3 Expression (com.sri.ai.expresso.api.Expression)3 Attribute (io.requery.meta.Attribute)3 Scanner (java.util.Scanner)3 RaptorColumnHandle (com.facebook.presto.raptor.RaptorColumnHandle)2 Range (com.facebook.presto.spi.predicate.Range)2 MaterializedResult (com.facebook.presto.testing.MaterializedResult)2 MaterializedRow (com.facebook.presto.testing.MaterializedRow)2