0

I have the following peace of code:

public class LazyProductDataModel<P extends Product> extends LazyDataModel<P> { 
    private List<P> data = new ArrayList<P>();
    private SearchProductsCriteria<P> criteria;

    public LazyProductDataModel(SearchProductsCriteria<P> criteria) {
        this.criteria = criteria;
    }

    public SearchProductsCriteria<P> getCriteria() {
        return criteria;
    }

    public void setCriteria(SearchProductsCriteria<P> criteria) {
        this.criteria = criteria;
    }

    @Override
    public List<P> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
        if (criteria == null) {
            return data;
        }

        int currentPage = first==0 ? 1 : first >= pageSize ? (first / pageSize) + 1 : 0;

        criteria.setPageNumber(currentPage);
        criteria.setPageSize(pageSize);

        try {
            if (criteria.getProductId() != null && criteria.getProductId() != 0) {
                P product = ServiceClientFactory.getInstance().
                        getProductRetrieveClient(criteria.getProductClass()).getProduct(criteria.getProductId());
                if (product != null) {
                    data.add(product);
                    this.setRowCount(1);
                }

            } else {
                LazyDataResponse<P> lazyResponse = ServiceClientFactory.getInstance().
                        getProductSearchClient(criteria.getProductClass()).searchProductsLazyResponse(criteria); 
                data = lazyResponse.getList();
                this.setRowCount(lazyResponse.getTotalRecordsCount());
            }

        } catch (Exception e) {
            LOGGER.error("Exception happened at LazyProductDataModel#load.", e);
            data = new ArrayList<P>();
            this.setRowCount(0);
        }

        return data;
    }
}

3rd line in the try block, getProductRetrieveClient(criteria.getProductClass()).getProduct(criteria.getProductId()); I am calling criteria.getProductClass(), this returns a Class<P>, and as you see in the class above, P extends Product, which means that I should be able to pass any class that extends Product, but in my class I am trying to use it but its not going as expected:

Here is how I am trying to use it, please help I'm new to the generics topic.

@ManagedBean
@ViewScoped
public class ReportController implements Serializable {

    private ProductReportCriteria<? extends Product> criteria = new ProductReportCriteria<>();

    public ReportController() {

    }

    @PostConstruct
    public void init() {
        criteria.setLocale(userToolKit.getLocale());
        criteria.setProductClass(Movie.class);
    }

    public ProductReportCriteria<?> getCriteria() {
        return criteria;
    }

    public void setCriteria(ProductReportCriteria<? extends Product> criteria) {
        this.criteria = criteria;
    }
}

in the last method onFilter inside the if statement, I am getting a compilation error,

The method setProductClass(Class<capture#9-of ? extends Product>) in the type SearchProductsCriteria<capture#9-of ? extends Product> is not applicable for the arguments (Class<Movie>)

public class Movie extends Product implements Serializable {
}
public class Product implements Serializable {
}

Finally,

public class ProductReportCriteria<P extends Product> extends SearchProductsCriteria<P> implements Serializable {
}

public class SearchProductsCriteria<P> implements Serializable {
    private Class<P> productClass;
    public Class<P> getProductClass() {
        return productClass;
    }
    public void setProductClass(Class<P> productClass) {
        this.productClass = productClass;
    }
}
Khaled
  • 529
  • 8
  • 19

1 Answers1

2

ProductReportCriteria<? extends Product> criteria doesn't mean anything that extends Product, it means a specific thing that extends Product, the compiler just doesn't know what.

A simpler example is List<? extends Number>: that doesn't mean "a list containing any subclass of Number": you can't add an Integer to such a list (list.add(Integer.valueOf(0))), even though Integer extends Number. This is because a List<Double> is a List<? extends Number>, and it would be problematic if you could add an Integer to a List<Double>:

List<Double> doubles = new ArrayList<>();
List<? extends Number> numbers = doubles;  // Fine.
numbers.add(Integer.valueOf(0));           // Doesn't work; pretend it does.
Double d = doubles.get(0);                 // ClassCastException!

If you want criteria's consumer methods to accept any instance of Product, change its type to either of:

ProductReportCriteria<? super Product>
ProductReportCriteria<Product>
ProductReportCriteria<?>       // Same as Product, because of declared bounds

Read up about PECS.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • I am still getting a compilation error, I have tried the three ways and still getting the same compilation error – Khaled Apr 20 '20 at 15:21
  • 1
    It’s not clear why you think, `ProductReportCriteria>` was the same as `ProductReportCriteria`. The `>` still denotes “*a **specific** thing that extends `Product`, the compiler just doesn't know what*”. In fact, for most practical purposes, `ProductReportCriteria>` is just a short-hand for `ProductReportCriteria extends Product>`. The ` super Product>` also denotes a specific but unknown type, but since it’s used at a place where only subtypes of `Product` are allowed, `Product` is the only type which can fulfill ` super Product>` and `

    ` at once.

    – Holger Apr 22 '20 at 16:19