0

I have the below code to instantiate all the service on the start of the application. Sonar lint on the IndicesFactory is complaining for generic raw type. What is the best way to fix this?

public class IndicesFactory {

private final Map<String, IndicesStrategy> indicesMap;

public IndicesFactory(List<IndicesStrategy> indicesStrategyList) {
    this.indicesMap = new HashMap<>();
    indicesStrategyList.forEach(strategy -> this.indicesMap.put(strategy.getOperation().toString(), strategy));
}

private IndicesStrategy getStrategy(String operation) {
    return this.indicesMap.get(operation);
}


public <T, R> R execute(String operation, T t) {
    return (R) getStrategy(operation).execute(t);
}}

And my IndicesStrategy interface has below code

public interface IndicesStrategy<operation extends Enum<?>, T, R extends 
  IndicesResponse> {

R execute(T t);

operation getOperation();
}
Harini
  • 105
  • 3
  • 13
  • Does this answer your question? [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Tim Moore Jul 18 '23 at 22:18
  • @TimMoore I know what a raw type is. The problem I m facing is how to apply that to a generic class like the one I have – Harini Jul 18 '23 at 22:39
  • You need to provide values for the type parameters wherever you use the generic type. I’m not sure what values are right for your use case, because the question doesn’t describe it, but I would guess you want wildcards `?` – Tim Moore Jul 18 '23 at 23:21

1 Answers1

0

To get rid of the raw type warnings, values need to be provided for the type parameters of IndicesStrategy wherever that type is used in IndicesFactory. It appears that this class needs to handle different types of IndicesStrategy, so it can use type wildcards (?) as the values:

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IndicesFactory {

    private final Map<String, IndicesStrategy<?, ?, ?>> indicesMap;

    public IndicesFactory(List<IndicesStrategy<?, ?, ?>> indicesStrategyList) {
        this.indicesMap = new HashMap<>();
        indicesStrategyList.forEach(strategy ->
                this.indicesMap.put(strategy.getOperation().toString(), strategy));
    }

    private IndicesStrategy<?, ?, ?> getStrategy(String operation) {
        return this.indicesMap.get(operation);
    }


    public <T, R extends IndicesResponse> R execute(String operation, T t) {
        IndicesStrategy<?, T, R> strategy =
                (IndicesStrategy<?, T, R>) getStrategy(operation);
        return strategy.execute(t);
    }
}

As an alternative equivalent, you might prefer to move the unchecked cast into the getStrategy method:

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IndicesFactory {

    private final Map<String, IndicesStrategy<?, ?, ?>> indicesMap;

    public IndicesFactory(List<IndicesStrategy<?, ?, ?>> indicesStrategyList) {
        this.indicesMap = new HashMap<>();
        indicesStrategyList.forEach(strategy ->
                this.indicesMap.put(strategy.getOperation().toString(), strategy));
    }

    private <T, R extends IndicesResponse> IndicesStrategy<?, T, R> getStrategy(String operation) {
        return (IndicesStrategy<?, T, R>) this.indicesMap.get(operation);
    }


    public <T, R extends IndicesResponse> R execute(String operation, T t) {
        IndicesStrategy<?, T, R> strategy = getStrategy(operation);
        return strategy.execute(t);
    }
}
Tim Moore
  • 8,958
  • 2
  • 23
  • 34