<commit_before>/*
 *  Copyright (C) 2010 reuillon
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the Affero GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.openmole.misc.executorservice.internal;

import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import org.openmole.commons.exception.InternalProcessingError;
import org.openmole.commons.exception.UserBadDataError;
import org.openmole.misc.executorservice.IExecutorService;
import org.openmole.misc.executorservice.ExecutorType;
import org.openmole.misc.workspace.ConfigurationLocation;

public class ExecutorService implements IExecutorService {

    final static ConfigurationLocation NbTread = new ConfigurationLocation(ExecutorService.class.getSimpleName(), "NbThreadsByExecutorTypes");

    static {
        Activator.getWorkspace().addToConfigurations(NbTread, "20");
    }
    
    final ThreadFactory threadFactory;
    final Map<Object, java.util.concurrent.ExecutorService> executorServices = new TreeMap<Object, java.util.concurrent.ExecutorService>();
    //final java.util.concurrent.ExecutorService cachedPool;
    int nbThreads;

    public ExecutorService(ThreadFactory threadFactory) throws InternalProcessingError, UserBadDataError {
        super();
        this.threadFactory = threadFactory;
        nbThreads = Activator.getWorkspace().getPreferenceAsInt(NbTread);
        //cachedPool = Executors.newCachedThreadPool(threadFactory);
    }

    @Override
    public java.util.concurrent.ExecutorService getExecutorService(ExecutorType type) {
        if (type == ExecutorType.OWN) {
            return Executors.newSingleThreadExecutor(threadFactory);
        }

        java.util.concurrent.ExecutorService ret;

        synchronized (executorServices) {
            ret = executorServices.get(type);
            if (ret == null) {
                ret = Executors.newFixedThreadPool(nbThreads, threadFactory);
                executorServices.put(type, ret);
            }
        }
        return ret;
    }

    @Override
    public java.util.concurrent.ExecutorService getExecutorService(String type) {
        java.util.concurrent.ExecutorService ret;

        synchronized (executorServices) {
            ret = executorServices.get(type);
            if (ret == null) {
                ret = Executors.newFixedThreadPool(nbThreads, threadFactory);
                executorServices.put(type, ret);
            }
        }
        return ret;
    }

    @Override
    public void removeAndShutDownExecutorService(String type, boolean now) {<commit_msg>Homegenize types.<commit_after>/*
 *  Copyright (C) 2010 reuillon
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the Affero GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.openmole.misc.executorservice.internal;

import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import org.openmole.commons.exception.InternalProcessingError;
import org.openmole.commons.exception.UserBadDataError;
import org.openmole.misc.executorservice.IExecutorService;
import org.openmole.misc.executorservice.ExecutorType;
import org.openmole.misc.workspace.ConfigurationLocation;

public class ExecutorService implements IExecutorService {

    final static ConfigurationLocation NbTread = new ConfigurationLocation(ExecutorService.class.getSimpleName(), "NbThreadsByExecutorTypes");

    static {
        Activator.getWorkspace().addToConfigurations(NbTread, "20");
    }
    
    final ThreadFactory threadFactory;
    final Map<String, java.util.concurrent.ExecutorService> executorServices = new TreeMap<String, java.util.concurrent.ExecutorService>();
    //final java.util.concurrent.ExecutorService cachedPool;
    int nbThreads;

    public ExecutorService(ThreadFactory threadFactory) throws InternalProcessingError, UserBadDataError {
        super();
        this.threadFactory = threadFactory;
        nbThreads = Activator.getWorkspace().getPreferenceAsInt(NbTread);
        //cachedPool = Executors.newCachedThreadPool(threadFactory);
    }

    @Override
    public java.util.concurrent.ExecutorService getExecutorService(ExecutorType type) {
        if (type == ExecutorType.OWN) {
            return Executors.newSingleThreadExecutor(threadFactory);
        }

        java.util.concurrent.ExecutorService ret;

        synchronized (executorServices) {
            ret = executorServices.get(type.name());
            if (ret == null) {
                ret = Executors.newFixedThreadPool(nbThreads, threadFactory);
                executorServices.put(type.name(), ret);
            }
        }
        return ret;
    }

    @Override
    public java.util.concurrent.ExecutorService getExecutorService(String type) {
        java.util.concurrent.ExecutorService ret;

        synchronized (executorServices) {
            ret = executorServices.get(type);
            if (ret == null) {
                ret = Executors.newFixedThreadPool(nbThreads, threadFactory);
                executorServices.put(type, ret);
            }
        }
        return ret;
    }

    @Override
    public void removeAndShutDownExecutorService(String type, boolean now) {
{'fraction_non_alphanumeric': 0.08182102628285357, 'fraction_numerical': 0.002190237797246558, 'mean_word_length': 3.3166779203241052, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5112836', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1661, 'n_words': 445}