Reputation: 401
I have a forloop for each class and each method to get specific metrics and its exported to a csv like this
ClassName MethodName No of Nodes
One One.One 4
One Two.One 2
ClassName MethodName No of Nodes
Three Three.One 4
Three Four.One 1
However I want to get the sum of all the nodes per class before I export it to a csv. Like this
ClassName MethodName No of Nodes Total Nodes
One One.One 4
One Two.One 2 6
ClassName MethodName No of Nodes
Three Three.One 4
Three Four.One 1 5
This is what I have done so far
int numNodes;
for(Class cl : classes){
for(Method m : cl.getMethod){
numNodes = getNodeCount(m);
}
System.out.print(sum(numNodes));//this doesnt work.
}
public int getNodeCount(Graph g){
return g.getNodes().size();
}
I have created a sepreate method to getSum
public int sum(int...numbers){
int result = 0;
for(int i = 0 ; i < numbers.length; i++) {
result += numbers[i];
}
return result;
}
My question is how to get the sum of all the nodes per class before I export it to a csv.
Upvotes: 1
Views: 496
Reputation: 5294
Have you considered an object to store the data per class and handle your output? For example
public class ClassMetrics {
private Class clazz;
private Map<String, Integer> methodNodeCount;
private int totalNodeCount;
// getters and setters
public ClassMetrics(Class clazz) {
this.clazz = clazz;
this.methodNodeCount = new HashMap<>();
this.totalNodeCount = 0;
for (Method method : this.clazz.getDeclaredMethods()) {
int curNodeCount = getNodeCount(method);
this.methodNodeCount.put(method.getName(), curNodeCount); // <-- your getNodeCount method here
this.totalNodeCount += curNodeCount;
}
public void printToFile(File file) {
// implement your logic here...
}
public int getNodeCount(Graph g) {
// your method
}
}
}
Then you have
File file = new File("somewhere");
for (Class clazz : classes) {
new ClassMetrics(clazz).printToFile(file);
}
This way you break the problem down into smaller pieces and make the object (ClassMethrics instance) do the work (print to file).
Upvotes: 0
Reputation: 131324
Here, sum(numNodes)
uses numNodes
that contains only the last result of getNodeCount(m)
for the last method and the current iterated class :
for(Class cl : classes){
for(Method m : cl.getMethod){
numNodes = getNodeCount(m);
}
System.out.print(sum(numNodes));//this doesnt work.
}
You should compute the sum for each class by initialzing sumNodes
to 0
for each iterated class and by summing the getNodeCount(m)
result of each method of the current iterated class :
for(Class cl : classes){
int sumNodes = 0;
for(Method m : cl.getMethod){
sumNodes += getNodeCount(m);
}
System.out.print("sumNodes="+sumNodes)
}
Upvotes: 1
Reputation: 2814
What you need is a sum
counter per class, so you might create a map of
String=>Integer to store the className and it's respective sum like below
Map<String, Integer> sumPerClass = new HashMap<>();
for(Class cl : classes){
int numNodes = 0;
for(Method m : cl.getMethod){
numNodes += getNodeCount(m);
}
System.out.print("Class = " + cl.getName() + ", Sum = " + numNodes);
sumPerClass.put(cl.getName(), numNodes)
}
Upvotes: 0