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
068    public void set(Dependency dependency, VersionRangeResult result) {
069        this.dependency = dependency;
070        this.result = result;
071        this.versions = new ArrayList<>(result.getVersions());
072    }
073
074    /**
075     * Creates an initialized, new context instance out of this context session and provided dependency and result.
076     * The newly created context is still not thread safe, but allows to have multiple contexts with different
077     * dependencies and results in the same time (processed in multiple threads in parallel like BF collector does).
078     */
079    public DefaultVersionFilterContext initialize(Dependency dependency, VersionRangeResult result) {
080        return new DefaultVersionFilterContext(this.session, dependency, result);
081    }
082
083    public List<Version> get() {
084        return new ArrayList<>(versions);
085    }
086
087    @Override
088    public RepositorySystemSession getSession() {
089        return session;
090    }
091
092    @Override
093    public Dependency getDependency() {
094        return dependency;
095    }
096
097    @Override
098    public VersionConstraint getVersionConstraint() {
099        return result.getVersionConstraint();
100    }
101
102    @Override
103    public int getCount() {
104        return versions.size();
105    }
106
107    @Override
108    public ArtifactRepository getRepository(Version version) {
109        return result.getRepository(version);
110    }
111
112    @Override
113    public List<RemoteRepository> getRepositories() {
114        return Collections.unmodifiableList(result.getRequest().getRepositories());
115    }
116
117    @Override
118    public Iterator<Version> iterator() {
119        return versions.iterator();
120    }
121
122    @Override
123    public String toString() {
124        return dependency + " " + result.getVersions();
125    }
126}