0

I have below class created.

public class SomeRequest<B extends Billing,E extends Employee, L extends Level>{


B billing;


E employee;

Class<L> level;

public void process(HashMap<String, Object> input){

        this.billing = (B)data.get("billing");
    this.employee= (E)data.get("employee");
    this.level = (Class<L>)data.get("level");

         //Some logic

  }

}

In above class Employee, Billing and Level are Java POJOs.

Now how can i instantiate above SomeRequest class?

Thanks!

user3269829
  • 131
  • 2
  • 11

3 Answers3

1

Assuming that SomeRequest class has a no-arg constructor

In Java 7 using the diamond operator

SomeRequest<Billing,Employee,Level> instance = new SomeRequest<>();

In Java 6 or 5

SomeRequest<Billing,Employee,Level> instance = new SomeRequest<Billing,Employee,Level>();
Community
  • 1
  • 1
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
  • Narendra, thanks for your answer. I just edited my question and am using Java7. – user3269829 Feb 14 '14 at 07:55
  • @user3269829 Glad to help. You should certainly use first option if you are using Java 7. – Narendra Pathai Feb 14 '14 at 07:57
  • Narendra, in the Hashmap, i have to pass two objects and one class type right? Thanks! – user3269829 Feb 14 '14 at 07:58
  • Can you elaborate a bit. I didn't get what you said. – Narendra Pathai Feb 14 '14 at 07:59
  • In the question i have a method which takes hash map as an input. In hash map i have to set two objects and one class type right? – user3269829 Feb 14 '14 at 08:01
  • Oh and looking at your profile seems that you are new to stackoverflow. Once you are satisfied with an answer you should accept the best answer as accepted. Read [faq](http://stackoverflow.com/help/accepted-answer) for more – Narendra Pathai Feb 14 '14 at 08:01
  • sure Narendra..i will accept it...could you please look into my process() which takes hash map as an input? – user3269829 Feb 14 '14 at 08:04
  • That should be a separate post and that too in [codereview.stackexchange.com](http://codereview.stackexchange.com/). Asking such questions about review are off topic at stackoverflow. Again something that can be learnt from FAQ. – Narendra Pathai Feb 14 '14 at 08:10
0

In Java 5 or 6 you can use factory method as replacement for constructor with generics. In Java 5 or 6 type inference work fine on methods but not on constructors. You can write something like:

public class SomeRequest<B extends Billing,E extends Employee, L extends Level>{
   private SomeRequest(B billing, E employee, L level) {
       // ...
   }

   public static <B,E,L> with(B billing, E employee, L level) {
        return new SomeRequest<B, E, L>(billing, employee, level);
   }
}

Now you should't write new SomeRequest(billing, employee, level), you can write simple SomeRequest.with(billing, employee, level).

Oh, sorry, I asked before have seen your comment about Java 7.

A. A. Vybegallo
  • 246
  • 1
  • 6
0

@user3269829 you should not use HashMap for contain data of different types. You can add custom container, i.e. a class with three field:

class Container<B extends Billing,E extends Employee, L extends Level> {

    B billing;

    E employee;

    L level;

    public Container(B b, E e, L l) {
        billing = b;
        employee = e;
        level = l;
    }

    // ... getters only!
}

The process() method will have next signature:

 public void process(Container<B, E, L> input)

In the process() method you can:

SomeRequest<Billing,Employee,Level> instance = new SomeRequest<>();
instance.setBilling(container.getBilling());
instance.setEmployee(container.getEmployee());
instance.setLevel(container.getLevel());

or

SomeRequest<Billing,Employee,Level> instance = new SomeRequest<>(container.getBilling(), container.getEmployee(), container.getLevel());
A. A. Vybegallo
  • 246
  • 1
  • 6