user1269701
user1269701

Reputation: 135

Collection for Generic types

How can I represent the generic types in a single collection?

public interface Rule<T> {
  String execute(T dataProvider);
}

public interface DataProvider {
   // No common functions as of now
}

public class RulesExecutor {
     private Map<String, List<Rule>> documentToRuleListMap;
    public RulesExecutor(Map<String, List<Rule>> documentToRuleListMap) {
        this.documentToRuleListMap = documentToRuleListMap;
    }

    public executeRules(DataProvider dataProvider, String document) {
      // some logic here 
    }
}

Is it possible to store a map as shown in RulesExecutor sconstructor?

If not, what is the correct data structure to store this info in?

Upvotes: 1

Views: 99

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Is it possible to store a map as shown in RulesExecutor sconstructor ? If not, what is the right data structure to store this info ?

Sure:

public interface Rule<T> {
  String execute(T dataProvider)
}


public interface DataProvider {

}


class RulesExecutor<T extends DataProvider> {

   public RulesExecutor(Map<String, List<Rule<T>>> documentToRuleListMap) {
   }

   public void executeRules(T dataProvider) {
     // some logic here 
   };
}

Please note that your posted code will not compile due to careless mistakes:

public interface Rule<T> {
  String execute(T dataProvider) // *** missing semicolon
}


public interface DataProvider {

}

public class RulesExecutor() { // *** class declarations have no parenthesis

Please be more careful when posting code and make sure to post real code, not sort-of, kind-of code. It makes it easier to understand your problem and help when you do.

Upvotes: 1

Related Questions