/*
** Module   : Substitution.java
** Abstract : mechanism for substituting values at runtime
**
** Copyright (c) 2009, Golden Code Development Corporation.
**
** -#- -I- --Date-- -----------------------Description------------------------
** 001 GES 20090226 Mechanism for substituting values at runtime.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness;

/**
 * Mechanism for substituting values at runtime.
 */
public class Substitution
{
   /** Delegated value setter. */
   private final Setter helper;
   
   /** Substitution specification (uses <code>String.format()</code> rules). */
   private final String spec;
   
   /** Parameter substitutions. */
   private final Variable[] subs;
   
   /**
    * Define a new instance.
    *
    * @param    helper
    *           Delegated value setter.
    * @param    spec
    *           Substitution specification (uses <code>String.format()</code>
    *           rules).
    * @param    subs
    *           Parameter substitutions.
    */
   public Substitution(Setter helper, String spec, Variable[] subs)
   {
      this.helper = helper;
      this.spec   = spec;
      this.subs   = subs;
   }
   
   /**
    * Process the substitutions and set the calculated result.
    */
   public void resolve()
   {
      helper.set(String.format(spec, Variable.resolve(subs)));
   }
}
