001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package org.basepom.mojo.dvc.version;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018import static com.google.common.collect.ImmutableSet.toImmutableSet;
019import static com.google.common.collect.ImmutableSetMultimap.toImmutableSetMultimap;
020
021import org.basepom.mojo.dvc.QualifiedName;
022
023import java.util.Collection;
024import java.util.Map;
025import java.util.Objects;
026
027import com.google.common.base.MoreObjects;
028import com.google.common.collect.ImmutableSetMultimap;
029import com.google.common.collect.ImmutableSortedSet;
030import com.google.common.collect.Maps;
031import com.google.common.collect.SetMultimap;
032import org.apache.maven.artifact.versioning.ComparableVersion;
033
034public final class VersionResolutionCollection
035        implements Comparable<VersionResolutionCollection> {
036
037    private final ImmutableSortedSet<VersionResolutionElement> requestingDependencies;
038    private final ComparableVersion expectedVersion;
039
040    private VersionResolutionCollection(Map.Entry<ComparableVersion, Collection<VersionResolutionElement>> entry) {
041        checkNotNull(entry, "entry is null");
042        this.expectedVersion = entry.getKey();
043        this.requestingDependencies = ImmutableSortedSet.copyOf(entry.getValue());
044    }
045
046    public ImmutableSortedSet<VersionResolutionElement> getRequestingDependencies() {
047        return requestingDependencies;
048    }
049
050    public ComparableVersion getExpectedVersion() {
051        return expectedVersion;
052    }
053
054    /**
055     * Matches a given version to the collection.
056     *
057     * @param version The version to match to this collection.
058     * @return True if the selected version exactly matches the expected version.
059     */
060    public boolean isMatchFor(ComparableVersion version) {
061        checkNotNull(version, "version is null");
062        return expectedVersion.getCanonical().equals(version.getCanonical());
063    }
064
065    @Override
066    public int compareTo(final VersionResolutionCollection other) {
067        if (other == null) {
068            return 1;
069        } else if (other == this || equals(other)) {
070            return 0;
071        } else {
072            // order by expected version
073            return this.getExpectedVersion().compareTo(other.getExpectedVersion());
074        }
075    }
076
077    public boolean hasConflict() {
078        return requestingDependencies.stream().anyMatch(VersionResolutionElement::hasConflict);
079    }
080
081    public boolean hasDirectDependencies() {
082        return requestingDependencies.stream().anyMatch(VersionResolutionElement::isDirectDependency);
083    }
084
085    public boolean hasManagedDependencies() {
086        return requestingDependencies.stream().anyMatch(VersionResolutionElement::isManagedDependency);
087    }
088
089    public static ImmutableSetMultimap<QualifiedName, VersionResolutionCollection> toResolutionMap(final SetMultimap<QualifiedName, VersionResolution> map) {
090        final ImmutableSetMultimap.Builder<QualifiedName, VersionResolutionCollection> builder = ImmutableSetMultimap.builder();
091        builder.orderKeysBy(QualifiedName::compareTo).orderValuesBy(VersionResolutionCollection::compareTo);
092
093        Maps.transformValues(map.asMap(), v -> {
094            // fold into Map from version -> set of all dependencies that want this version.
095            final ImmutableSetMultimap<ComparableVersion, VersionResolutionElement> versionMap = v.stream()
096                    .collect(toImmutableSetMultimap(VersionResolution::getExpectedVersion, VersionResolution::getRequestingDependency));
097            // collect result into a set of VersionResolutionCollection objects.
098            return versionMap.asMap().entrySet().stream()
099                    .map(VersionResolutionCollection::new)
100                    .collect(toImmutableSet());
101            // stream the entries of QualifiedName, Set of version resolution collections
102            // collect them into a Multimap from QualifiedName to version resolution collections.
103        }).forEach(builder::putAll);
104
105        return builder.build();
106    }
107
108    @Override
109    public String toString() {
110        return MoreObjects.toStringHelper(this)
111                .add("requestingDependencies", requestingDependencies)
112                .add("expectedVersion", expectedVersion)
113                .toString();
114    }
115
116    @Override
117    public boolean equals(final Object o) {
118        if (this == o) {
119            return true;
120        }
121        if (o == null || getClass() != o.getClass()) {
122            return false;
123        }
124        final VersionResolutionCollection that = (VersionResolutionCollection) o;
125        return requestingDependencies.equals(that.requestingDependencies)
126                && expectedVersion.equals(that.expectedVersion);
127    }
128
129    @Override
130    public int hashCode() {
131        return Objects.hash(requestingDependencies, expectedVersion);
132    }
133}