<commit_before>/*
 * Copyright 2015 mk.
 *
 * 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.krysalis.barcode4j;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mk
 */
public class BarcodeGeneratorProvider {

    private static BarcodeGeneratorProvider instance;
    private static final Logger LOGGER = Logger.getLogger(BarcodeGeneratorProvider.class.getName());
    private final ServiceLoader<BarcodeGenerator> loader;
    private final Map<String, Class<BarcodeGenerator>> barcodeGenerators;

    private BarcodeGeneratorProvider() {
        this.barcodeGenerators = new HashMap<String, Class<BarcodeGenerator>>();
        loader = ServiceLoader.load(BarcodeGenerator.class);
        initialize();
    }

    public static synchronized BarcodeGeneratorProvider getInstance() {
        if (instance == null) {
            instance = new BarcodeGeneratorProvider();
            LOGGER.log(Level.INFO, "Created new instance.");
        }
        return instance;
    }

    public Collection<String> getAvailableBarcodeGenerators() {
        Set<String> res = new TreeSet<String>();
        res.addAll(barcodeGenerators.keySet());
        return res;
    }

    public BarcodeGenerator getBarcodeGenerator(String id) throws BarcodeException {
        Class<BarcodeGenerator> clazz = barcodeGenerators.get(id);

        if (clazz == null) {
            throw new BarcodeException("BarcodeGenerator with " + id + " is not available.");
        }

        BarcodeGenerator res = null;
        try {
            res = clazz.newInstance();
        } catch (InstantiationException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
        }
        return res;
    }

    private void initialize() {
        Iterator<BarcodeGenerator> iterator = loader.iterator();
        while (iterator.hasNext()) {
            try {
                BarcodeGenerator bg = iterator.next();
                String id = bg.getId();
                Class<BarcodeGenerator> clazz = (Class<BarcodeGenerator>) bg.getClass();
                barcodeGenerators.put(id, clazz);
                Collection<String> addIds = bg.getAdditionalNames();
                if (addIds != null) {
                    for (String addId : addIds) {
                        barcodeGenerators.putIfAbsent(addId, clazz);
                    }
                }<commit_msg>javadoc on new BarcodeGeneratorProvider<commit_after>/*
 * Copyright 2015 mk.
 *
 * 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.krysalis.barcode4j;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Loading of implementation classes through SPI.
 *
 * @author mk
 * @version 1.1
 * @since 2.1.2
 */
public class BarcodeGeneratorProvider {

    private static BarcodeGeneratorProvider instance;
    private static final Logger LOGGER = Logger.getLogger(BarcodeGeneratorProvider.class.getName());
    private final ServiceLoader<BarcodeGenerator> loader;
    private final Map<String, Class<BarcodeGenerator>> barcodeGenerators;

    /**
     * Initialize this singleton.
     */
    private BarcodeGeneratorProvider() {
        this.barcodeGenerators = new HashMap<String, Class<BarcodeGenerator>>();
        loader = ServiceLoader.load(BarcodeGenerator.class);
        initialize();
    }

    /**
     * Gets the Provider.
     *
     * @return single instance
     */
    public static synchronized BarcodeGeneratorProvider getInstance() {
        if (instance == null) {
            instance = new BarcodeGeneratorProvider();
            LOGGER.log(Level.INFO, "Created new instance.");
        }
        return instance;
    }

    /**
     * Lists all names of available BarcodeGenerators.
     *
     * @return Set of available BarcodeGenerator names
     */
    public Collection<String> getAvailableBarcodeGenerators() {
        Set<String> res = new TreeSet<String>();
        res.addAll(barcodeGenerators.keySet());
        return res;
    }

    /**
     * Instanciates a fresh copy of a BarcodeGenerator.
     *
     * @param id Name of Barcode ({@link #getAvailableBarcodeGenerators() })
     * @return a fresh copy
     * @throws BarcodeException if barcode with {@code id} is not available 
     */
    public BarcodeGenerator getBarcodeGenerator(String id) throws BarcodeException {
        Class<BarcodeGenerator> clazz = barcodeGenerators.get(id);

        if (clazz == null) {
            throw new BarcodeException("BarcodeGenerator with " + id + " is not available.");
        }

        BarcodeGenerator res = null;
        try {
            res = clazz.newInstance();
        } catch (InstantiationException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
        }
        return res;
    }

    /**
     * Loads all implementing Classes through ServiceLoader interface.
     */
    private void initialize() {
        Iterator<BarcodeGenerator> iterator = loader.iterator();
        while (iterator.hasNext()) {
            try {
                BarcodeGenerator bg = iterator.next();
                String id = bg.getId();
                Class<BarcodeGenerator> clazz = (Class<BarcodeGenerator>) bg.getClass();
                barcodeGenerators.put(id, clazz);
                Collection<String> addIds = bg.getAdditionalNames();
                if (addIds != null) {
                    for (String addId : addIds) {
                        barcodeGenerators.putIfAbsent(addId, clazz);
                    }
                }
{'fraction_non_alphanumeric': 0.09410446716146198, 'fraction_numerical': 0.0032208374177286093, 'mean_word_length': 2.6180344478216817, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '3637176', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1889, 'n_words': 543}