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

org.gradle.jvm.internal.resolve.DefaultVariantsMetaData Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * Copyright 2016 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.jvm.internal.resolve;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import org.gradle.api.Named;
import org.gradle.model.internal.manage.schema.ModelProperty;
import org.gradle.model.internal.manage.schema.ModelSchema;
import org.gradle.model.internal.manage.schema.StructSchema;
import org.gradle.model.internal.type.ModelType;
import org.gradle.platform.base.BinarySpec;
import org.gradle.platform.base.internal.VariantAspect;

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

public class DefaultVariantsMetaData implements VariantsMetaData {
    private final Map variantCoordinates;
    private final Set allVariantAxes;
    private final Set nonNullVariantAxes;
    private final Map> variantAxisTypes;

    private DefaultVariantsMetaData(Map variantCoordinates, Map> variantAxisTypes) {
        this.variantCoordinates = variantCoordinates;
        this.allVariantAxes = variantCoordinates.keySet();
        this.nonNullVariantAxes = ImmutableSet.copyOf(Maps.filterEntries(variantCoordinates, new Predicate>() {
            @Override
            public boolean apply(Map.Entry input) {
                return input.getValue()!=null;
            }
        }).keySet());
        this.variantAxisTypes = variantAxisTypes;
    }

    public static VariantsMetaData extractFrom(BinarySpec binarySpec, ModelSchema binarySpecSchema) {
        Map variants = Maps.newLinkedHashMap();
        ImmutableMap.Builder> dimensionTypesBuilder = ImmutableMap.builder();
        if (binarySpecSchema instanceof StructSchema) {
            VariantAspect variantAspect = ((StructSchema) binarySpecSchema).getAspect(VariantAspect.class);
            if (variantAspect != null) {
                for (ModelProperty property : variantAspect.getDimensions()) {
                    // note: it's not the role of this class to validate that the annotation is properly used, that
                    // is to say only on a getter returning String or a Named instance, so we trust the result of
                    // the call
                    Object value = property.getPropertyValue(binarySpec);
                    variants.put(property.getName(), value);
                    dimensionTypesBuilder.put(property.getName(), property.getType());
                }
            }
        }
        return new DefaultVariantsMetaData(Collections.unmodifiableMap(variants), dimensionTypesBuilder.build());
    }

    @Override
    public Set getDeclaredVariantAxes() {
        return allVariantAxes;
    }

    @Override
    public Set getNonNullVariantAxes() {
        return nonNullVariantAxes;
    }

    @Override
    public String getValueAsString(String variantAxis) {
        Object o = variantCoordinates.get(variantAxis);
        if (o instanceof Named) {
            return ((Named) o).getName();
        }
        if (o instanceof String) {
            return (String) o;
        }
        return o == null ? null : o.toString();
    }

    @Override
    @SuppressWarnings("unchecked")
    public  T getValueAsType(Class clazz, String variantAxis) {
        return (T) variantCoordinates.get(variantAxis);
    }

    @Override
    public ModelType getVariantAxisType(String variantAxis) {
        return variantAxisTypes.get(variantAxis);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy