<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.options;

import java.util.Collections;
import java.util.List;

import org.pepstock.charba.client.ChartEnvelop;
import org.pepstock.charba.client.GlobalOptions;
import org.pepstock.charba.client.commons.IsEnvelop;
import org.pepstock.charba.client.commons.Key;
import org.pepstock.charba.client.commons.NativeObject;
import org.pepstock.charba.client.commons.ObjectType;
import org.pepstock.charba.client.configuration.ConfigurationEnvelop;
import org.pepstock.charba.client.defaults.IsDefaultPlugins;
import org.pepstock.charba.client.enums.DefaultPluginId;
import org.pepstock.charba.client.plugins.AbstractPluginOptions;
import org.pepstock.charba.client.plugins.AbstractPluginOptionsFactory;
import org.pepstock.charba.client.plugins.PluginIdChecker;
import org.pepstock.charba.client.utils.Utilities;

/**
 * Definitions about plugins options. This is used to configure plugins (mainly the global ones).<br>
 * Every plugin could have own configuration structure.<br>
 * The java script object key is the plugin id.
 * 
 * @author <NAME>" Stocchero
 *
 */
public class Plugins extends AbstractModel<Options, IsDefaultPlugins> implements IsDefaultPlugins {

	// exception message when the plugin id is not the same of options passed as argument
	private static final String INVALID_ID_NOT_EQUALS_IN_OPTIONS = "Plugin id '{0}' is not equals to plugin id '{1}'of options";
	// exception message when the plugin id is not the same of factory passed as argument
	private static final String INVALID_ID_NOT_EQUALS_IN_FACTORY = "Plugin id '{0}' is not equals to plugin id '{1}'of factory";

	/**
	 * Creates the object wrapping another plugin.<br>This constructor is used ONLY from {@link GlobalOptions}.
	 * 
	 * @param envelop envelop passed by chart package in oredr to wrap another plugin.
	 */
	protected Plugins(ChartEnvelop<Plugins> envelop) {
		this(IsEnvelop.checkAndGetIfValid(envelop).getContent().getParent(), IsEnvelop.checkAndGetIfValid(envelop).getContent().getChildKey(), IsEnvelop.checkAndGetIfValid(envelop).getContent().getDefaultValues(),
				IsEnvelop.checkAndGetIfValid(envelop).getContent().getNativeObject());
	}

	/**
	 * Creates the object with the parent, the key of this element, defaults and native object to map java script properties.
	 * 
	 * @param options options of the chart.
	 * @param childKey the property name of this element to use to add it to the parent.
	 * @param defaultValues default provider
	 * @param nativeObject native object to map java script properties
	 */
	Plugins(Options options, Key childKey, IsDefaultPlugins defaultValues, NativeObject nativeObject) {
		super(options, childKey, defaultValues, nativeObject);
	}

	/**
	 * Returns the options for default plugin.
	 * 
	 * @param plugin default plugin id
	 * @return the options for default plugin
	 */
	NativeObject getDefaultPluginOptions(DefaultPluginId plugin) {
		return getValue(plugin);
	}

	/**
	 * Returns the unmodifiable list of registered plugin ids.
	 * 
	 * @return the unmodifiable list of registered plugin ids
	 */
	public final List<Key> getAllIds() {
		return Collections.unmodifiableList(keys());
	}

	/**
	 * Sets if a global plugin must be enabled or not.
	 * 
	 * @param pluginId plugin id.
	 * @param enabled <code>false</code> disable a global plugin.
	 */
	public final void setEnabled(String pluginId, boolean enabled) {
		setEnabled(PluginIdChecker.key(pluginId), enabled, false);
	}

	/**
	 * Sets if a default CHART.JS plugin must be enabled or not.<br>
	 * Callable only for chart configuration scope (no global or other).
	 * 
	 * @param envelop default CHART.JS plugin instance.
	 * @param enabled <code>false</code> disable a default CHART.JS plugin.
	 */
	public final void setEnabled(ConfigurationEnvelop<DefaultPluginId> envelop, boolean enabled) {
		// checks if envelop is valid
		if (IsEnvelop.isValid(envelop)) {
			setEnabled(envelop.getContent(), enabled, true);
		}
	}

	/**
	 * Sets if a global plugin must be enabled or not.<br>
	 * If <code>overrideDefaultPlugin</code>, passed as argument, is <code>true</code> that means this is called by configuration and it can enabled or disabled default plugins.
	 * 
	 * @param pluginId plugin id.
	 * @param enabled <code>false</code> disable a global plugin.
	 * @param overrideDefaultPlugin if <code>true</code> means this is called by configuration and it can enable or disable default plugins.
	 */
	private final void setEnabled(Key pluginId, boolean enabled, boolean overrideDefaultPlugin) {
		// checks if is a default plugin
		// if default plugin does nothing
		// but if override plugin is set, means that is done by configuration then allowed
		if (!DefaultPluginId.is(pluginId) || overrideDefaultPlugin) {
			// stores ONLY if enabled is false
			if (!enabled) {
				// stores false
				setValueAndAddToParent(pluginId, false);
			} else if (DefaultPluginId.is(pluginId)) {
				// if here, a default plugin is managing
				// then removes the key
				removeIfExists(pluginId);
			} else if (!isType(pluginId, ObjectType.OBJECT)) {
				// if here, is not a default plugin and
				// then sets an empty object to enable the plugin
				setEmptyValue(pluginId);
			}
		}
	}

	/**
	 * Returns if a plugin is enabled or not.
	 * 
	 * @param pluginId plugin id.
	 * @return <code>false</code> if a global plugin is not enabled otherwise <code>true</code>.
	 */
	public final boolean isEnabled(DefaultPluginId pluginId) {<commit_msg>Fixes bug when a default plugin was disabled<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.options;

import java.util.Collections;
import java.util.List;

import org.pepstock.charba.client.ChartEnvelop;
import org.pepstock.charba.client.GlobalOptions;
import org.pepstock.charba.client.commons.IsEnvelop;
import org.pepstock.charba.client.commons.Key;
import org.pepstock.charba.client.commons.NativeObject;
import org.pepstock.charba.client.commons.ObjectType;
import org.pepstock.charba.client.configuration.ConfigurationEnvelop;
import org.pepstock.charba.client.defaults.IsDefaultPlugins;
import org.pepstock.charba.client.enums.DefaultPluginId;
import org.pepstock.charba.client.plugins.AbstractPluginOptions;
import org.pepstock.charba.client.plugins.AbstractPluginOptionsFactory;
import org.pepstock.charba.client.plugins.PluginIdChecker;
import org.pepstock.charba.client.utils.Utilities;

/**
 * Definitions about plugins options. This is used to configure plugins (mainly the global ones).<br>
 * Every plugin could have own configuration structure.<br>
 * The java script object key is the plugin id.
 * 
 * @author <NAME>" Stocchero
 *
 */
public class Plugins extends AbstractModel<Options, IsDefaultPlugins> implements IsDefaultPlugins {

	// exception message when the plugin id is not the same of options passed as argument
	private static final String INVALID_ID_NOT_EQUALS_IN_OPTIONS = "Plugin id '{0}' is not equals to plugin id '{1}'of options";
	// exception message when the plugin id is not the same of factory passed as argument
	private static final String INVALID_ID_NOT_EQUALS_IN_FACTORY = "Plugin id '{0}' is not equals to plugin id '{1}'of factory";

	/**
	 * Creates the object wrapping another plugin.<br>This constructor is used ONLY from {@link GlobalOptions}.
	 * 
	 * @param envelop envelop passed by chart package in oredr to wrap another plugin.
	 */
	protected Plugins(ChartEnvelop<Plugins> envelop) {
		this(IsEnvelop.checkAndGetIfValid(envelop).getContent().getParent(), IsEnvelop.checkAndGetIfValid(envelop).getContent().getChildKey(), IsEnvelop.checkAndGetIfValid(envelop).getContent().getDefaultValues(),
				IsEnvelop.checkAndGetIfValid(envelop).getContent().getNativeObject());
	}

	/**
	 * Creates the object with the parent, the key of this element, defaults and native object to map java script properties.
	 * 
	 * @param options options of the chart.
	 * @param childKey the property name of this element to use to add it to the parent.
	 * @param defaultValues default provider
	 * @param nativeObject native object to map java script properties
	 */
	Plugins(Options options, Key childKey, IsDefaultPlugins defaultValues, NativeObject nativeObject) {
		super(options, childKey, defaultValues, nativeObject);
	}

	/**
	 * Returns the options for default plugin.
	 * 
	 * @param plugin default plugin id
	 * @return the options for default plugin
	 */
	NativeObject getDefaultPluginOptions(DefaultPluginId plugin) {
		return getValue(plugin);
	}

	/**
	 * Returns the unmodifiable list of registered plugin ids.
	 * 
	 * @return the unmodifiable list of registered plugin ids
	 */
	public final List<Key> getAllIds() {
		return Collections.unmodifiableList(keys());
	}

	/**
	 * Sets if a global plugin must be enabled or not.
	 * 
	 * @param pluginId plugin id.
	 * @param enabled <code>false</code> disable a global plugin.
	 */
	public final void setEnabled(String pluginId, boolean enabled) {
		setEnabled(PluginIdChecker.key(pluginId), enabled, false);
	}

	/**
	 * Sets if a default CHART.JS plugin must be enabled or not.<br>
	 * Callable only for chart configuration scope (no global or other).
	 * 
	 * @param envelop default CHART.JS plugin instance.
	 * @param enabled <code>false</code> disable a default CHART.JS plugin.
	 */
	public final void setEnabled(ConfigurationEnvelop<DefaultPluginId> envelop, boolean enabled) {
		// checks if envelop is valid
		if (IsEnvelop.isValid(envelop)) {
			setEnabled(envelop.getContent(), enabled, true);
		}
	}

	/**
	 * Sets if a global plugin must be enabled or not.<br>
	 * If <code>overrideDefaultPlugin</code>, passed as argument, is <code>true</code> that means this is called by configuration and it can enabled or disabled default plugins.
	 * 
	 * @param pluginId plugin id.
	 * @param enabled <code>false</code> disable a global plugin.
	 * @param overrideDefaultPlugin if <code>true</code> means this is called by configuration and it can enable or disable default plugins.
	 */
	private final void setEnabled(Key pluginId, boolean enabled, boolean overrideDefaultPlugin) {
		// checks if is a default plugin
		// if default plugin does nothing
		// but if override plugin is set, means that is done by configuration then allowed
		if (!DefaultPluginId.is(pluginId) || overrideDefaultPlugin) {
			// stores ONLY if enabled is false
			if (!enabled) {
				// stores false
				setValueAndAddToParent(pluginId, false);
			} else if (DefaultPluginId.is(pluginId)) {
				// if here, a default plugin is managing
				// it should add again the previous configuration
				reEnableDefaultPlugin(Key.getKeyByValue(DefaultPluginId.values(), pluginId.value()));
			} else if (!isType(pluginId, ObjectType.OBJECT)) {
				// if here, is not a default plugin and
				// then sets an empty object to enable the plugin
				setEmptyValue(pluginId);
			}
		}
	}
	
	/**
	 * Invoked to re-apply the default plugin configuration when the plugin was disable.
	 * 
	 * @param pluginId default plugin id
	 */
	private void reEnableDefaultPlugin(DefaultPluginId pluginId) {
		// checks if the property is a boolean
		// because it means it has been disable previously
		if (isType(pluginId, ObjectType.BOOLEAN)) {
			// checks which type of default plugin
			// in order to store the configuration object of the plugin
			if (DefaultPluginId.LEGEND.equals(pluginId)) {
				// re-apply the legend to plugin
				setValue(pluginId, getParent().getLegend());
			} else if (DefaultPluginId.TITLE.equals(pluginId)) {
				// re-apply the title to plugin
				setValue(pluginId, getParent().getTitle());
			} else if (DefaultPluginId.TOOLTIP.equals(pluginId)) {
				// re-apply the tolltip to plugin
				setValue(pluginId, getParent().getTooltips());
			} else {
				// removes key it exist
				removeIfExists(pluginId);
			}
		}
	}

	/**
	 * Returns if a plugin is enabled or not.
	 * 
	 * @param pluginId plugin id.
	 * @return <code>false</code> if a global plugin is not enabled otherwise <code>true</code>.
	 */
	public final boolean isEnabled(DefaultPluginId pluginId) {
{'fraction_non_alphanumeric': 0.08970099667774087, 'fraction_numerical': 0.001812141347025068, 'mean_word_length': 4.141692546583851, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, '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': '6266233', 'n_tokens_mistral': 3897, 'n_tokens_neox': 3608, 'n_words': 1341}