1

I have method

public List getListaDataTable(param1, param2, param3)

Method must call to others method depends witch params is null. If I do it with if ... else I going to do 7 condition. Any idea how resolve this?

THX

Mathew Rock
  • 989
  • 2
  • 14
  • 32
  • 1
    Are all seven conditions relevant? –  Jan 26 '15 at 09:09
  • 1
    Are those params of the same type or different types? – Eran Jan 26 '15 at 09:09
  • Different type and are all condition relevant – Mathew Rock Jan 26 '15 at 09:10
  • You can use overloading, but then the caller of the method would have to know which parameters are not null in order to call the correct overloaded method. – Eran Jan 26 '15 at 09:11
  • Encapsulate your parameters into a request object, add 7 boolean functions inside this request object that represent your 7 conditions that you need to check for. – hofan41 Jan 26 '15 at 09:14
  • it all depends how you want to use your parameters, maybe you could use varargs and filter out null values add more details so we could find better solution for your case – user902383 Jan 26 '15 at 09:21

2 Answers2

1
    char[] code = {'0', '0', '0'};
    if (param1 != null) code[0] = '1';
    if (param2 != null) code[1] = '1';
    if (param3 != null) code[2] = '1';

    String codeString = String.copyValueOf(code);
    switch (codeString) {
      case "000":
        //all are null
        break;
      case "100":
        //param1 is not null
      ...

    }
Vitaly
  • 2,552
  • 2
  • 18
  • 21
0

You can surcharge your function with no null parameters

public List getListaDataTable(param1, param2, param3, param4, param5, param6, param7)
{
    // Do function A
    // Do function C

public List getListaDataTable(param1, param2, param3, param4, param7)
{
    // Do function B
    // Do function C
Phil
  • 47
  • 1
  • 10
  • i don't think it is scalable solution, and second thing, you still need to evaluate parameters to find which is null to find which method you need to call – user902383 Jan 26 '15 at 09:30
  • We have not enought informations to détemine it, parameters are not typed, mathieu not use an array, i think the types arent not identiques, – Phil Jan 26 '15 at 09:36