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.core.model.type;
017
018import java.util.List;
019
020import org.apache.commons.lang3.StringUtils;
021
022/**
023 * Abstract class for boolean element types
024 */
025public abstract class AbstractBooleanElementType extends AbstractElementType<Boolean>
026{
027    @Override
028    public Boolean convertValue(Object value)
029    {
030        if (value instanceof String)
031        {
032            if (StringUtils.isEmpty((String) value))
033            {
034                return null;
035            }
036            
037            return Boolean.valueOf((String) value);
038        }
039        
040        return super.convertValue(value);
041    }
042
043    @Override
044    public Object fromJSONForClient(Object json)
045    {
046        if (json == null || json instanceof Boolean)
047        {
048            return json;
049        }
050        else if (json instanceof String)
051        {
052            return convertValue(json);
053        }
054        else if (json instanceof List)
055        {
056            @SuppressWarnings("unchecked")
057            List<Object> jsonList = (List<Object>) json;
058            Boolean[] result = new Boolean[jsonList.size()];
059            for (int i = 0; i < jsonList.size(); i++)
060            {
061                Object singleJSON = jsonList.get(i);
062                if (singleJSON instanceof Boolean)
063                {
064                    result[i] = (Boolean) singleJSON;
065                }
066                else if (singleJSON instanceof String)
067                {
068                    result[i] = convertValue(singleJSON);
069                }
070                else
071                {
072                    throw new IllegalArgumentException("Try to convert the non Boolean JSON object '" + json + "' into a Boolean");
073                }
074            }
075            return result;
076        }
077        else
078        {
079            throw new IllegalArgumentException("Try to convert the non Boolean JSON object '" + json + "' into a Boolean");
080        }
081    }
082    
083    public boolean isSimple()
084    {
085        return true;
086    }
087    
088    @Override
089    public boolean isCompatible(Object value)
090    {
091        return super.isCompatible(value) || value instanceof boolean[];
092    }
093}