2

I am not sure can we write the generic method as a function programming using Java 8 lamda expression as below

Generic method

public <T> MongoCollection<T> getCollection(String collectionName, Class<T> typeParameterClass) {
    return mongoClient
            .getDatabase(mongodbConfiguration.getDatabase())
            .getCollection(collectionName, typeParameterClass);
}

I want to convert this generic method to generic functional lamda expression as below

public BiFunction<String, Class<T>, T> getCollection = (collectionName, typeParameterClass) -> {
    return mongoClient
            .getDatabase(mongodbConfiguration.getDatabase())
            .getCollection(collectionName, typeParameterClass);
};

From the above code I have an error on type T and typeParameterClass

enter image description here

Update 1

While implementing the method suggested by @Clashsoft the second argumenent is Class<Object> instead of Class<T>

repository.getCollectionFactory().apply("", Category.class);

enter image description here

1
  • Your signature should at minimum be BiFunction<String, Class<T>, MongoCollection<T>> (though I don't think that'll work either), as the return type is MongoCollection<T>, and not T. Commented Feb 19, 2021 at 9:45

2 Answers 2

2

That's not possible with a field. You can however create another method / getter with a type parameter:

public <T> BiFunction<String, Class<T>, MongoCollection<T>> getCollectionFactory() {
    return  (collectionName, typeParameterClass) -> {
        return mongoClient
                .getDatabase(mongodbConfiguration.getDatabase())
                .getCollection(collectionName, typeParameterClass);
    };
};

Keep in mind you have to specify the type when calling the method:

repository.<Category>.getCollectionFactory().apply("test", Category.class)

Or use a variable with an explicit type:

BiFunction<String, Class<Category>, MongoCollection<Category>> factory = repository.getCollectionFactory();
factory.apply("test", Category.class)

Of course, you can already do this with the regular old getCollection method:

BiFunction<String, Class<Category>, MongoCollection<Category>> factory = repository::getCollection;

So the usefulness of the factory method is questionable.

Sign up to request clarification or add additional context in comments.

7 Comments

I think that should be BiFunction<String, Class<T>, MongoCollection<T>> since the function returns a MongoCollection<T>.
@khelwood thanks, I copied that part from the question. Fixed!
@Clashsoft While implementing this method repository.getCollectionFactory().apply("test" ,Category.class)); the second argument is Class<Object> but it should be Class<T>
@SanJaisy yes that's a problem you'll have a lot with this approach. You need to specify the type argument in the getCollectionFactory call: repository.<Category>.getCollectionFactory().apply("test", Category.class). That's why I'd try to stick with the regular method.
It's possible with a field if the class is generic no?
|
1

The BiFunction has wrong the third type (the result type) that should be MongoCollection<T>:

public BiFunction<String, Class<T>, MongoCollection<T>> getCollection = (name, type) -> 
    mongoClient
        .getDatabase(mongodbConfiguration.getDatabase())
        .getCollection(name, type);

Also note the T must be the class generic parameter as long as getCollection is no longer a method but a field.

public class ClassHavingGetCollectionField<T> {
   ...
}

... otherwise you need to define a method with a generic type returning such functional interface implementation:

public <T> BiFunction<String, Class<T>, MongoCollection<T>> getCollectionBiFunction() {
    return (name, type) ->
         mongoClient
             .getDatabase(mongodbConfiguration.getDatabase())
             .getCollection(name, type);
}

1 Comment

This doesn't work, what is the type for T ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.