001/*
002 *  Copyright 2019 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.plugin;
017
018import java.util.Collection;
019import java.util.List;
020import java.util.stream.Collectors;
021
022import org.apache.commons.collections4.CollectionUtils;
023
024import org.ametys.runtime.plugin.PluginsManager.InactivityCause;
025
026class IncomingDeactivation
027{
028    static enum Type
029    {
030        DEACTIVATED(InactivityCause.DEACTIVATED),
031        OVERRIDDEN(InactivityCause.OVERRIDDEN);
032        
033        private InactivityCause _cause;
034        private Type(InactivityCause cause)
035        {
036            _cause = cause;
037        }
038        InactivityCause getInactivityCause()
039        {
040            return _cause;
041        }
042    }
043    
044    private Type _type;
045    private String _featureId;
046    
047    IncomingDeactivation(Type type, String featureId)
048    {
049        _type = type;
050        _featureId = featureId;
051    }
052    
053    static boolean containsAny(List<String> includedFeatures, Collection<IncomingDeactivation> deactivatedBy)
054    {
055        if (deactivatedBy == null)
056        {
057            return false;
058        }
059        Collection<String> deactivatedByIds = deactivatedBy.stream()
060                .map(IncomingDeactivation::getFeatureId)
061                .collect(Collectors.toList());
062        return CollectionUtils.containsAny(includedFeatures, deactivatedByIds);
063    }
064    
065    static Collection<IncomingDeactivation> intersection(List<String> includedFeatures, Collection<IncomingDeactivation> deactivatedBy)
066    {
067        return deactivatedBy.stream()
068                .filter(incomingDeactivation -> includedFeatures.contains(incomingDeactivation.getFeatureId()))
069                .collect(Collectors.toList());
070    }
071
072    Type getType()
073    {
074        return _type;
075    }
076
077    String getFeatureId()
078    {
079        return _featureId;
080    }
081    
082    @Override
083    public String toString()
084    {
085        return String.format("IncomingDeactivation(%s, %s)", _featureId, _type.name());
086    }
087}