org.gradle.api.internal.DefaultPolymorphicNamedEntityInstantiator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* 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 org.gradle.api.GradleException;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.NamedDomainObjectFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DefaultPolymorphicNamedEntityInstantiator implements PolymorphicNamedEntityInstantiator {
private final Map, NamedDomainObjectFactory extends T>> factories = new HashMap<>();
private final Class extends T> baseType;
private final String displayName;
public DefaultPolymorphicNamedEntityInstantiator(Class extends T> 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 = new ArrayList<>();
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 extends U> 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 extends Class extends T>> getCreatableTypes() {
return ImmutableSet.copyOf(factories.keySet());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy