All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.technophobia.substeps.model.PatternMap Maven / Gradle / Ivy

/*
 *	Copyright Technophobia Ltd 2012
 *
 *   This file is part of Substeps.
 *
 *    Substeps is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Substeps 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 Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with Substeps.  If not, see .
 */
package com.technophobia.substeps.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Pattern;

import com.google.common.base.Strings;

/**
 * a map of regex patterns to other things.
 * 
 * @author imoore
 * 
 */
public class PatternMap {

    private final Map patternMap = new HashMap();
    private final Map keys = new HashMap();
    private V nullValue = null;


    public V getNullVale() {
        return nullValue;
    }


    /**
     * Adds a new pattern to the map.
     * 
     * Users of this class must check if the pattern has already been added
     * by using {@link containsPattern} to avoid IllegalStateException in the event that
     * the pattern has already been added to the map. 
     * 
     * @param pattern
     * @param value
     * 
     * @throws IllegalStateException - if the map already contains the specified patter.
     */
    public void put(final String pattern, final V value) throws IllegalStateException {

        if (pattern != null) {
            if (keys.containsKey(pattern)) {
                throw new IllegalStateException("");
            }
            keys.put(pattern, value);

            final Pattern p = Pattern.compile(pattern);

            patternMap.put(p, value);
        } else {
            nullValue = value;
        }
    }


    public int size() {
        return patternMap.size();
    }


    public Set keySet() {
        return patternMap.keySet();
    }


    public Collection values() {
        return patternMap.values();
    }


    public List get(final String string) {
        List vals = null;

        if (!Strings.isNullOrEmpty(string)) {
            final Set> entrySet = patternMap.entrySet();

            for (final Entry e : entrySet) {

                if (e.getKey().matcher(string).matches()) {

                    if (vals == null) {
                        vals = new ArrayList();
                    }
                    vals.add(e.getValue());
                }
            }
        } else {
            if (nullValue != null) {
                vals = new ArrayList();
                vals.add(nullValue);
            }
        }
        if (vals == null) {
            vals = Collections.emptyList();
        }

        return vals;
    }


    /**
     * @param param
     * @param i
     * 
     * @return
     */
    public V get(final String param, final int idx) {
        V rtn = null;
        if (param != null) {
            final List list = this.get(param);
            if (list != null && !list.isEmpty() && list.size() > idx) {
                rtn = list.get(idx);
            }
        } else {
            rtn = nullValue;
        }
        return rtn;
    }


    /**
     * @param pattern
     * @return
     */
    public boolean containsPattern(final String pattern) {
        return keys.containsKey(pattern);
    }


    /**
     * @param newPattern
     * @return
     */
    public V getValueForPattern(final String pattern) {
        return keys.get(pattern);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy