001/*
002 *  Copyright 2021 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.forms.question.sources;
017
018import java.util.HashMap;
019import java.util.Map;
020
021/**
022 * Class for creating choice options
023 */
024public class ChoiceOption
025{
026    private Object _value;
027    
028    /**
029     * Create a {@link ChoiceOption}
030     * @param value the value of the option
031     */
032    public ChoiceOption(Object value) 
033    {
034        _value = value;
035    }
036    
037    /**
038     * Get the option value
039     * @return the value
040     */
041    public Object getValue()
042    {
043        return _value;
044    }
045    
046    /**
047     * Set the new value
048     * @param value the new value
049     */
050    public void setValue(Object value)
051    {
052        _value = value;
053    }
054
055    /**
056     * Get all attributes for this option
057     * @return the attributes 
058     */
059    public Map<String, Object> getAttributes() 
060    {
061        Map<String, Object> attrs = new HashMap<>();
062        attrs.put("value", _value);
063        return attrs;
064    }
065    
066    @Override
067    public String toString()
068    {
069        return _value.toString();
070    }
071    
072    @Override
073    public boolean equals(Object obj)
074    {
075        if (obj == null)
076        {
077            return false;
078        }
079
080        if (!(obj instanceof ChoiceOption))
081        {
082            return false;
083        }
084
085        if (this == obj)
086        {
087            return true;
088        }
089        
090        return ((ChoiceOption) obj).getValue().equals(_value);
091    }
092    
093    @Override
094    public int hashCode()
095    {
096        return _value.hashCode();
097    }
098}