001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.scxml2.env.minimal;
018
019import java.util.Collections;
020import java.util.Map;
021
022import org.apache.commons.scxml2.Context;
023import org.apache.commons.scxml2.env.SimpleContext;
024
025/**
026 * MinimalContext implementation for the SCXML Null Data Model.
027 * <p>
028 * This context disables any access to the parent context (other than through getParent()) and
029 * ignores setting any local data.
030 * </p>
031 * <p>
032 * The MinimalContext requires a none MinimalContext based parent Context for creation.
033 * If the parent context is of type MinimalContext, <em>its</em> parent will be used as the parent of the
034 * new MinimalContext instance
035 * </p>
036 */
037public class MinimalContext extends SimpleContext {
038
039    /** Serial version UID. */
040    private static final long serialVersionUID = 1L;
041
042    private static Context getMinimalContextParent(final Context parent) {
043        if (parent != null) {
044            if (parent instanceof MinimalContext) {
045                return getMinimalContextParent(parent.getParent());
046            }
047            return parent;
048        }
049        throw new IllegalStateException("A MinimalContext instance requires a non MinimalContext based parent.");
050    }
051
052    public MinimalContext(final Context parent) {
053        super(getMinimalContextParent(parent));
054    }
055
056    @Override
057    public void set(final String name, final Object value) {
058    }
059
060    @Override
061    public Object get(final String name) {
062        return null;
063    }
064
065    @Override
066    public boolean has(final String name) {
067        return false;
068    }
069
070    @Override
071    public boolean hasLocal(final String name) {
072        return false;
073    }
074
075    @Override
076    public void reset() {
077    }
078
079    @Override
080    public void setLocal(final String name, final Object value) {
081    }
082
083    @Override
084    protected void setVars(final Map<String, Object> vars) {
085    }
086
087    @Override
088    public Map<String, Object> getVars() {
089        return Collections.emptyMap();
090    }
091}