001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.collection;
020
021import java.util.Iterator;
022import java.util.List;
023
024import org.eclipse.aether.RepositoryException;
025import org.eclipse.aether.RepositorySystemSession;
026import org.eclipse.aether.graph.Dependency;
027import org.eclipse.aether.repository.ArtifactRepository;
028import org.eclipse.aether.repository.RemoteRepository;
029import org.eclipse.aether.version.Version;
030import org.eclipse.aether.version.VersionConstraint;
031
032/**
033 * Decides which versions matching a version range should actually be considered for the dependency graph. The version
034 * filter is not invoked for dependencies that do not declare a version range but a single version.
035 * <p>
036 * <strong>Note:</strong> Implementations must be stateless.
037 * <p>
038 * <em>Warning:</em> This hook is called from a hot spot and therefore implementations should pay attention to
039 * performance. Among others, implementations should provide a semantic {@link Object#equals(Object) equals()} method.
040 *
041 * @see org.eclipse.aether.RepositorySystemSession#getVersionFilter()
042 * @see org.eclipse.aether.RepositorySystem#collectDependencies(org.eclipse.aether.RepositorySystemSession,
043 *      CollectRequest)
044 */
045public interface VersionFilter {
046
047    /**
048     * A context used during version filtering to hold relevant data.
049     *
050     * @noimplement This interface is not intended to be implemented by clients.
051     * @noextend This interface is not intended to be extended by clients.
052     */
053    interface VersionFilterContext extends Iterable<Version> {
054
055        /**
056         * Gets the repository system session during which the version filtering happens.
057         *
058         * @return The repository system session, never {@code null}.
059         */
060        RepositorySystemSession getSession();
061
062        /**
063         * Gets the dependency whose version range is being filtered.
064         *
065         * @return The dependency, never {@code null}.
066         */
067        Dependency getDependency();
068
069        /**
070         * Gets the total number of available versions. This count reflects any removals made during version filtering.
071         *
072         * @return The total number of available versions.
073         */
074        int getCount();
075
076        /**
077         * Gets an iterator over the available versions of the dependency. The iterator returns versions in ascending
078         * order. Use {@link Iterator#remove()} to exclude a version from further consideration in the dependency graph.
079         *
080         * @return The iterator of available versions, never {@code null}.
081         */
082        Iterator<Version> iterator();
083
084        /**
085         * Gets the version constraint that was parsed from the dependency's version string.
086         *
087         * @return The parsed version constraint, never {@code null}.
088         */
089        VersionConstraint getVersionConstraint();
090
091        /**
092         * Gets the repository from which the specified version was resolved.
093         *
094         * @param version The version whose source repository should be retrieved, must not be {@code null}.
095         * @return The repository from which the version was resolved or {@code null} if unknown.
096         */
097        ArtifactRepository getRepository(Version version);
098
099        /**
100         * Gets the remote repositories from which the versions were resolved.
101         *
102         * @return The (read-only) list of repositories, never {@code null}.
103         */
104        List<RemoteRepository> getRepositories();
105    }
106
107    /**
108     * Filters the available versions for a given dependency. Implementations will usually call
109     * {@link VersionFilterContext#iterator() context.iterator()} to inspect the available versions and use
110     * {@link java.util.Iterator#remove()} to delete unacceptable versions. If no versions remain after all filtering
111     * has been performed, the dependency collection process will automatically fail, i.e. implementations need not
112     * handle this situation on their own.
113     *
114     * @param context The version filter context, must not be {@code null}.
115     * @throws RepositoryException If the filtering could not be performed.
116     */
117    void filterVersions(VersionFilterContext context) throws RepositoryException;
118
119    /**
120     * Derives a version filter for the specified collection context. The derived filter will be used to handle version
121     * ranges encountered in child dependencies of the current node. When calculating the child filter, implementors are
122     * strongly advised to simply return the current instance if nothing changed to help save memory.
123     *
124     * @param context The dependency collection context, must not be {@code null}.
125     * @return The version filter for the target node or {@code null} if versions should not be filtered any more.
126     */
127    VersionFilter deriveChildFilter(DependencyCollectionContext context);
128}