1

I need to count a specific Object, but I'll know which object only at runtime. Right now I have something like

public class Details {
    private String typeOfObjectRequired;
    private int numberOfObjectRequired;
}

And in another class I have

public class Container {
    private List<Type1> type1List;
    private List<Type2> type2List;
    private Type3 type3Object;

    public int countType1() {
        return type1List.size();
    }

    public int countType2() {
        return type2List.size();
    }

    public int countType3() {
        return type3Object.getNumberOfSomething();
    }

}

Now I'm doing like this (in a third class that has both Details and Container as attributes)

public boolean hasNumberOfObjectRequired() {
    int count = 0;
    String type = details.getTypeOfObjectRequired();

    if(type.equals("type1")) count = container.countType1();
    else if (type.equals("type2")) count = container.countType2();
    else if (type.equals("type3")) count = container.countType3();

    if (count > details.getNumberOfObJectRequired) return true;
    return false;
}

Is there a better way to do this? I don't like to have so many if, also because I have more than just 3 different types.

EDIT: Right now I have 5 different types, and I always need only one of them. Basically I want to call different methods based on the String

Vince
  • 14,470
  • 7
  • 39
  • 84
sciamp-dev
  • 157
  • 1
  • 10
  • Can you elaborate a bit more on what you are trying to achieve? How many different types do you thing you'll have? 10? Thousands? Will you have one field per type? Are you ever going to want a compound `Details`, like "has number of objects required" that demands two of `Type1` and four of `Type3`? With your question as vague as it is, it will likely be closed as "too broad" or "unclear". – AJNeufeld May 31 '17 at 21:21
  • Don't compare strings with `==`. Use `.equals`. Check out [How to compare String](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Vince May 31 '17 at 21:43
  • @AJNeufeld I added some information, I don't think I can be more specific than that – sciamp-dev May 31 '17 at 21:55
  • @VinceEmigh Yeah, thanks for the remainder. I'm used to other languages.. – sciamp-dev May 31 '17 at 21:58
  • @Champ I updated my answer for an easier approach that doesn't require lambdas. Hope it helps. – Vince Jun 01 '17 at 01:14

2 Answers2

1

The Container class could contain a Map of the lists it's composed of:

class Container {
    private Map<String, List<?>> lists = new HashMap<>();

    private List<TypeOne> first = ...;
    private List<TypeTwo> second = ...;

    public Container() {
        lists.put("type1", first);
        lists.put("type2", second);
    }

    public int count(String type) {
        return lists.get(type).size();
    }
}

You can grab the size based on the type by calling count:

public boolean hasNumberOfObjectRequired() {
    String type = details.getTypeOfObjectRequired();
    int requiredCount = details.getNumberOfObjectRequired();

    return container.count(type) >= requiredCount;
}
Vince
  • 14,470
  • 7
  • 39
  • 84
  • Thanks for the help! I like the solution with lambdas, too, it helps me familiarize with functional programming – sciamp-dev Jun 01 '17 at 10:40
0

You can use reflection...

public boolean hasNumberOfObjectRequired() {
int count = 0; 
String type = details.getTypeOfObjectRequired(); 
Method m = Container.class.getMethod("countType"+type.charAt(4));
return m.invoke(container) > details.getNumberOfObJectRequired);
}

Or you can use a switch

switch(type){
case "type1":
count = ...
break;
case "type2"
....
}

Even better if type is a int instead of a string

dade.sliep
  • 137
  • 1
  • 5
  • How is this better? Seems like unnecessary use of reflection – Vince May 31 '17 at 21:53
  • Reflection seems a bit overkill, I don't know if it's actually better.. Concatenated if and switch are the same thing, so the problem remains – sciamp-dev May 31 '17 at 22:02
  • I just showed a few possible solutions. I know in this case reflection is unnecessary but it's also better then the original static solution. If you have a better one please tell us – dade.sliep May 31 '17 at 22:04
  • 1
    @dade.sliep Can check out my answer. Easier to scale when you maintain a table. Also ensures `O(1)` access. Switch cases do use tables under the hood, but they don't really look clean when scaled up. – Vince May 31 '17 at 22:09
  • 1
    @Champ ok reflection is overkill, but switch using int instead of string is much better than nested if – dade.sliep May 31 '17 at 22:10