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.resolution;
020
021import org.eclipse.aether.RepositorySystem;
022import org.eclipse.aether.metadata.Metadata;
023import org.eclipse.aether.transfer.MetadataNotFoundException;
024
025import static java.util.Objects.requireNonNull;
026
027/**
028 * The result of a metadata resolution request.
029 *
030 * @see RepositorySystem#resolveMetadata(org.eclipse.aether.RepositorySystemSession, java.util.Collection)
031 */
032public final class MetadataResult {
033
034    private final MetadataRequest request;
035
036    private Exception exception;
037
038    private boolean updated;
039
040    private Metadata metadata;
041
042    /**
043     * Creates a new result for the specified request.
044     *
045     * @param request The resolution request, must not be {@code null}.
046     */
047    public MetadataResult(MetadataRequest request) {
048        this.request = requireNonNull(request, "metadata request cannot be null");
049    }
050
051    /**
052     * Gets the resolution request that was made.
053     *
054     * @return The resolution request, never {@code null}.
055     */
056    public MetadataRequest getRequest() {
057        return request;
058    }
059
060    /**
061     * Gets the resolved metadata (if any).
062     *
063     * @return The resolved metadata or {@code null} if the resolution failed.
064     */
065    public Metadata getMetadata() {
066        return metadata;
067    }
068
069    /**
070     * Sets the resolved metadata.
071     *
072     * @param metadata The resolved metadata, may be {@code null} if the resolution failed.
073     * @return This result for chaining, never {@code null}.
074     */
075    public MetadataResult setMetadata(Metadata metadata) {
076        this.metadata = metadata;
077        return this;
078    }
079
080    /**
081     * Records the specified exception while resolving the metadata.
082     *
083     * @param exception The exception to record, may be {@code null}.
084     * @return This result for chaining, never {@code null}.
085     */
086    public MetadataResult setException(Exception exception) {
087        this.exception = exception;
088        return this;
089    }
090
091    /**
092     * Gets the exception that occurred while resolving the metadata.
093     *
094     * @return The exception that occurred or {@code null} if none.
095     */
096    public Exception getException() {
097        return exception;
098    }
099
100    /**
101     * Sets the updated flag for the metadata.
102     *
103     * @param updated {@code true} if the metadata was actually fetched from the remote repository during the
104     *            resolution, {@code false} if the metadata was resolved from a locally cached copy.
105     * @return This result for chaining, never {@code null}.
106     */
107    public MetadataResult setUpdated(boolean updated) {
108        this.updated = updated;
109        return this;
110    }
111
112    /**
113     * Indicates whether the metadata was actually fetched from the remote repository or resolved from the local cache.
114     * If metadata has been locally cached during a previous resolution request and this local copy is still up-to-date
115     * according to the remote repository's update policy, no remote access is made.
116     *
117     * @return {@code true} if the metadata was actually fetched from the remote repository during the resolution,
118     *         {@code false} if the metadata was resolved from a locally cached copy.
119     */
120    public boolean isUpdated() {
121        return updated;
122    }
123
124    /**
125     * Indicates whether the requested metadata was resolved. Note that the metadata might have been successfully
126     * resolved (from the local cache) despite {@link #getException()} indicating a transfer error while trying to
127     * refetch the metadata from the remote repository.
128     *
129     * @return {@code true} if the metadata was resolved, {@code false} otherwise.
130     * @see Metadata#getPath()
131     */
132    public boolean isResolved() {
133        return getMetadata() != null && getMetadata().getPath() != null;
134    }
135
136    /**
137     * Indicates whether the requested metadata is not present in the remote repository.
138     *
139     * @return {@code true} if the metadata is not present in the remote repository, {@code false} otherwise.
140     */
141    public boolean isMissing() {
142        return getException() instanceof MetadataNotFoundException;
143    }
144
145    @Override
146    public String toString() {
147        return getMetadata() + (isUpdated() ? " (updated)" : " (cached)");
148    }
149}