package org.openlca.core.results;

import java.util.ArrayList;
import java.util.List;

import org.openlca.core.model.descriptors.FlowDescriptor;
import org.openlca.core.model.descriptors.ProcessDescriptor;

/**
 * A helper class that generates instances of {@link AnalysisFlowResult} from an
 * analysis result.
 */
public final class AnalysisFlowResults {

	private AnalysisFlowResults() {
	}

	public static List<AnalysisFlowResult> getForFlow(AnalysisResult result,
			FlowDescriptor flow, List<ProcessDescriptor> processes) {
		List<AnalysisFlowResult> results = new ArrayList<>();
		for (ProcessDescriptor process : processes) {
			AnalysisFlowResult r = getResult(result, process, flow);
			results.add(r);
		}
		return results;
	}

	public static List<AnalysisFlowResult> getForProcess(AnalysisResult result,
			ProcessDescriptor process, List<FlowDescriptor> flows) {
		List<AnalysisFlowResult> results = new ArrayList<>();
		for (FlowDescriptor flow : flows) {
			AnalysisFlowResult r = getResult(result, process, flow);
			results.add(r);
		}
		return results;
	}

	public static AnalysisFlowResult getResult(AnalysisResult result,
			ProcessDescriptor process, FlowDescriptor flow) {
		long flowId = flow.getId();
		long processId = process.getId();
		double single = result.getSingleFlowResult(processId, flowId);
		double total = result.getTotalFlowResult(processId, flowId);
		AnalysisFlowResult r = new AnalysisFlowResult();
		r.setFlow(flow);
		r.setProcess(process);
		r.setSingleResult(single);
		r.setTotalResult(total);
		r.setInput(result.getFlowIndex().isInput(flowId));
		return r;
	}

}

@@ -73,22 +73,219 @@
 va.util.
-List;%0A
+HashSet;%0Aimport java.util.List;%0A%0Aimport org.openlca.core.database.Cache;%0Aimport org.openlca.core.indices.FlowIndex;%0Aimport org.openlca.core.indices.LongPair;%0Aimport org.openlca.core.indices.ProductIndex;
 %0Aimport 
@@ -609,38 +609,32 @@
 ist%3C
-Analysis
 Flow
-Result
+Descriptor
 %3E getF
-orF
 low
+s
 (Ana
@@ -656,16 +656,85 @@
 ult,%0A%09%09%09
+Cache cache) %7B%0A%09%09FlowIndex flowIndex = result.getFlowIndex();%0A%09%09List%3C
 FlowDesc
@@ -743,115 +743,732 @@
 ptor
+%3E
  flow
-, List%3CProcessDescriptor%3E processes) %7B%0A%09%09List%3CAnalysisFlowResult%3E results = new ArrayList%3C%3E();%0A%09%09for (
+s = new ArrayList%3C%3E();%0A%09%09for (int i = 0; i %3C flowIndex.size(); i++) %7B%0A%09%09%09long flowId = flowIndex.getFlowAt(i);%0A%09%09%09FlowDescriptor flow = cache.getFlowDescriptor(flowId);%0A%09%09%09if (flow != null)%0A%09%09%09%09flows.add(flow);%0A%09%09%7D%0A%09%09return flows;%0A%09%7D%0A%0A%09public static List%3CAnalysisFlowResult%3E getForFlow(AnalysisResult result,%0A%09%09%09FlowDescriptor flow, Cache cache) %7B%0A%09%09List%3CAnalysisFlowResult%3E results = new ArrayList%3C%3E();%0A%09%09ProductIndex index = result.getProductIndex();%0A%09%09HashSet%3CLong%3E handled = new HashSet%3C%3E();%0A%09%09for (int i = 0; i %3C index.size(); i++) %7B%0A%09%09%09LongPair processProduct = index.getProductAt(i);%0A%09%09%09long processId = processProduct.getFirst();%0A%09%09%09if (handled.contains(processId))%0A%09%09%09%09continue;%0A%09%09%09handled.add(processId);%0A%09%09%09
 Proc
@@ -1493,22 +1493,86 @@
 ess 
-: p
+= cache.getP
 rocess
+D
 es
-) %7B
+criptor(processId);%0A%09%09%09if (process == null)%0A%09%09%09%09continue;
 %0A%09%09%09
@@ -1639,32 +1639,33 @@
 results.add(r);%0A
+%0A
 %09%09%7D%0A%09%09return res
@@ -1785,34 +1785,19 @@
 ss, 
-List%3CFlowDescriptor%3E flows
+Cache cache
 ) %7B%0A
@@ -1881,21 +1881,39 @@
  flow : 
-flows
+getFlows(result, cache)
 ) %7B%0A%09%09%09A

use cache in analysis flow result generator