<commit_before>package org.ow2.choreos.ee.nodes;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.ow2.choreos.ee.config.CloudConfiguration;
import org.ow2.choreos.ee.nodes.cloudprovider.CloudProvider;
import org.ow2.choreos.ee.nodes.cloudprovider.CloudProviderFactory;
import org.ow2.choreos.nodes.NodeNotDestroyed;
import org.ow2.choreos.nodes.NodeNotFoundException;
import org.ow2.choreos.nodes.datamodel.CloudNode;
import org.ow2.choreos.utils.Concurrency;
import org.ow2.choreos.utils.TimeoutsAndTrials;

public class NodesDestroyer {

    private static final String TASK_NAME = "NODE_DELETION";

    private static Logger logger = Logger.getLogger(NodesDestroyer.class);

    private final Collection<CloudNode> nodesToDestroy;

    private CloudProvider cp;
    private List<DestroyTask> tasks = new ArrayList<DestroyTask>();
    private ExecutorService executor;

    private int totalTimeout;

    private List<CloudNode> destroyedNodes = new ArrayList<CloudNode>();

    public NodesDestroyer(CloudConfiguration cloudConfiguration, Collection<CloudNode> nodesToDestroy) {
	this.nodesToDestroy = nodesToDestroy;
	this.totalTimeout = TimeoutsAndTrials.getTotalTimeout(TASK_NAME);
	totalTimeout += totalTimeout * 0.2;
	this.cp = CloudProviderFactory.getFactoryInstance().getCloudProviderInstance(cloudConfiguration);
    }

    /**
     * 
     * @throws NodeNotDestroyed
     *             if some node was not destroyed
     */
    public List<CloudNode> destroyNodes() throws NodeNotDestroyed {
	destroy();
	waitDestroy();
	check();
	return destroyedNodes;
    }

    public List<CloudNode> getDestroyedNodes() {
	return destroyedNodes;
    }

    public void setDestroyedNodes(List<CloudNode> destroyedNodes) {
	this.destroyedNodes = destroyedNodes;
    }

    private void destroy() {
	if (nodesToDestroy == null || nodesToDestroy.isEmpty())
	    return;
	executor = Executors.newFixedThreadPool(nodesToDestroy.size());
	tasks = new ArrayList<DestroyTask>();
	for (CloudNode node : nodesToDestroy) {
	    DestroyTask task = new DestroyTask(node);
	    tasks.add(task);
	    executor.submit(task);
	}
    }

    private void waitDestroy() {
	String erMsg = "Could not wait for nodes destroyment";
	int n = nodesToDestroy.size();
	int totalTimeout = this.totalTimeout + 2 * n; // one req/sec rule
	Concurrency.waitExecutor(executor, totalTimeout, TimeUnit.SECONDS, logger, erMsg);
    }

    private void check() throws NodeNotDestroyed {
	int originalSize = nodesToDestroy.size();
	int finalSize = destroyedNodes.size();<commit_msg>avoiding a null pointer exception<commit_after>package org.ow2.choreos.ee.nodes;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.ow2.choreos.ee.config.CloudConfiguration;
import org.ow2.choreos.ee.nodes.cloudprovider.CloudProvider;
import org.ow2.choreos.ee.nodes.cloudprovider.CloudProviderFactory;
import org.ow2.choreos.nodes.NodeNotDestroyed;
import org.ow2.choreos.nodes.NodeNotFoundException;
import org.ow2.choreos.nodes.datamodel.CloudNode;
import org.ow2.choreos.utils.Concurrency;
import org.ow2.choreos.utils.TimeoutsAndTrials;

public class NodesDestroyer {

    private static final String TASK_NAME = "NODE_DELETION";

    private static Logger logger = Logger.getLogger(NodesDestroyer.class);

    private final Collection<CloudNode> nodesToDestroy;

    private CloudProvider cp;
    private List<DestroyTask> tasks = new ArrayList<DestroyTask>();
    private ExecutorService executor;

    private int totalTimeout;

    private List<CloudNode> destroyedNodes = new ArrayList<CloudNode>();

    public NodesDestroyer(CloudConfiguration cloudConfiguration, Collection<CloudNode> nodesToDestroy) {
	this.nodesToDestroy = nodesToDestroy;
	this.totalTimeout = TimeoutsAndTrials.getTotalTimeout(TASK_NAME);
	totalTimeout += totalTimeout * 0.2;
	this.cp = CloudProviderFactory.getFactoryInstance().getCloudProviderInstance(cloudConfiguration);
    }

    /**
     * 
     * @throws NodeNotDestroyed
     *             if some node was not destroyed
     */
    public List<CloudNode> destroyNodes() throws NodeNotDestroyed {
        if (nodesToDestroy == null || nodesToDestroy.isEmpty())
            return new ArrayList<CloudNode>();
        destroy();
	waitDestroy();
	check();
	return destroyedNodes;
    }

    public List<CloudNode> getDestroyedNodes() {
	return destroyedNodes;
    }

    public void setDestroyedNodes(List<CloudNode> destroyedNodes) {
	this.destroyedNodes = destroyedNodes;
    }

    private void destroy() {
	executor = Executors.newFixedThreadPool(nodesToDestroy.size());
	tasks = new ArrayList<DestroyTask>();
	for (CloudNode node : nodesToDestroy) {
	    DestroyTask task = new DestroyTask(node);
	    tasks.add(task);
	    executor.submit(task);
	}
    }

    private void waitDestroy() {
	String erMsg = "Could not wait for nodes destroyment";
	int n = nodesToDestroy.size();
	int totalTimeout = this.totalTimeout + 2 * n; // one req/sec rule
	Concurrency.waitExecutor(executor, totalTimeout, TimeUnit.SECONDS, logger, erMsg);
    }

    private void check() throws NodeNotDestroyed {
	int originalSize = nodesToDestroy.size();
	int finalSize = destroyedNodes.size();
{'fraction_non_alphanumeric': 0.0984713828652684, 'fraction_numerical': 0.004621400639886242, 'mean_word_length': 4.627, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '6204730', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1628, 'n_words': 355}