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.plugins.repository.model;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.List;
021
022import org.ametys.plugins.repository.model.CompositeDefinition;
023import org.ametys.runtime.model.DefinitionAndValue;
024import org.ametys.runtime.model.ModelItem;
025import org.ametys.runtime.model.ModelItemGroup;
026
027/**
028 * Pair of a value and its definition for a composite
029 * @param <T> Type of the root object
030 */
031public class CompositeDefinitionAndValue<T> extends DefinitionAndValue<T>
032{
033
034    private CompositeDefinition _compositeDefinition;
035    private List<DefinitionAndValue> _children = new ArrayList<>();
036    
037    /**
038     * Creates a composite definition and value pair
039     * @param root the root object of this definition and value pair
040     * @param definition the definition
041     * @param value the value
042     * @param children the children of the definition and value pair
043     */
044    public CompositeDefinitionAndValue(T root, ModelItem definition, Object value, DefinitionAndValue... children)
045    {
046        this(root, definition, value, null, children);
047    }
048    
049    /**
050     * Creates a composite definition and value pair
051     * @param root the root object of this definition and value pair
052     * @param definition the definition
053     * @param value the value
054     * @param parent the parent of the definition and value pair
055     * @param children the children of the definition and value pair
056     */
057    public CompositeDefinitionAndValue(T root, ModelItem definition, Object value, DefinitionAndValue<T> parent, DefinitionAndValue... children)
058    {
059        super(root, definition, value, parent);
060        
061        if (definition instanceof CompositeDefinition)
062        {
063            _compositeDefinition = (CompositeDefinition) definition;
064        }
065        else
066        {
067            throw new IllegalArgumentException("The definition of a composite definition and value pair must be a " + CompositeDefinition.class.getName() + ".");
068        }
069        
070        _children = Arrays.asList(children);
071    }
072    
073    @Override
074    public ModelItemGroup getDefinition()
075    {
076        return _compositeDefinition;
077    }
078    
079    /**
080     * Retrieves the children definition and value pairs of this composite 
081     * @return the children
082     */
083    public List<DefinitionAndValue> getChildren()
084    {
085        return _children;
086    }
087    
088    /**
089     * Add a definition and value pair as a child of this composite
090     * @param definitionAndValue the definition and value pair to add as child
091     */
092    public void addChild(DefinitionAndValue definitionAndValue)
093    {
094        _children.add(definitionAndValue);
095    }
096}