<commit_before>package org.openlca.core.results;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.openlca.core.database.Cache;
import org.openlca.core.model.NormalizationWeightingSet;
import org.openlca.core.model.ProjectVariant;
import org.openlca.core.model.descriptors.FlowDescriptor;
import org.openlca.core.model.descriptors.ImpactCategoryDescriptor;

/**
 * A project result is a wrapper for the inventory results of the respective
 * project variants.
 */
public class ProjectResult {

	private HashMap<ProjectVariant, InventoryResult> results = new HashMap<>();

	public void addResult(ProjectVariant variant, InventoryResult result) {
		results.put(variant, result);
	}

	public Set<ProjectVariant> getVariants() {
		return results.keySet();
	}

	public InventoryResult getResult(ProjectVariant variant) {
		return results.get(variant);
	}

	public Set<FlowDescriptor> getFlows(Cache cache) {
		Set<FlowDescriptor> flows = new HashSet<>();
		for (InventoryResult result : results.values())
			flows.addAll(result.getFlowResults().getFlows(cache));
		return flows;
	}

	public InventoryFlowResult getFlowResult(ProjectVariant variant,
			FlowDescriptor flow) {
		InventoryResult result = results.get(variant);
		if (result == null)
			return null;
		return result.getFlowResults().get(flow);
	}

	public List<InventoryFlowResult> getFlowResults(ProjectVariant variant,
			Cache cache) {
		InventoryResult result = results.get(variant);
		if (result == null)
			return Collections.emptyList();
		return result.getFlowResults().getAll(cache);
	}

	public Set<ImpactCategoryDescriptor> getImpacts(Cache cache) {
		Set<ImpactCategoryDescriptor> impacts = new HashSet<>();
		for (InventoryResult result : results.values())
			impacts.addAll(result.getImpactResults().getImpacts(cache));
		return impacts;
	}

	public InventoryImpactResult getImpactResult(ProjectVariant variant,
			ImpactCategoryDescriptor impact) {
		InventoryResult result = results.get(variant);
		if (result == null)
			return null;
		return result.getImpactResults().get(impact);
	}

	public InventoryImpactResult getImpactResult(ProjectVariant variant,
			ImpactCategoryDescriptor impact, NormalizationWeightingSet nwSet) {
		InventoryResult result = results.get(variant);
		if (result == null)
			return null;
		return result.getImpactResults().get(impact, nwSet);
	}

}<commit_msg>added contribution calculation to project results<commit_after>package org.openlca.core.results;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.openlca.core.database.Cache;
import org.openlca.core.model.NormalizationWeightingSet;
import org.openlca.core.model.ProjectVariant;
import org.openlca.core.model.descriptors.FlowDescriptor;
import org.openlca.core.model.descriptors.ImpactCategoryDescriptor;
import org.openlca.core.results.Contributions.Function;

/**
 * A project result is a wrapper for the inventory results of the respective
 * project variants.
 */
public class ProjectResult {

	private HashMap<ProjectVariant, InventoryResult> results = new HashMap<>();

	public void addResult(ProjectVariant variant, InventoryResult result) {
		results.put(variant, result);
	}

	public Set<ProjectVariant> getVariants() {
		return results.keySet();
	}

	public InventoryResult getResult(ProjectVariant variant) {
		return results.get(variant);
	}

	public Set<FlowDescriptor> getFlows(Cache cache) {
		Set<FlowDescriptor> flows = new HashSet<>();
		for (InventoryResult result : results.values())
			flows.addAll(result.getFlowResults().getFlows(cache));
		return flows;
	}

	public InventoryFlowResult getFlowResult(ProjectVariant variant,
			FlowDescriptor flow) {
		InventoryResult result = results.get(variant);
		if (result == null)
			return null;
		return result.getFlowResults().get(flow);
	}

	public List<InventoryFlowResult> getFlowResults(ProjectVariant variant,
			Cache cache) {
		InventoryResult result = results.get(variant);
		if (result == null)
			return Collections.emptyList();
		return result.getFlowResults().getAll(cache);
	}

	public ContributionSet<ProjectVariant> getContributions(
			final FlowDescriptor flow) {
		return Contributions.calculate(getVariants(),
				new Function<ProjectVariant>() {
					@Override
					public double value(ProjectVariant variant) {
						return getFlowResult(variant, flow).getValue();
					}
				});
	}

	public Set<ImpactCategoryDescriptor> getImpacts(Cache cache) {
		Set<ImpactCategoryDescriptor> impacts = new HashSet<>();
		for (InventoryResult result : results.values())
			impacts.addAll(result.getImpactResults().getImpacts(cache));
		return impacts;
	}

	public InventoryImpactResult getImpactResult(ProjectVariant variant,
			ImpactCategoryDescriptor impact) {
		InventoryResult result = results.get(variant);
		if (result == null)
			return null;
		return result.getImpactResults().get(impact);
	}

	public InventoryImpactResult getImpactResult(ProjectVariant variant,
			ImpactCategoryDescriptor impact, NormalizationWeightingSet nwSet) {
		InventoryResult result = results.get(variant);
		if (result == null)
			return null;
		return result.getImpactResults().get(impact, nwSet);
	}

	public ContributionSet<ProjectVariant> getContributions(
			final ImpactCategoryDescriptor impact) {
		return Contributions.calculate(getVariants(),
				new Function<ProjectVariant>() {
					@Override
					public double value(ProjectVariant variant) {
						return getImpactResult(variant, impact).getValue();
					}
				});
	}
}
{'fraction_non_alphanumeric': 0.10186335403726708, 'fraction_numerical': 0.0, 'mean_word_length': 5.448512585812357, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '16507', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1592, 'n_words': 367}