Each service already has a List in which the "parent" is stored (the services it depends on). Thus, the size of this list is the number of direct parents. Since you also want to go further and find indirect dependencies, you can do this by asking each parent service how many services it depends on.
The code looks something like this:
public int getCountOfDependencies() { int theCount = 0; for (Service nxtService : dependentServicesOn) { theCount++;
A warning! This will not work if the service can depend on another service through two or more "paths". For example, consider this scenario:
serviceB.dependesOn(serviceA); serviceC.dependesOn(serviceA); serviceD.dependesOn(serviceB); serviceD.dependesOn(serviceC);
Now A is the "parent" of D in two ways. Thus, if you call getCountOfDependencies () on D , it will count the direct parents ( B and C ) and will ask each of them to report its dependencies. Each of them will report 1, and thus A will be counted with a double count.
So, if you may have this situation, you will have to change the approach.
source share