<commit_before>package com.amazon.aiv.sulfur.factories;

import com.amazon.aiv.sulfur.factories.exceptions.InvalidPageConfigException;
import com.amazon.aiv.sulfur.factories.exceptions.InvalidPageConfigsLocationException;
import com.amazon.aiv.sulfur.factories.exceptions.PageConfigsLocationNotProvidedException;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author <NAME> <<EMAIL>>
 *
 * #factory
 * #singleton
 *
 * TODO
 */
public class PageConfigFactory {
    private static final Logger LOG = Logger.getLogger(PageConfigFactory.class);

    private static PageConfigFactory singleton = null;

    /** Holds a reference to the Filename of PageConfigs that have been found */
    private Map<String, PageConfig> mPageConfigs = new HashMap <String, PageConfig>();

    /**
     * Factory method
     *
     * @return Singleton PageConfigFactory
     * @throws com.amazon.aiv.sulfur.factories.exceptions.PageConfigsLocationNotProvidedException
     */
    public synchronized static PageConfigFactory getInstance() {

        if (null == singleton) {
            singleton = new PageConfigFactory();
        }

        return singleton;
    }

    /**
     * Utility method to get rid of the PageConfigFactory Singleton Instance.
     * NOTE: Make sure you know what you are doing when using this.
     */
    public synchronized static void clearInstance() {
        singleton = null;
    }

    /**
     * Creates a PageConfigFactory, checking the given PageConfigFactory#PAGE_CONFIGS_DIR_PATH.
     *
     * @throws com.amazon.aiv.sulfur.factories.exceptions.PageConfigsLocationNotProvidedException
     */
    private PageConfigFactory() {
        // Read path to PageConfig Files directory
        String pageConfigsDirPath = System.getProperties().getProperty(Consts.SYSPROP_PAGE_CONFIGS_DIR_PATH);
        if (null == pageConfigsDirPath) {           //< check validity
            throw new PageConfigsLocationNotProvidedException();
        }

        // Scan the given directory for files with extension EXTENSION_PAGE_CONFIG_FILE
        File pageConfigsDir = new File(pageConfigsDirPath);
        File[] pageConfigsFiles = pageConfigsDir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File fileDir, String fileName) {
                return fileName.endsWith(Consts.EXTENSION_PAGE_CONFIG_FILE);
            }
        });
        if (null == pageConfigsFiles) {             //< check validity<commit_msg>Throw exception if the directory with PageConfig files is empty.<commit_after>package com.amazon.aiv.sulfur.factories;

import com.amazon.aiv.sulfur.factories.exceptions.InvalidPageConfigException;
import com.amazon.aiv.sulfur.factories.exceptions.InvalidPageConfigsLocationException;
import com.amazon.aiv.sulfur.factories.exceptions.PageConfigsLocationNotProvidedException;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author <NAME> <<EMAIL>>
 *
 * #factory
 * #singleton
 *
 * TODO
 */
public class PageConfigFactory {
    private static final Logger LOG = Logger.getLogger(PageConfigFactory.class);

    private static PageConfigFactory singleton = null;

    /** Holds a reference to the Filename of PageConfigs that have been found */
    private Map<String, PageConfig> mPageConfigs = new HashMap <String, PageConfig>();

    /**
     * Factory method
     *
     * @return Singleton PageConfigFactory
     * @throws com.amazon.aiv.sulfur.factories.exceptions.PageConfigsLocationNotProvidedException
     */
    public synchronized static PageConfigFactory getInstance() {

        if (null == singleton) {
            singleton = new PageConfigFactory();
        }

        return singleton;
    }

    /**
     * Utility method to get rid of the PageConfigFactory Singleton Instance.
     * NOTE: Make sure you know what you are doing when using this.
     */
    public synchronized static void clearInstance() {
        singleton = null;
    }

    /**
     * Creates a PageConfigFactory, checking the given PageConfigFactory#PAGE_CONFIGS_DIR_PATH.
     *
     * @throws com.amazon.aiv.sulfur.factories.exceptions.PageConfigsLocationNotProvidedException
     */
    private PageConfigFactory() {
        // Read path to PageConfig Files directory
        String pageConfigsDirPath = System.getProperties().getProperty(Consts.SYSPROP_PAGE_CONFIGS_DIR_PATH);
        if (null == pageConfigsDirPath) {           //< check validity
            throw new PageConfigsLocationNotProvidedException();
        }

        // Scan the given directory for files with extension EXTENSION_PAGE_CONFIG_FILE
        File pageConfigsDir = new File(pageConfigsDirPath);
        File[] pageConfigsFiles = pageConfigsDir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File fileDir, String fileName) {
                return fileName.endsWith(Consts.EXTENSION_PAGE_CONFIG_FILE);
            }
        });
        if (null == pageConfigsFiles || pageConfigsFiles.length == 0) {             //< check validity
{'fraction_non_alphanumeric': 0.08563586459286368, 'fraction_numerical': 0.0005489478499542544, 'mean_word_length': 3.2175925925925926, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, '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': '6078698', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1477, 'n_words': 358}