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