001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.runtime.model;
017
018import java.util.HashMap;
019import java.util.Map;
020
021/**
022 * Pair of a value and its definition
023 * @param <T> Type of the root object
024 */
025public class DefinitionAndValue<T>
026{
027    private T _root;
028    private ModelItem _definition;
029    private Object _value;
030    private DefinitionAndValue<T> _parent;
031    
032    /**
033     * Creates a definition and value pair
034     * @param root the root object of this definition and value pair
035     * @param definition the definition
036     * @param value the value
037     */
038    public DefinitionAndValue(T root, ModelItem definition, Object value)
039    {
040        this(root, definition, value, null);
041    }
042    
043    /**
044     * Creates a definition and value pair
045     * @param root the root object of this definition and value pair
046     * @param definition the definition
047     * @param value the value
048     * @param parent the parent of the definition and value pair
049     */
050    public DefinitionAndValue(T root, ModelItem definition, Object value, DefinitionAndValue<T> parent)
051    {
052        this._root = root;
053        this._definition = definition;
054        this._value = value;
055        this._parent = parent;
056    }
057
058    /**
059     * Retrieves the root object of this definition and value pair
060     * @return the root
061     */
062    public T getRoot()
063    {
064        return _root;
065    }
066    
067    /**
068     * Retrieves the definition
069     * @return the definition
070     */
071    public ModelItem getDefinition()
072    {
073        return _definition;
074    }
075    
076    /**
077     * Retrieves the value
078     * @return the value
079     */
080    public Object getValue()
081    {
082        return _value;
083    }
084    
085    /**
086     * Retrieves the parent of the definition and value pair
087     * @return the parent
088     */
089    public DefinitionAndValue<T> getParent()
090    {
091        return _parent;
092    }
093    
094    /**
095     * extract a map of values from a map of DefinitionAndValue
096     * @param definitionAndValues a map containing definition and values
097     * @return a map containing only values
098     */
099    public static Map<String, Object> extractValues(Map<String, DefinitionAndValue> definitionAndValues)
100    {
101        Map<String, Object> values = new HashMap<>();
102        for (Map.Entry<String, DefinitionAndValue> entry : definitionAndValues.entrySet())
103        {
104            Object value = entry.getValue().getValue();
105            values.put(entry.getKey(), value);
106        }
107        return values;
108    }
109}