Home > database >  "Make this anonymous inner class a lambda" code smell in sonar lint
"Make this anonymous inner class a lambda" code smell in sonar lint

Time:05-16

While checking up the sonar lint report, I am getting this code smell "Make this anonymous inner class a lambda" number of times. How can I make this inner cass as lambda ,it is bit challenging for me How do I resolve this, these are my affected codes.

Aggregation agg = Aggregation.newAggregation(

                new AggregationOperation() {

                    @Override
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(addFields, new Document(resourceDetails, new Document(arrayElemAt,
                                Arrays.asList(new Document(objToArray, rootResourseDetails), 1L))));
                    }

                }, new AggregationOperation() {

                    @Override
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(addFields, new Document(resourceDetails, resourceDeatilsV));
                    }

                }, LookupOperation.newLookup().from(resourceDetailsStr).localField(resourceDetails).foreignField("_id")
                        .as(resourceDetails),
                new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(addFields, new Document(resourceDetails,
                                new Document(arrayElemAt, Arrays.asList(resourcedetailsTempString, 0L))));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(addFields, new Document(domainStr, new Document(arrayElemAt,
                                Arrays.asList(new Document(objToArray, resourceDetailsDomain), 1L))));
                    }
                }

                , new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(addFields, new Document(domainStr, domainV));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(lookup, new Document("from", "infra_asset")
                                .append("let", new Document(assetIdStr, assetRefId).append(domainStr, rootDomain))
                                .append(pipeLineStr,
                                        Arrays.asList(new Document(match, new Document(expr, new Document("$and",
                                                Arrays.asList(new Document("$eq", Arrays.asList(domain, infra)),
                                                        new Document("$eq", Arrays.asList("$_id", assetId))))))))
                                .append("as", "infraAssest"));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(lookup, new Document("from", "application_asset")
                                .append("let", new Document(assetIdStr, assetRefId).append(domainStr, rootDomain))
                                .append(pipeLineStr,
                                        Arrays.asList(new Document(match, new Document(expr, new Document("$and",
                                                Arrays.asList(new Document("$eq", Arrays.asList(domain, "Application")),
                                                        new Document("$eq", Arrays.asList("$_id", assetId))))))))
                                .append("as", applicationAsset));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(project, new Document(resourceDetails, 1L).append(resourceThresholdStr, 1L)
                                .append(customFieldStr, new Document(cond, new Document("if",
                                        new Document("$eq", Arrays.asList(rootDomain, infra)))
                                        .append("then", rootInfraAsset)
                                        .append("else", new Document(cond,
                                                new Document("if",
                                                        new Document("$eq", Arrays.asList(rootDomain, application)))
                                                        .append("then", rootApplicationAsset)
                                                        .append("else", "null"))))));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(addFields, new Document(customFieldStr,
                                new Document(arrayElemAt, Arrays.asList(customField, 0L))));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(match, new Document(expr, new Document("$and",
                                Arrays.asList(new Document("$eq", Arrays.asList(customFieldPlatformName, platformName)),
                                        new Document("$eq", Arrays.asList(customFieldAssetName, assetName))))));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(addFields, new Document(moduleStr, new Document(arrayElemAt,
                                Arrays.asList(new Document(objToArray, resourceDetailModule), 1L))));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(addFields, new Document(moduleStr, moduleV));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(lookup, new Document("from", modulesStr).append(localFieldStr, moduleStr)
                                .append(foreignFieldStr, "_id").append("as", moduleStr));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(addFields,
                                new Document(moduleStr, new Document(arrayElemAt, Arrays.asList(module, 0L))));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(match,
                                new Document(expr, new Document("$eq", Arrays.asList(moduleString, moduleName))));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {

                        return new Document(addFields, new Document(resourceThresholdStr, new Document(arrayElemAt,
                                Arrays.asList(new Document(objToArray, "$$ROOT.resourceThreshold"), 1L))));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(addFields, new Document(resourceThresholdStr, "$resourceThreshold.v"));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(lookup,
                                new Document("from", "resource_threshold").append(localFieldStr, resourceThresholdStr)
                                        .append(foreignFieldStr, "_id").append("as", resourceThresholdStr));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(addFields, new Document(resourceThresholdStr,
                                new Document(arrayElemAt, Arrays.asList("$resourceThreshold", 0L))));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(addFields, new Document(platformNameStr, customFieldPlatformName)
                                .append("asset_name", customFieldAssetName).append(moduleNameStr, moduleString));
                    }
                }, new AggregationOperation() {
                    public Document toDocument(AggregationOperationContext context) {
                        return new Document(project, new Document(moduleNameStr, 1L).append(resourceThresholdStr, 1L)
                                .append(platformNameStr, 1L).append("asset_name", 1L));
                    }
                }

        );
 

every block starting from new AggregationOperation is affected.

CodePudding user response:

Instead of writing

new AggregationOperation() {

    @Override
    public Document toDocument(AggregationOperationContext context) {
        return new Document(addFields, new Document(resourceDetails, new Document(arrayElemAt,
        Arrays.asList(new Document(objToArray, rootResourseDetails), 1L))));
    }
}

You should just be able to write

context -> new Document(addFields, new Document(resourceDetails, new Document(arrayElemAt,
        Arrays.asList(new Document(objToArray, rootResourseDetails), 1L))))

and similarly for every AggregationOperation that you create.

This is because the compiler can figure out that you need an AggregationOperation and understands that an AggregationOperation has only one method. That's why you can replace the anonymous AggregationOperation with the lambda equivalent of that method.

You might like to look into whether your IDE can do this refactoring for you automatically.

  • Related