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

org.gradle.plugins.ide.internal.resolver.model.IdeDependencyKey Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * Copyright 2014 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.plugins.ide.internal.resolver.model;

import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import org.gradle.plugins.ide.idea.model.Dependency;

/**
 * Key used to merge same dependency extracted from different configurations.
 */
public abstract class IdeDependencyKey {


    public static IdeDependencyKey forRepoFileDependency(
            IdeExtendedRepoFileDependency dependency, DependencyBuilder dependencyBuilder) {
        return new RepoFileDependencyKey(dependency, dependencyBuilder);
    }

    public interface DependencyBuilder {
        R buildDependency(T dependency, String scope);
    }

    public static  IdeDependencyKey forProjectDependency(
            IdeProjectDependency dependency, DependencyBuilder dependencyBuilder) {
        return new ProjectDependencyKey(dependency, dependencyBuilder);
    }

    public static  IdeDependencyKey forLocalFileDependency(
            IdeLocalFileDependency dependency, DependencyBuilder dependencyBuilder) {
        return new LocalFileDependencyKey(dependency, dependencyBuilder);
    }

    protected final T ideDependency;
    private final DependencyBuilder dependencyBuilder;

    protected IdeDependencyKey(T ideDependency, DependencyBuilder dependencyBuilder) {
        this.ideDependency = Preconditions.checkNotNull(ideDependency);
        this.dependencyBuilder = Preconditions.checkNotNull(dependencyBuilder);
    }

    public R buildDependency(String scope) {
        return dependencyBuilder.buildDependency(ideDependency, scope);
    }

    protected abstract boolean isSameDependency(IdeDependency otherDependency);

    protected abstract int dependencyHashCode();

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof IdeDependencyKey)) {
            return false;
        }

        IdeDependencyKey that = (IdeDependencyKey) o;

        return isSameDependency(that.ideDependency);
    }

    @Override
    public int hashCode() {
        return dependencyHashCode();
    }

    private static class LocalFileDependencyKey extends IdeDependencyKey {
        private LocalFileDependencyKey(IdeLocalFileDependency dependency, DependencyBuilder dependencyBuilder) {
            super(dependency, dependencyBuilder);
        }

        @Override
        protected int dependencyHashCode() {
            return ideDependency.getFile().hashCode();
        }

        @Override
        protected boolean isSameDependency(IdeDependency otherDependency) {
            return otherDependency instanceof IdeLocalFileDependency && Objects.equal(ideDependency.getFile(), ((IdeLocalFileDependency) otherDependency).getFile());
        }

        public String toString() {
            return "LocalFileDependencyKey{" + ideDependency.getFile() + "}";
        }
    }

    private static class ProjectDependencyKey extends IdeDependencyKey {
        private ProjectDependencyKey(IdeProjectDependency dependency, DependencyBuilder dependencyBuilder) {
            super(dependency, dependencyBuilder);
        }

        @Override
        protected int dependencyHashCode() {
            return ideDependency.getProjectId().hashCode();
        }

        @Override
        protected boolean isSameDependency(IdeDependency otherDependency) {
            return otherDependency instanceof IdeProjectDependency && Objects.equal(ideDependency.getProjectId(), ((IdeProjectDependency) otherDependency).getProjectId());
        }

        @Override
        public String toString() {
            return "ProjectDependencyKey{" + ideDependency.getProjectId() + "}";
        }
    }

    private static class RepoFileDependencyKey extends IdeDependencyKey {
        private RepoFileDependencyKey(IdeExtendedRepoFileDependency dependency, DependencyBuilder dependencyBuilder) {
            super(dependency, dependencyBuilder);
        }

        @Override
        protected int dependencyHashCode() {
            int hash = ideDependency.getFile().hashCode();
            hash = 31 * hash + (ideDependency.getId() != null ? ideDependency.getId().hashCode() : 1);
            return hash;
        }

        @Override
        protected boolean isSameDependency(IdeDependency otherDependency) {
            if (!(otherDependency instanceof IdeRepoFileDependency)) {
                return false;
            }
            IdeRepoFileDependency that = (IdeRepoFileDependency) otherDependency;
            return Objects.equal(ideDependency.getFile(), that.getFile()) && Objects.equal(ideDependency.getId(), that.getId());
        }

        @Override
        public String toString() {
            return "RepoFileDependencyKey{" + ideDependency.getId() + "}";
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy