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