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 com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;

/**
 * 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 the pattern * @param value the 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 getRelaxedValue(final String sourceKey, final String[] keywordPrecedence) { // TODO - lookup in the relaxed map, based on the keyword precedence String baseLine = null; for (final String altKeyword : keywordPrecedence) { baseLine = StringUtils.removeStart(sourceKey, altKeyword); if (!baseLine.equals(sourceKey)) { break; } } List vals = null; for (final String altKeyword : keywordPrecedence) { vals = get(altKeyword + baseLine); if (!vals.isEmpty()) { break; } } return vals; } 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; } 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; } public boolean containsPattern(final String pattern) { return keys.containsKey(pattern); } public V getValueForPattern(final String pattern) { return keys.get(pattern); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy