<commit_before>/**
    Copyright 2017 <NAME>" Stocchero

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

	    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
package org.pepstock.charba.client.impl.callbacks;

import java.util.List;

import org.pepstock.charba.client.IsChart;
import org.pepstock.charba.client.callbacks.ScriptableContext;
import org.pepstock.charba.client.data.DataPoint;
import org.pepstock.charba.client.data.Dataset;
import org.pepstock.charba.client.data.HasDataPoints;
import org.pepstock.charba.client.enums.DataType;

/**
 * Utility to calculate the percentage of the value based on the datasets of chart.
 * 
 * @author <NAME>" Stocchero
 *
 */
public final class Percentage {

	/**
	 * To avoid any instantiation
	 */
	private Percentage() {
		// do nothing
	}

	/**
	 * Computes the percentage of the value based on the data of datasets. If the dataset is composed by {@link DataPoint}, it
	 * uses the {@link DataPoint#getY()} value to compute the percentage. The stack is ignored.
	 * 
	 * @param chart chart instance
	 * @param value current value to be evaluated
	 * @param context data labels plugin context
	 * @return the percentage value, a double between 0 and 1. If the datasets does not contains any data, it will return
	 *         {@link Double#NaN}.
	 */
	public static double compute(IsChart chart, double value, ScriptableContext context) {
		return compute(chart, value, context, false);
	}

	/**
	 * Computes the percentage of the value based on the data of datasets. If the dataset is composed by {@link DataPoint}, it
	 * uses the {@link DataPoint#getY()} value to compute the percentage.
	 * 
	 * @param chart chart instance
	 * @param value current value to be evaluated
	 * @param context data labels plugin context
	 * @param stacked if <code>true</code>, it calculates the total at the same data index (cross datasets), otherwise if
	 *            <code>false</code> it calculates the total at the same dataset index (single dataset)
	 * @return the percentage value, a double between 0 and 1. If the datasets does not contains any data, it will return
	 *         {@link Double#NaN}.
	 */
	public static double compute(IsChart chart, double value, ScriptableContext context, boolean stacked) {
		// creates the total reference
		double total = 0D;
		// checks if stacked
		if (stacked) {
			// scans all datasets
			for (Dataset ds : chart.getData().getDatasets()) {
				// if dataset contains data points
				if (DataType.POINTS.equals(ds.getDataType())) {
					// then dataset is data point container
					// and then cast it
					HasDataPoints hasDataPoints = (HasDataPoints) ds;
					// gets the data points at data index
					DataPoint point = hasDataPoints.getDataPoints().get(context.getIndex());
					// adds the Y value to the total
					total = total + point.getY();
				} else if (DataType.NUMBERS.equals(ds.getDataType())) {
					// if here, the dataset has got data as doubles
					// then it get the double at data index
					double data = ds.getData().get(context.getIndex());
					// adds it to total
					total = total + data;
				}
			}
		} else {
			// if here, the argument of stack is false
			// then it calculates the values inside the dataset
			Dataset ds = chart.getData().getDatasets().get(context.getDatasetIndex());
			// if dataset contains data points
			if (DataType.POINTS.equals(ds.getDataType())) {
				// then dataset is data points container
				// and then cast it
				HasDataPoints hasDataPoints = (HasDataPoints) ds;
				// gets the data points
				List<DataPoint> points = hasDataPoints.getDataPoints();
				// scans data points
				for (DataPoint dataPoint : points) {
					// adds the Y value to the total
					total = total + dataPoint.getY();
				}
			} else if (DataType.NUMBERS.equals(ds.getDataType())) {
				// if here, the dataset has got data as doubles
				// then it get the doubles
				List<Double> data = ds.getData();
				// scans doubles
				for (Double dataValue : data) {
					// adds it to total
					total = total + dataValue;
				}
			}
		}
		// if total is zero
		// means that no datasets are available data
		if (total == 0) {
			// returns NaN
			return Double.NaN;
		}
		// calculates the percentage
		return value / total;
	}

}<commit_msg>Cognitive Complexity of methods should not be too high<commit_after>/**
    Copyright 2017 <NAME>" Stocchero

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

	    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
package org.pepstock.charba.client.impl.callbacks;

import java.util.List;

import org.pepstock.charba.client.IsChart;
import org.pepstock.charba.client.callbacks.ScriptableContext;
import org.pepstock.charba.client.data.DataPoint;
import org.pepstock.charba.client.data.Dataset;
import org.pepstock.charba.client.data.HasDataPoints;
import org.pepstock.charba.client.enums.DataType;

/**
 * Utility to calculate the percentage of the value based on the datasets of chart.
 * 
 * @author <NAME>" Stocchero
 *
 */
public final class Percentage {

	/**
	 * To avoid any instantiation
	 */
	private Percentage() {
		// do nothing
	}

	/**
	 * Computes the percentage of the value based on the data of datasets. If the dataset is composed by {@link DataPoint}, it
	 * uses the {@link DataPoint#getY()} value to compute the percentage. The stack is ignored.
	 * 
	 * @param chart chart instance
	 * @param value current value to be evaluated
	 * @param context data labels plugin context
	 * @return the percentage value, a double between 0 and 1. If the datasets does not contains any data, it will return
	 *         {@link Double#NaN}.
	 */
	public static double compute(IsChart chart, double value, ScriptableContext context) {
		return compute(chart, value, context, false);
	}

	/**
	 * Computes the percentage of the value based on the data of datasets. If the dataset is composed by {@link DataPoint}, it
	 * uses the {@link DataPoint#getY()} value to compute the percentage.
	 * 
	 * @param chart chart instance
	 * @param value current value to be evaluated
	 * @param context data labels plugin context
	 * @param stacked if <code>true</code>, it calculates the total at the same data index (cross datasets), otherwise if
	 *            <code>false</code> it calculates the total at the same dataset index (single dataset)
	 * @return the percentage value, a double between 0 and 1. If the datasets does not contains any data, it will return
	 *         {@link Double#NaN}.
	 */
	public static double compute(IsChart chart, double value, ScriptableContext context, boolean stacked) {
		// creates the total reference
		double total;
		// checks if stacked
		if (stacked) {
			// get the total for stacked datasets
			total = getTotalForStacked(chart, context);
		} else {
			// get the total for no stacked datasets
			total = getTotal(chart, context);
		}
		// if total is zero
		// means that no datasets are available data
		if (total == 0) {
			// returns NaN
			return Double.NaN;
		}
		// calculates the percentage
		return value / total;
	}

	/**
	 * Calculates the total for data values for stacked dataset.
	 * 
	 * @param chart chart instance
	 * @param context data labels plugin context
	 * @return the total for data values for stacked dataset.
	 */
	private static double getTotalForStacked(IsChart chart, ScriptableContext context) {
		// creates the total reference
		double total = 0D;
		// scans all datasets
		for (Dataset ds : chart.getData().getDatasets()) {
			// if dataset contains data points
			if (DataType.POINTS.equals(ds.getDataType())) {
				// then dataset is data point container
				// and then cast it
				HasDataPoints hasDataPoints = (HasDataPoints) ds;
				// gets the data points at data index
				DataPoint point = hasDataPoints.getDataPoints().get(context.getIndex());
				// adds the Y value to the total
				total = total + point.getY();
			} else if (DataType.NUMBERS.equals(ds.getDataType())) {
				// if here, the dataset has got data as doubles
				// then it get the double at data index
				double data = ds.getData().get(context.getIndex());
				// adds it to total
				total = total + data;
			}
		}
		return total;
	}

	/**
	 * Calculates the total for data values for NO stacked dataset.
	 * 
	 * @param chart chart instance
	 * @param context data labels plugin context
	 * @return the total for data values for NO stacked dataset.
	 */
	private static double getTotal(IsChart chart, ScriptableContext context) {
		// creates the total reference
		double total = 0D;
		// if here, the argument of stack is false
		// then it calculates the values inside the dataset
		Dataset ds = chart.getData().getDatasets().get(context.getDatasetIndex());
		// if dataset contains data points
		if (DataType.POINTS.equals(ds.getDataType())) {
			// then dataset is data points container
			// and then cast it
			HasDataPoints hasDataPoints = (HasDataPoints) ds;
			// gets the data points
			List<DataPoint> points = hasDataPoints.getDataPoints();
			// scans data points
			for (DataPoint dataPoint : points) {
				// adds the Y value to the total
				total = total + dataPoint.getY();
			}
		} else if (DataType.NUMBERS.equals(ds.getDataType())) {
			// if here, the dataset has got data as doubles
			// then it get the doubles
			List<Double> data = ds.getData();
			// scans doubles
			for (Double dataValue : data) {
				// adds it to total
				total = total + dataValue;
			}
		}
		return total;
	}
}
{'fraction_non_alphanumeric': 0.09037309104968104, 'fraction_numerical': 0.002803015658225401, 'mean_word_length': 3.1637826961770625, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6613220', 'n_tokens_mistral': 3194, 'n_tokens_neox': 2820, 'n_words': 1192}