001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.repository.capabilities;
016    
017    import java.util.HashMap;
018    import java.util.HashSet;
019    import java.util.Map;
020    import java.util.Set;
021    
022    /**
023     * @author Adolfo P??rez
024     */
025    public abstract class BaseCapabilityProvider implements CapabilityProvider {
026    
027            @Override
028            public <S extends Capability> S getCapability(Class<S> capabilityClass) {
029                    if (_exportedCapabilityClasses.contains(capabilityClass)) {
030                            Capability capability = getInternalCapability(capabilityClass);
031    
032                            if (capability == null) {
033                                    throw new IllegalArgumentException(
034                                            String.format(
035                                                    "Capability %s is not supported by provider %s",
036                                                    capabilityClass.getName(), getProviderKey()));
037                            }
038    
039                            return (S)capability;
040                    }
041    
042                    throw new IllegalArgumentException(
043                            String.format(
044                                    "Capability %s is not exported by provider %s",
045                                    capabilityClass.getName(), getProviderKey()));
046            }
047    
048            @Override
049            public <S extends Capability> boolean isCapabilityProvided(
050                    Class<S> capabilityClass) {
051    
052                    return _exportedCapabilityClasses.contains(capabilityClass);
053            }
054    
055            protected <S extends Capability> void addExportedCapability(
056                    Class<S> capabilityClass, S capability) {
057    
058                    addSupportedCapability(capabilityClass, capability);
059    
060                    _exportedCapabilityClasses.add(capabilityClass);
061            }
062    
063            protected <S extends Capability> void addSupportedCapability(
064                    Class<S> capabilityClass, S capability) {
065    
066                    if (_supportedCapabilities.containsKey(capabilityClass)) {
067                            throw new IllegalStateException(
068                                    "Capability " + capabilityClass.getName() + " already exists");
069                    }
070    
071                    _supportedCapabilities.put(capabilityClass, capability);
072            }
073    
074            protected Map<Class<? extends Capability>, Capability> getCapabilities() {
075                    return _supportedCapabilities;
076            }
077    
078            protected <S extends Capability> S getInternalCapability(
079                    Class<S> capabilityClass) {
080    
081                    return (S)_supportedCapabilities.get(capabilityClass);
082            }
083    
084            protected abstract String getProviderKey();
085    
086            private final Set<Class<? extends Capability>> _exportedCapabilityClasses =
087                    new HashSet<>();
088            private final Map<Class<? extends Capability>, Capability>
089                    _supportedCapabilities = new HashMap<>();
090    
091    }