Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.model.internal.fixture;
import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectFactory;
import org.gradle.api.Transformer;
import org.gradle.api.internal.DefaultPolymorphicNamedEntityInstantiator;
import org.gradle.api.internal.PolymorphicNamedEntityInstantiator;
import org.gradle.api.internal.rules.DefaultRuleAwarePolymorphicNamedEntityInstantiator;
import org.gradle.api.internal.rules.RuleAwarePolymorphicNamedEntityInstantiator;
import org.gradle.internal.Actions;
import org.gradle.internal.BiAction;
import org.gradle.internal.Factories;
import org.gradle.internal.Factory;
import org.gradle.model.ModelMap;
import org.gradle.model.RuleSource;
import org.gradle.model.internal.core.ChildNodeInitializerStrategyAccessors;
import org.gradle.model.internal.core.ModelAction;
import org.gradle.model.internal.core.ModelActionRole;
import org.gradle.model.internal.core.ModelMapModelProjection;
import org.gradle.model.internal.core.ModelNode;
import org.gradle.model.internal.core.ModelPath;
import org.gradle.model.internal.core.ModelReference;
import org.gradle.model.internal.core.ModelRegistration;
import org.gradle.model.internal.core.ModelRegistrations;
import org.gradle.model.internal.core.MutableModelNode;
import org.gradle.model.internal.core.NodeBackedModelMap;
import org.gradle.model.internal.core.NodeInitializerContext;
import org.gradle.model.internal.core.NodeInitializerRegistry;
import org.gradle.model.internal.core.NodePredicate;
import org.gradle.model.internal.core.UnmanagedModelProjection;
import org.gradle.model.internal.core.rule.describe.SimpleModelRuleDescriptor;
import org.gradle.model.internal.registry.DefaultModelRegistry;
import org.gradle.model.internal.registry.ModelRegistry;
import org.gradle.model.internal.type.ModelType;
import org.gradle.model.internal.type.ModelTypes;
import javax.annotation.Nullable;
import java.util.Set;
import static org.gradle.model.internal.core.ModelActionRole.Initialize;
import static org.gradle.model.internal.core.ModelActionRole.Mutate;
import static org.gradle.model.internal.core.NodeInitializerContext.forType;
/**
* A helper for adding rules to a model registry.
*
* Allows unsafe use of the model registry by allow registering of rules that can close over external, unmanaged, state.
*/
public class ModelRegistryHelperExtension {
// ModelRegistry methods
public static MutableModelNode atState(DefaultModelRegistry modelRegistry, String path, ModelNode.State state) {
return (MutableModelNode) modelRegistry.atState(ModelPath.path(path), state);
}
public static MutableModelNode atStateOrLater(ModelRegistry modelRegistry, String path, ModelNode.State state) {
return (MutableModelNode) modelRegistry.atStateOrLater(ModelPath.path(path), state);
}
public static ModelNode.State state(ModelRegistry modelRegistry, String path) {
return modelRegistry.state(ModelPath.path(path));
}
@Nullable
public static MutableModelNode node(DefaultModelRegistry modelRegistry, String path) {
return modelRegistry.node(ModelPath.path(path));
}
public static ModelRegistry registerInstance(ModelRegistry modelRegistry, String path, final C c) {
return modelRegistry.register(unmanaged(registration(ModelPath.path(path)), c));
}
public static ModelRegistry registerInstance(ModelRegistry modelRegistry, String path, final C c, Action super C> action) {
return modelRegistry.register(unmanaged(registration(ModelPath.path(path)), c, action));
}
public static ModelRegistry registerWithInitializer(ModelRegistry modelRegistry, String path, Class type, NodeInitializerRegistry nodeInitializerRegistry) {
NodeInitializerContext nodeInitializerContext = forType(ModelType.of(type));
ModelRegistration registration = ModelRegistrations.of(ModelPath.path(path), nodeInitializerRegistry.getNodeInitializer(nodeInitializerContext)).descriptor("create " + path).build();
modelRegistry.register(registration);
return modelRegistry;
}
public static ModelRegistry register(ModelRegistry modelRegistry, String path, Transformer extends ModelRegistration, ? super ModelRegistrations.Builder> definition) {
return register(modelRegistry, ModelPath.path(path), definition);
}
public static ModelRegistry register(ModelRegistry modelRegistry, ModelPath path, Transformer extends ModelRegistration, ? super ModelRegistrations.Builder> definition) {
return modelRegistry.register(definition.transform(registration(path)));
}
public static ModelRegistry registerModelMap(ModelRegistry modelRegistry, String path, final Class itemType, final Action super PolymorphicNamedEntityInstantiator> registrations) {
configure(modelRegistry, Initialize, ModelReference.of(path, ModelRegistryHelper.instantiatorType(itemType)), new Action>() {
@Override
public void execute(final RuleAwarePolymorphicNamedEntityInstantiator instantiator) {
registrations.execute(new PolymorphicNamedEntityInstantiator() {
@Override
public Set extends Class extends I>> getCreatableTypes() {
return instantiator.getCreatableTypes();
}
@Override
public void registerFactory(Class type, NamedDomainObjectFactory extends U> factory) {
instantiator.registerFactory(type, factory, new SimpleModelRuleDescriptor("ModelRegistryHelper.modelMap"));
}
@Override
public S create(String name, Class type) {
return instantiator.create(name, type);
}
});
}
});
return register(modelRegistry, path, new Transformer() {
@Override
public ModelRegistration transform(ModelRegistrations.Builder modelRegistrationBuilder) {
return modelMap(modelRegistrationBuilder, itemType);
}
});
}
public static ModelRegistry mutateModelMap(ModelRegistry modelRegistry, final String path, final Class itemType, final Action super ModelMap> action) {
return mutate(modelRegistry, new Transformer>() {
@Override
public ModelAction transform(ModelActionBuilder