All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.camunda.bpm.engine.cdi.impl.AbstractVariableMap Maven / Gradle / Ivy

There is a newer version: 7.22.0-alpha5
Show newest version
/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. Camunda licenses this file to you under the Apache License,
 * Version 2.0; you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.camunda.bpm.engine.cdi.impl;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import jakarta.inject.Inject;

import org.camunda.bpm.engine.cdi.BusinessProcess;
import org.camunda.bpm.engine.variable.VariableMap;
import org.camunda.bpm.engine.variable.context.VariableContext;
import org.camunda.bpm.engine.variable.value.TypedValue;

abstract class AbstractVariableMap implements VariableMap {

  @Inject
  protected BusinessProcess businessProcess;

  abstract protected Object getVariable(String variableName);
  abstract protected  T getVariableTyped(String variableName);

  abstract protected void setVariable(String variableName, Object value);

  @Override
  public Object get(Object key) {
    if(key == null) {
      throw new IllegalArgumentException("This map does not support 'null' keys.");
    }
    return getVariable(key.toString());
  }

  @SuppressWarnings("unchecked")
  @Override
  public  T getValue(String name, Class type) {
    Object object = get(name);
    if (object == null) {
      return null;
    } else if (type.isAssignableFrom(object.getClass())) {
      return (T) object;
    } else {
      throw new ClassCastException("Cannot cast variable named '" + name + "' with value '" + object + "' to type '" + type + "'.");
    }
  }

  @Override
  public  T getValueTyped(String name) {
    if (name == null) {
      throw new IllegalArgumentException("This map does not support 'null' keys.");
    }
    return getVariableTyped(name);
  }

  @Override
  public Object put(String key, Object value) {
    if(key == null) {
      throw new IllegalArgumentException("This map does not support 'null' keys.");
    }
    Object variableBefore = getVariable(key);
    setVariable(key, value);
    return variableBefore;
  }

  @Override
  public void putAll(Map< ? extends String, ? extends Object> m) {
    for (java.util.Map.Entry< ? extends String, ? extends Object> newEntry : m.entrySet()) {
      setVariable(newEntry.getKey(), newEntry.getValue());
    }
  }

  @Override
  public VariableMap putValue(String name, Object value) {
    put(name, value);
    return this;
  }

  @Override
  public VariableMap putValueTyped(String name, TypedValue value) {
    if(name == null) {
      throw new IllegalArgumentException("This map does not support 'null' names.");
    }
    setVariable(name, value);
    return this;
  }

  @Override
  public int size() {
    throw new UnsupportedOperationException(getClass().getName()+".size() is not supported.");
  }

  @Override
  public boolean isEmpty() {
    throw new UnsupportedOperationException(getClass().getName()+".isEmpty() is not supported.");
  }

  @Override
  public boolean containsKey(Object key) {
    throw new UnsupportedOperationException(getClass().getName()+".containsKey() is not supported.");
  }

  @Override
  public boolean containsValue(Object value) {
    throw new UnsupportedOperationException(getClass().getName()+".containsValue() is not supported.");
  }

  @Override
  public Object remove(Object key) {
    throw new UnsupportedOperationException(getClass().getName()+".remove is unsupported. Use " + getClass().getName() + ".put(key, null)");
  }

  @Override
  public void clear() {
    throw new UnsupportedOperationException(getClass().getName()+".clear() is not supported.");
  }

  @Override
  public Set keySet() {
    throw new UnsupportedOperationException(getClass().getName()+".keySet() is not supported.");
  }

  @Override
  public Collection values() {
    throw new UnsupportedOperationException(getClass().getName()+".values() is not supported.");
  }

  @Override
  public Set> entrySet() {
    throw new UnsupportedOperationException(getClass().getName()+".entrySet() is not supported.");
  }

  public VariableContext asVariableContext() {
    throw new UnsupportedOperationException(getClass().getName()+".asVariableContext() is not supported.");
  }

}