View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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  package org.apache.commons.scxml2.env;
18  
19  import java.io.Serializable;
20  import java.util.AbstractMap;
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  import org.apache.commons.scxml2.Context;
25  
26  /**
27   * A map that will back the effective {@link Context} for an {@link org.apache.commons.scxml2.Evaluator} execution.
28   * The effective context enables the chaining of contexts all the way from the current state node to the root.
29   */
30  public final class EffectiveContextMap extends AbstractMap<String, Object> implements Serializable {
31  
32      private static final long serialVersionUID = 1L;
33  
34      /** The {@link org.apache.commons.scxml2.Context} for the current state. */
35      private final Context leaf;
36  
37      /** Constructor. */
38      public EffectiveContextMap(final Context ctx) {
39          super();
40          this.leaf = ctx;
41      }
42  
43      /**
44       * {@inheritDoc}
45       */
46      @Override
47      public Set<Entry<String, Object>> entrySet() {
48          Set<Entry<String, Object>> entrySet = new HashSet<Entry<String, Object>>();
49          Context current = leaf;
50          while (current != null) {
51              entrySet.addAll(current.getVars().entrySet());
52              current = current.getParent();
53          }
54          return entrySet;
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      @Override
61      public Object put(final String key, final Object value) {
62          Object old = leaf.get(key);
63          if (leaf.has(key)) {
64              leaf.set(key, value);
65          } else {
66              leaf.setLocal(key, value);
67          }
68          return old;
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public Object get(final Object key) {
76          if (key != null) {
77              Context current = leaf;
78              while (current != null) {
79                  if (current.getVars().containsKey(key.toString())) {
80                      return current.getVars().get(key);
81                  }
82                  current = current.getParent();
83              }
84          }
85          return null;
86      }
87  }