/*---------------------------------------------------------------------------*\
 $Id: 1786269f59d2cd9be5ab99ffaf0cc4e82fad9982 $
\*---------------------------------------------------------------------------*/

package org.clapper.util.text;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

/**

/**
 * 

The MultipleMapVariableDereferencer class implements the * VariableDereferencer interface and resolves variable * references by looking them up in one or more supplied Map * objects.

* *

The keys and values in the supplied Map objects * must be String objects.

* *

If more than one contained map has a value for a specific key, the * first map's value will be used. ("First", in this case, means the first * map added to the MultipleMapVariableDereference object.)

* * @see VariableDereferencer * @see VariableSubstituter * @see MapVariableDereferencer * * @version $Revision: 6735 $ * * @author Copyright © 2004-2007 Brian M. Clapper */ public class MultipleMapVariableDereferencer implements VariableDereferencer { /*----------------------------------------------------------------------*\ Private Constants \*----------------------------------------------------------------------*/ /*----------------------------------------------------------------------*\ Private Instance Data \*----------------------------------------------------------------------*/ private Collection> mapList = new ArrayList>(); /*----------------------------------------------------------------------*\ Constructor \*----------------------------------------------------------------------*/ /** * Creates a new instance of MultipleMapVariableDereferencer * that has no associated Map objects. Map objects * can be added with the {@link #addMap} method. */ public MultipleMapVariableDereferencer() { } /** * Creates a new instance of MultipleMapVariableDereferencer * that is initialized with a specified set of maps. * * @param maps the maps to use */ public MultipleMapVariableDereferencer(Map... maps) { for (Map map : maps) addMap(map); } /*----------------------------------------------------------------------*\ Public Methods \*----------------------------------------------------------------------*/ /** * Add a map to the list of maps to use when dereferencing variable values. * The map is added to the end of the list. * * @param map The map to add */ public final void addMap(Map map) { mapList.add(map); } /** * Get the value associated with a given variable. * * @param varName The name of the variable for which the value is * desired. * @param context a context object, passed through from the caller * to the dereferencer, or null if there isn't one. * Ignored here. * * @return The variable's value. If the variable has no value, this * method must return null. */ public String getVariableValue (String varName, Object context) { String result = null; for (Map map : mapList) { result = map.get(varName); if (result != null) break; } return result; } }

Ads help maintain this website.