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.web.frontoffice.search.metamodel;
017
018import java.util.HashMap;
019import java.util.Map;
020import java.util.Objects;
021import java.util.Set;
022
023/**
024 * This class holds the current values of the additional parameters
025 */
026public class AdditionalParameterValueMap
027{
028    private Map<String, Object> _values;
029    private Set<String> _notDisplayableParameterIds;
030    
031    /**
032     * Builds an AdditionalParameterValueMap
033     * @param values The values
034     * @param notDisplayableParameterIds The parameter ids which are not displayable to an end-user
035     */
036    protected AdditionalParameterValueMap(Map<String, Object> values, Set<String> notDisplayableParameterIds)
037    {
038        _values = new HashMap<>(values);
039        _notDisplayableParameterIds = notDisplayableParameterIds;
040    }
041    
042    /**
043     * Gets the value of the given additional parameter
044     * @param <T> the type of the value
045     * @param parameterId The id of the parameter
046     * @return the value of the given additional parameter
047     * @throws ClassCastException if the value cannot be cast to the expected type
048     */
049    public <T> T getValue(String parameterId) throws ClassCastException
050    {
051        @SuppressWarnings("unchecked")
052        T value = (T) _values.get(parameterId);
053        return value;
054    }
055    
056    /**
057     * Gets the parameter ids that have a value
058     * @return the parameter ids that have a value
059     */
060    public Set<String> getParameterIds()
061    {
062        return _values.keySet();
063    }
064    
065    /**
066     * <b>Expert method, for debug purpose, use only if you know what you do</b>
067     * <br>Gets the value for display purpose.
068     * @param parameterId The id of the parameter
069     * @param defaultValue The value to display if it is not displayable (for security purpose for instance)
070     * @return the value for display purpose.
071     */
072    public String getDisplayableValue(String parameterId, String defaultValue)
073    {
074        if (_notDisplayableParameterIds.contains(parameterId))
075        {
076            return defaultValue;
077        }
078        return Objects.toString(getValue(parameterId));
079    }
080}