ar.com.allium.rules.core.service.AlliumCoreRuleManager.java Source code

Java tutorial

Introduction

Here is the source code for ar.com.allium.rules.core.service.AlliumCoreRuleManager.java

Source

package ar.com.allium.rules.core.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import ar.com.allium.rules.core.model.AlliumRule;
import ar.com.allium.rules.core.model.AlliumRuleNone;

/**
 * Copyright 2014 Joel del Valle <joelmarcosdelvalle@gmail.com>
 * 
 * This file is part of allium-rules, project of allium-projects.
 * 
 * allium-rules is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * allium-rules 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
 * allium-rules. If not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 * @author joel.delvalle
 * 
 *         AlliumRules service manager
 * 
 */
@Service("alliumCoreRuleManager")
public final class AlliumCoreRuleManager {

    private static Logger log = Logger.getLogger(AlliumCoreRuleManager.class);

    private Map<String, List<String>> ruleSetMap = new HashMap<String, List<String>>();

    private Map<String, AlliumRule> rulesMap = new HashMap<String, AlliumRule>();

    public void addRule(String alliumRuleSetName, AlliumRule rule) {
        log.debug("add rule: " + rule.getRuleName() + "  in a set rule: " + alliumRuleSetName);

        alliumRuleSetName = this.buildCorrectClassName(alliumRuleSetName);
        String ruleName = this.buildCorrectClassName(rule.getRuleName());

        rulesMap.put(ruleName, rule);

        if (!ruleSetMap.containsKey(alliumRuleSetName)) {
            ruleSetMap.put(alliumRuleSetName, new ArrayList<String>());
        }

        List<String> rulesList = ruleSetMap.get(alliumRuleSetName);

        if (!rulesList.contains(ruleName)) {
            rulesList.add(ruleName);
        }
    }

    public void addRule(AlliumRule rule) {
        log.debug("add rule: " + rule.getRuleName());

        String ruleName = this.buildCorrectClassName(rule.getRuleName());

        if (!rulesMap.containsKey(ruleName)) {
            rulesMap.put(ruleName, rule);
        }
    }

    public List<AlliumRule> getRuleList(String alliumRuleSetName) {
        log.debug("get rule list: " + alliumRuleSetName);

        alliumRuleSetName = this.buildCorrectClassName(alliumRuleSetName);

        if (ruleSetMap.containsKey(alliumRuleSetName)) {
            return this.buildRuleSet(ruleSetMap.get(alliumRuleSetName));
        }

        return new ArrayList<AlliumRule>();
    }

    public AlliumRule getRule(Class<? extends AlliumRule> alliumRuleClass) {
        log.debug("get rule: " + alliumRuleClass.getSimpleName());

        String ruleName = this.buildCorrectClassName(alliumRuleClass.getSimpleName());

        if (rulesMap.containsKey(ruleName)) {
            return rulesMap.get(ruleName);
        }

        return new AlliumRuleNone();
    }

    private List<AlliumRule> buildRuleSet(List<String> ruleNameList) {

        List<AlliumRule> ruleSet = new ArrayList<AlliumRule>();

        for (String ruleName : ruleNameList) {
            ruleSet.add(rulesMap.get(this.buildCorrectClassName(ruleName)));
        }

        Collections.sort(ruleSet);

        return ruleSet;
    }

    private String buildCorrectClassName(String className) {
        return Character.toLowerCase(className.charAt(0)) + className.substring(1);
    }

}