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.internal.impl.collect;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.Iterator;
024import java.util.List;
025
026import org.eclipse.aether.RepositorySystemSession;
027import org.eclipse.aether.collection.VersionFilter;
028import org.eclipse.aether.graph.Dependency;
029import org.eclipse.aether.repository.ArtifactRepository;
030import org.eclipse.aether.repository.RemoteRepository;
031import org.eclipse.aether.resolution.VersionRangeResult;
032import org.eclipse.aether.version.Version;
033import org.eclipse.aether.version.VersionConstraint;
034
035/**
036 * Default implementation of {@link VersionFilter.VersionFilterContext}.
037 * Internal helper class for collector implementations.
038 * This instance is not thread safe, and same context instance must not be shared across threads.
039 */
040public final class DefaultVersionFilterContext implements VersionFilter.VersionFilterContext {
041    private final RepositorySystemSession session;
042
043    private Dependency dependency;
044
045    private VersionRangeResult result;
046
047    private ArrayList<Version> versions;
048
049    public DefaultVersionFilterContext(RepositorySystemSession session) {
050        this.session = session;
051        this.dependency = null;
052        this.result = null;
053        this.versions = null;
054    }
055
056    private DefaultVersionFilterContext(
057            RepositorySystemSession session, Dependency dependency, VersionRangeResult result) {
058        this.session = session;
059        this.dependency = dependency;
060        this.result = result;
061        this.versions = new ArrayList<>(result.getVersions());
062    }
063
064    /**
065     * The use of this method allows strictly single-threaded use of context. Is unused in production, only in tests.
066     *
067     * @deprecated Use {@link #initialize(Dependency, VersionRangeResult)} instead, which creates a new context and
068     * is safe to use from several threads.
069     */
070    @Deprecated
071    public void set(Dependency dependency, VersionRangeResult result) {
072        this.dependency = dependency;
073        this.result = result;
074        this.versions = new ArrayList<>(result.getVersions());
075    }
076
077    /**
078     * Creates an initialized, new context instance out of this context session and provided dependency and result.
079     * The newly created context is still not thread safe, but allows to have multiple contexts with different
080     * dependencies and results in the same time (processed in multiple threads in parallel like BF collector does).
081     */
082    public DefaultVersionFilterContext initialize(Dependency dependency, VersionRangeResult result) {
083        return new DefaultVersionFilterContext(this.session, dependency, result);
084    }
085
086    public List<Version> get() {
087        return new ArrayList<>(versions);
088    }
089
090    @Override
091    public RepositorySystemSession getSession() {
092        return session;
093    }
094
095    @Override
096    public Dependency getDependency() {
097        return dependency;
098    }
099
100    @Override
101    public VersionConstraint getVersionConstraint() {
102        return result.getVersionConstraint();
103    }
104
105    @Override
106    public int getCount() {
107        return versions.size();
108    }
109
110    @Override
111    public ArtifactRepository getRepository(Version version) {
112        return result.getRepository(version);
113    }
114
115    @Override
116    public List<RemoteRepository> getRepositories() {
117        return Collections.unmodifiableList(result.getRequest().getRepositories());
118    }
119
120    @Override
121    public Iterator<Version> iterator() {
122        return versions.iterator();
123    }
124
125    @Override
126    public String toString() {
127        return dependency + " " + result.getVersions();
128    }
129}