001package org.eclipse.aether.resolution;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static java.util.Objects.requireNonNull;
023
024import org.eclipse.aether.RepositorySystem;
025import org.eclipse.aether.metadata.Metadata;
026import org.eclipse.aether.transfer.MetadataNotFoundException;
027
028/**
029 * The result of a metadata resolution request.
030 * 
031 * @see RepositorySystem#resolveMetadata(org.eclipse.aether.RepositorySystemSession, java.util.Collection)
032 */
033public final class MetadataResult
034{
035
036    private final MetadataRequest request;
037
038    private Exception exception;
039
040    private boolean updated;
041
042    private Metadata metadata;
043
044    /**
045     * Creates a new result for the specified request.
046     *
047     * @param request The resolution request, must not be {@code null}.
048     */
049    public MetadataResult( MetadataRequest request )
050    {
051        this.request = requireNonNull( request, "metadata request cannot be null" );
052    }
053
054    /**
055     * Gets the resolution request that was made.
056     *
057     * @return The resolution request, never {@code null}.
058     */
059    public MetadataRequest getRequest()
060    {
061        return request;
062    }
063
064    /**
065     * Gets the resolved metadata (if any).
066     * 
067     * @return The resolved metadata or {@code null} if the resolution failed.
068     */
069    public Metadata getMetadata()
070    {
071        return metadata;
072    }
073
074    /**
075     * Sets the resolved metadata.
076     * 
077     * @param metadata The resolved metadata, may be {@code null} if the resolution failed.
078     * @return This result for chaining, never {@code null}.
079     */
080    public MetadataResult setMetadata( Metadata metadata )
081    {
082        this.metadata = metadata;
083        return this;
084    }
085
086    /**
087     * Records the specified exception while resolving the metadata.
088     * 
089     * @param exception The exception to record, may be {@code null}.
090     * @return This result for chaining, never {@code null}.
091     */
092    public MetadataResult setException( Exception exception )
093    {
094        this.exception = exception;
095        return this;
096    }
097
098    /**
099     * Gets the exception that occurred while resolving the metadata.
100     * 
101     * @return The exception that occurred or {@code null} if none.
102     */
103    public Exception getException()
104    {
105        return exception;
106    }
107
108    /**
109     * Sets the updated flag for the metadata.
110     * 
111     * @param updated {@code true} if the metadata was actually fetched from the remote repository during the
112     *            resolution, {@code false} if the metadata was resolved from a locally cached copy.
113     * @return This result for chaining, never {@code null}.
114     */
115    public MetadataResult setUpdated( boolean updated )
116    {
117        this.updated = updated;
118        return this;
119    }
120
121    /**
122     * Indicates whether the metadata was actually fetched from the remote repository or resolved from the local cache.
123     * If metadata has been locally cached during a previous resolution request and this local copy is still up-to-date
124     * according to the remote repository's update policy, no remote access is made.
125     * 
126     * @return {@code true} if the metadata was actually fetched from the remote repository during the resolution,
127     *         {@code false} if the metadata was resolved from a locally cached copy.
128     */
129    public boolean isUpdated()
130    {
131        return updated;
132    }
133
134    /**
135     * Indicates whether the requested metadata was resolved. Note that the metadata might have been successfully
136     * resolved (from the local cache) despite {@link #getException()} indicating a transfer error while trying to
137     * refetch the metadata from the remote repository.
138     * 
139     * @return {@code true} if the metadata was resolved, {@code false} otherwise.
140     * @see Metadata#getFile()
141     */
142    public boolean isResolved()
143    {
144        return getMetadata() != null && getMetadata().getFile() != null;
145    }
146
147    /**
148     * Indicates whether the requested metadata is not present in the remote repository.
149     * 
150     * @return {@code true} if the metadata is not present in the remote repository, {@code false} otherwise.
151     */
152    public boolean isMissing()
153    {
154        return getException() instanceof MetadataNotFoundException;
155    }
156
157    @Override
158    public String toString()
159    {
160        return getMetadata() + ( isUpdated() ? " (updated)" : " (cached)" );
161    }
162
163}