Cannot convert from Collection<> to List<>

user0000001

I am trying to validate a result from a library (Panache / Hibernate using Quarkus) by passing the object to a "validator", where the validator validates that the result is not empty and subsequently returns the passed object back to the caller.

In this example, listAll() is from PanacheEntityBase and simply returns a list of Employee objects. If this List is empty, as in, there are no results in the result set, it does not throw an exception. So to avoid having an empty / null check in every call, I am writing the follow convenience class to validate all results.

In my validator:

public class ResultValidator {

    public static <T> Collection<T> validate(Collection<T> result, Class<T> type) throws EmptyResultException {
        if (result.isEmpty()) {
            throw new EmptyResultException();
        }

        return result;
    }
}

The caller would invoke this like:

return ResultValidator.validate(listAll(), Employee.class);

Rather, I'm having to upcast:

return (List<Employee>) ResultValidator.validate(listAll(), Employee.class);

The code above, that invokes validate, results in a compiler error Cannot convert from Collection<> to List<>. Unless I'm upcasting to List<> or explicitly defining List<> type in my validate implementation, I will get this error.

What do I need to change in my validate(Collection, Class) implementation to avoid upcasting or declaring an explicit type?

rzwitserloot

If your intent is to return 'self', then:

public static <T, C extends Collection<T>> C validate(C result, Class<T> type) {
   // note that requiring a `Class<T>` is a code smell...

   if (result.isEmpty()) throw new EmptyResultException();
   return result;
}

List<String> list = ...;
list = validate(list, String.class);

now works fine, but note that it is not possible to make a new C instance. You're either returning result straight up, and therefore the fact that validate returns at all is, at best, some code/API convenience but not inherently useful, or, you can't use this, and you'd need a 'make a new collection' factory. That would get you to:


public static <T, C extends Collection<T>> C validate(C result, Class<T> type, Supplier<C> collectionMaker) {
 ... 
}

Where the caller would have to provide something. e.g.:

List<String> list = ...;
validate(list, String.class, () -> new ArrayList<String>());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot convert from 'System.Generic.Collection.List'

Cannot convert from List<List> to List<List<?>>

Cannot convert from List<Bar> to List<Foo>

Cannot Convert Type System.Collection.Generic.List<T>

Type mismatch: cannot convert from ArrayList to List

Cannot convert from 'initializer-list' to UserController

How to convert a Collection to List?

Convert Collection to List

Convert to Array or Collection or List

how to add object directly to nested ienumerable collection object group by Error- Cannot convert List to Collection

How to convert IGroupMembersCollectionWithReferencesPage from Azure AD to list/collection in c#?

c# - Cannot convert from List<DateTime?> to List<dynamic>

2 errors - Cannot implicitly convert type system.collection.generic.list<float> to float and cannot implicitly convert type float [] to float

Cannot Implicitly convert type "System.Collection.Generic.List<T>" to "T"

Collections.sort: cannot convert from void to List

Cannot convert to struct from brace-enclosed initializer list

Type mismatch: cannot convert from element type Object to List

"Cannot convert argument from Class<T>* to Class<T> &&" on list insert

Type mismatch: cannot convert from List<String> to ArrayList<String>

Error : Type mismatch: cannot convert from List<Integer> to ArrayList<Integer>

Type mismatch: cannot convert from Object to List<User>

Kotlin - Convert Collection to Array List

how to convert object to collection or list

How to convert a counter collection into a list

Convert Array From Laravel Collection

How to convert a Collection<Collection<Double>> to a List<List<Double>>?

Cannot Convert IList To Binding List

Cannot convert Django Queryset to a List

can not convert from System.Collection.Generic.List<Dal.Questio> to Dal.Questio