View Javadoc

    1   /*
    2    *
    3    *   Copyright 2005-2006 The Apache Software Foundation.
    4    *
    5    *  Licensed under the Apache License, Version 2.0 (the "License");
    6    *  you may not use this file except in compliance with the License.
    7    *  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   *  Unless required by applicable law or agreed to in writing, software
    12   *  distributed under the License is distributed on an "AS IS" BASIS,
    13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   *  See the License for the specific language governing permissions and
    15   *  limitations under the License.
    16   *
    17   */
    18  package org.apache.commons.scxml.env;
    19
    20  import java.util.HashMap;
    21  import java.util.Map;
    22
    23  import org.apache.commons.logging.Log;
    24  import org.apache.commons.logging.LogFactory;
    25  import org.apache.commons.scxml.Context;
    26
    27  /***
    28   * Simple Context wrapping a map of variables.
    29   *
    30   */
    31  public class SimpleContext implements Context {
    32
    33      /*** Implementation independent log category. */
    34      private Log log = LogFactory.getLog(Context.class);
    35      /*** The parent Context to this Context. */
    36      private Context parent;
    37      /*** The Map of variables and their values in this Context. */
    38      private Map vars;
    39
    40      /***
    41       * Constructor.
    42       *
    43       */
    44      public SimpleContext() {
    45          this(null, null);
    46      }
    47
    48      /***
    49       * Constructor.
    50       *
    51       * @param parent A parent Context, can be null
    52       */
    53      public SimpleContext(final Context parent) {
    54          this(parent, null);
    55      }
    56      /***
    57       * Constructor.
    58       *
    59       * @param initialVars A pre-populated initial variables map
    60       */
    61      public SimpleContext(final Map initialVars) {
    62          this(null, initialVars);
    63      }
    64
    65      /***
    66       * Constructor.
    67       *
    68       * @param parent A parent Context, can be null
    69       * @param initialVars A pre-populated initial variables map
    70       */
    71      public SimpleContext(final Context parent, final Map initialVars) {
    72          this.parent = parent;
    73          if (initialVars == null) {
    74              this.vars = new HashMap();
    75          } else {
    76              this.vars = initialVars;
    77          }
    78      }
    79
    80      /***
    81       * Assigns a new value to an existing variable or creates a new one.
    82       * The method searches the chain of parent Contexts for variable
    83       * existence.
    84       *
    85       * @param name The variable name
    86       * @param value The variable value
    87       * @see org.apache.commons.scxml.Context#set(String, Object)
    88       */
    89      public void set(final String name, final Object value) {
    90          if (vars.containsKey(name)) { //first try to override local
    91              setLocal(name, value);
    92          } else if (parent != null && parent.has(name)) { //then check for global
    93              parent.set(name, value);
    94          } else { //otherwise create a new local variable
    95              setLocal(name, value);
    96          }
    97      }
    98
    99      /***
    100      * Get the value of this variable; delegating to parent.
    101      *
    102      * @param name The variable name
    103      * @return Object The variable value
    104      * @see org.apache.commons.scxml.Context#get(java.lang.String)
    105      */
    106     public Object get(final String name) {
    107         if (vars.containsKey(name)) {
    108             return vars.get(name);
    109         } else if (parent != null) {
    110             return parent.get(name);
    111         } else {
    112             return null;
    113         }
    114     }
    115
    116     /***
    117      * Check if this variable exists, delegating to parent.
    118      *
    119      * @param name The variable name
    120      * @return boolean true if this variable exists
    121      * @see org.apache.commons.scxml.Context#has(java.lang.String)
    122      */
    123     public boolean has(final String name) {
    124         if (vars.containsKey(name)) {
    125             return true;
    126         } else if (parent != null && parent.has(name)) {
    127             return true;
    128         }
    129         return false;
    130     }
    131
    132     /***
    133      * Clear this Context.
    134      *
    135      * @see org.apache.commons.scxml.Context#reset()
    136      */
    137     public void reset() {
    138         vars.clear();
    139     }
    140
    141     /***
    142      * Get the parent Context, may be null.
    143      *
    144      * @return Context The parent Context
    145      * @see org.apache.commons.scxml.Context#getParent()
    146      */
    147     public Context getParent() {
    148         return parent;
    149     }
    150
    151     /***
    152      * Assigns a new value to an existing variable or creates a new one.
    153      * The method allows to shaddow a variable of the same name up the
    154      * Context chain.
    155      *
    156      * @param name The variable name
    157      * @param value The variable value
    158      * @see org.apache.commons.scxml.Context#setLocal(String, Object)
    159      */
    160     public void setLocal(final String name, final Object value) {
    161         vars.put(name, value);
    162         if (log.isDebugEnabled() && !name.equals("_ALL_STATES")) {
    163             log.debug(name + " = " + String.valueOf(value));
    164         }
    165     }
    166
    167     /***
    168      * Set the variables map.
    169      *
    170      * @param vars The new Map of variables.
    171      */
    172     protected void setVars(final Map vars) {
    173         this.vars = vars;
    174     }
    175
    176     /***
    177      * Get the Map of all local variables in this Context.
    178      *
    179      * @return Returns the vars.
    180      */
    181     public Map getVars() {
    182         return vars;
    183     }
    184
    185     /***
    186      * Set the log used by this <code>Context</code> instance.
    187      *
    188      * @param log The new log.
    189      */
    190     protected void setLog(final Log log) {
    191         this.log = log;
    192     }
    193
    194     /***
    195      * Get the log used by this <code>Context</code> instance.
    196      *
    197      * @return Log The log being used.
    198      */
    199     protected Log getLog() {
    200         return log;
    201     }
    202
    203 }
    204