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

org.milyn.delivery.ContentHandlerConfigMapTable Maven / Gradle / Ivy

There is a newer version: 1.7.1
Show newest version
/*
	Milyn - Copyright (C) 2006

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License (version 2.1) as published by the Free Software
	Foundation.

	This library 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:
	http://www.gnu.org/licenses/lgpl.txt
*/
package org.milyn.delivery;

import org.milyn.cdr.SmooksResourceConfiguration;
import org.milyn.delivery.ordering.Sorter;

import java.util.*;

/**
 * Simple table for storing {@link ContentHandlerConfigMap} lists against a selector string.
 * 
 * @author [email protected]
 */
public class ContentHandlerConfigMapTable {

    private Map>> table = new LinkedHashMap>>();
    private int count = 0;

    /**
     * Add a delivery unit mapping for the specified selector.
     *
     * @param elementName The target element for the content handler.
     * @param resourceConfig Resource configuration.
     * @param contentHandler The delivery unit.
     */
    public void addMapping(String elementName, SmooksResourceConfiguration resourceConfig, T contentHandler) {
        addMapping(elementName, new ContentHandlerConfigMap(contentHandler, resourceConfig));
    }

    /**
     * Add a mapping for the specified element.
     * @param elementName The element name.
     * @param mapInst The mapping instance to be added.
     */
    private void addMapping(String elementName, ContentHandlerConfigMap mapInst) {
        List> elementMappings = table.get(elementName.toLowerCase());

        if(elementMappings == null) {
            elementMappings = new Vector>();
            table.put(elementName.toLowerCase(), elementMappings);
        }
        elementMappings.add(mapInst);
        count++;
    }

    /**
     * Add all the content handlers defined in the supplied configMap.
     * @param configMap The config map.
     */
    public void addAll(ContentHandlerConfigMapTable configMap) {
        Set>>> mappingsES = configMap.table.entrySet();

        for (Map.Entry>> elementMappings : mappingsES) {
            String elementName = elementMappings.getKey();
            List> mappingList = elementMappings.getValue();

            for (ContentHandlerConfigMap mapping : mappingList) {
                addMapping(elementName, mapping);
            }
        }
    }

    public Map>> getTable() {
        return Collections.unmodifiableMap(table);
    }

    /**
     * Get the {@link ContentHandlerConfigMap} list for the supplied selector string.
     * @param selector The lookup selector.
     * @return It's list of {@link ContentHandlerConfigMap} instances, or null if there are none.
     */
    public List> getMappings(String selector) {
        return table.get(selector.toLowerCase());
    }

    /**
     * Get the combined {@link ContentHandlerConfigMap} list for the supplied list of selector strings.
     * @param selectors The lookup selectors.
     * @return The combined {@link ContentHandlerConfigMap} list for the supplied list of selector strings,
     * or an empty list if there are none.
     */
    public List> getMappings(String[] selectors) {
        List> combinedList = new ArrayList>();

        for(String selector : selectors) {
            List> selectorList = table.get(selector.toLowerCase());
            if(selectorList != null) {
                combinedList.addAll(selectorList);
            }
        }

        return combinedList;
    }

    /**
     * Is the table empty.
     * @return True if the table is empty, otherwise false.
     */
    public boolean isEmpty() {
        return table.isEmpty();
    }

    /**
     * Get the total number of mappings on this table.
     * @return The total number of mappings on this table.
     */
    public int getCount() {
        return count;
    }

    /**
     * Sort the Table in the specified sort order.
     * @param sortOrder The sort order.
     */
    public void sort(Sorter.SortOrder sortOrder) {
        Set>>> tableEntries = table.entrySet();

        for(Map.Entry>> tableEntry : tableEntries) {
            Sorter.sort(tableEntry.getValue(), sortOrder);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy