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

org.gradle.api.publication.maven.internal.pom.DefaultConf2ScopeMappingContainer Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * Copyright 2010 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.publication.maven.internal.pom;

import org.gradle.api.InvalidUserDataException;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.maven.Conf2ScopeMapping;
import org.gradle.api.artifacts.maven.Conf2ScopeMappingContainer;
import org.gradle.util.WrapUtil;

import java.util.*;

public class DefaultConf2ScopeMappingContainer implements Conf2ScopeMappingContainer {
    private Map mappings = new HashMap();

    private boolean skipUnmappedConfs = true;

    public DefaultConf2ScopeMappingContainer() {
    }

    public DefaultConf2ScopeMappingContainer(Map mappings) {
        this.mappings.putAll(mappings);
    }

    public Conf2ScopeMapping getMapping(Collection configurations) {
        Set result = getMappingsWithHighestPriority(configurations);
        if (result.size() > 1) {
            throw new InvalidUserDataException(
                    "The configuration to scope mapping is not unique. The following configurations "
                            + "have the same priority: " + result);
        }
        return result.size() == 0 ? null : result.iterator().next();
    }

    private Set getMappingsWithHighestPriority(Collection configurations) {
        Integer lastPriority = null;
        Set result = new HashSet();
        for (Conf2ScopeMapping conf2ScopeMapping : getMappingsForConfigurations(configurations)) {
            Integer thisPriority = conf2ScopeMapping.getPriority();
            if (lastPriority != null && lastPriority.equals(thisPriority)) {
                result.add(conf2ScopeMapping);
            } else if (lastPriority == null || (thisPriority != null && lastPriority < thisPriority)) {
                lastPriority = thisPriority;
                result = WrapUtil.toSet(conf2ScopeMapping);
            }
        }
        return result;
    }

    private List getMappingsForConfigurations(Collection configurations) {
        List existingMappings = new ArrayList();
        for (Configuration configuration : configurations) {
            if (mappings.get(configuration) != null) {
                existingMappings.add(mappings.get(configuration));
            } else {
                existingMappings.add(new Conf2ScopeMapping(null, configuration, null));
            }
        }
        return existingMappings;
    }

    public Conf2ScopeMappingContainer addMapping(int priority, Configuration configuration, String scope) {
        mappings.put(configuration, new Conf2ScopeMapping(priority, configuration, scope));
        return this;
    }

    public Map getMappings() {
        return mappings;
    }

    public boolean isSkipUnmappedConfs() {
        return skipUnmappedConfs;
    }

    public void setSkipUnmappedConfs(boolean skipUnmappedConfs) {
        this.skipUnmappedConfs = skipUnmappedConfs;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        DefaultConf2ScopeMappingContainer that = (DefaultConf2ScopeMappingContainer) o;

        if (!mappings.equals(that.mappings)) {
            return false;
        }

        return true;
    }

    public int hashCode() {
        return mappings.hashCode();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy