All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.api.internal.DefaultPolymorphicNamedEntityInstantiator Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.api.internal;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.gradle.api.GradleException;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.NamedDomainObjectFactory;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.gradle.internal.Cast.uncheckedCast;

public class DefaultPolymorphicNamedEntityInstantiator implements PolymorphicNamedEntityInstantiator {
    private final Map, NamedDomainObjectFactory> factories = Maps.newHashMap();
    private final Class baseType;
    private final String displayName;

    public DefaultPolymorphicNamedEntityInstantiator(Class type, String displayName) {
        this.displayName = displayName;
        this.baseType = type;
    }

    @Override
    public  S create(String name, Class type) {
        @SuppressWarnings("unchecked")
        NamedDomainObjectFactory factory = (NamedDomainObjectFactory) factories.get(type);
        if (factory == null) {
            throw new InvalidUserDataException(
                    String.format("Cannot create a %s because this type is not known to %s. Known types are: %s", type.getSimpleName(), displayName, getSupportedTypeNames()),
                    new NoFactoryRegisteredForTypeException());
        }
        return factory.create(name);
    }

    public String getSupportedTypeNames() {
        List names = Lists.newArrayList();
        for (Class clazz : factories.keySet()) {
            names.add(clazz.getSimpleName());
        }
        Collections.sort(names);
        return names.isEmpty() ? "(None)" : Joiner.on(", ").join(names);
    }

    @Override
    public  void registerFactory(Class type, NamedDomainObjectFactory factory) {
        if (!baseType.isAssignableFrom(type)) {
            String message = String.format("Cannot register a factory for type %s because it is not a subtype of container element type %s.", type.getSimpleName(), baseType.getSimpleName());
            throw new IllegalArgumentException(message);
        }
        if(factories.containsKey(type)){
            throw new GradleException(String.format("Cannot register a factory for type %s because a factory for this type is already registered.", type.getSimpleName()));
        }
        factories.put(type, factory);
    }

    @Override
    public Set> getCreatableTypes() {
        return ImmutableSet.copyOf(factories.keySet());
    }

    public void copyFactoriesFrom(DefaultPolymorphicNamedEntityInstantiator source) {
        for (Class languageType : source.factories.keySet()) {
            copyFactory(source, languageType);
        }
    }

     void copyFactory(DefaultPolymorphicNamedEntityInstantiator source, Class type) {
        NamedDomainObjectFactory factory = uncheckedCast(source.factories.get(type));
        registerFactory(type, factory);
    }
}