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