001/*
002 *  Copyright 2016 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.config;
017
018import java.util.HashSet;
019import java.util.Map;
020import java.util.Set;
021import java.util.TreeMap;
022import java.util.TreeSet;
023
024import org.ametys.runtime.i18n.I18nizableText;
025import org.ametys.runtime.parameter.ParameterCheckerDescriptor;
026import org.ametys.runtime.parameter.ParameterHelper.ParameterType;
027
028/**
029 * Represent a group of parameters.
030 */
031public class ConfigParameterGroup
032{
033    private Map<String, ConfigParameter> _groupParams;
034    private String _switcher;
035    private I18nizableText _groupLabel;
036    private Set<ConfigParameterCheckerDescriptor> _paramCheckers;
037    
038    /**
039     * Create a group
040     * @param groupLabel The label of the group
041     */
042    public ConfigParameterGroup (I18nizableText groupLabel)
043    {
044        _groupLabel = groupLabel;
045        _groupParams = new TreeMap<>();
046        _switcher = null;
047        _paramCheckers = new HashSet<>();
048    }
049    
050    void addParam(ConfigParameter param)
051    {
052        String id = param.getId();
053        _groupParams.put(id, param);
054        
055        if (param.isGroupSwitch())
056        {
057            if (_switcher == null)
058            {
059                _switcher = id;
060                if (param.getType() != ParameterType.BOOLEAN)
061                {
062                    throw new RuntimeException("The group '" + _groupLabel.toString() + "' has a switch '" + _switcher + "' that is not valid because it is not a boolean.");
063                }
064            }
065            else
066            {
067                throw new RuntimeException("At least two group-switches have been defined for the configuration group '" + _groupLabel.toString() + "'. These parameters are '" + _switcher + "' and '" + param.getId() + "'.");
068            }
069        }
070    }
071    
072    void addParamChecker(ConfigParameterCheckerDescriptor paramChecker)
073    {
074        _paramCheckers.add(paramChecker);
075    }
076    
077    /**
078     * Returns the {@link ParameterCheckerDescriptor}s associated with this group.
079     * @return the {@link ParameterCheckerDescriptor}s associated with this group.
080     */
081    public Set<ConfigParameterCheckerDescriptor> getParamCheckers()
082    {
083        return _paramCheckers;
084    }
085    
086    /**
087     * Returns the label.
088     * @return the label.
089     */
090    public I18nizableText getLabel()
091    {
092        return _groupLabel;
093    }
094    
095    /**
096     * Returns the {@link ConfigParameter} contained in this group.
097     * @param withSwitch if the returned parameters should contains the group switcher, if any.
098     * @return the {@link ConfigParameter} contained in this group.
099     */
100    public Set<ConfigParameter> getParams(boolean withSwitch)
101    {
102        if (withSwitch)
103        {
104            return new TreeSet<>(_groupParams.values());
105        }
106        else
107        {
108            Map<String, ConfigParameter> groupParams = new TreeMap<>(_groupParams);
109            
110            if (_switcher != null)
111            {
112                groupParams.remove(_switcher);
113            }
114            
115            return new TreeSet<>(groupParams.values());
116        }
117    }
118    
119    /**
120     * Returns the named {@link ConfigParameter}.
121     * @param id the id to retrieve.
122     * @return the named {@link ConfigParameter}.
123     */
124    public ConfigParameter getParameter(String id)
125    {
126        return _groupParams.get(id);
127    }
128    
129    /**
130     * Returns the group switch, if any.
131     * @return the group switch, if any.
132     */
133    public String getSwitch()
134    {
135        return _switcher;
136    }
137}