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.repository;
020
021import java.io.File;
022import java.nio.file.Path;
023
024import static java.util.Objects.requireNonNull;
025
026/**
027 * A result from the local repository about the existence of metadata.
028 *
029 * @see LocalRepositoryManager#find(org.eclipse.aether.RepositorySystemSession, LocalMetadataRequest)
030 */
031public final class LocalMetadataResult {
032
033    private final LocalMetadataRequest request;
034
035    private Path path;
036
037    private boolean stale;
038
039    /**
040     * Creates a new result for the specified request.
041     *
042     * @param request The local metadata request, must not be {@code null}.
043     */
044    public LocalMetadataResult(LocalMetadataRequest request) {
045        this.request = requireNonNull(request, "local metadata request cannot be null");
046    }
047
048    /**
049     * Gets the request corresponding to this result.
050     *
051     * @return The corresponding request, never {@code null}.
052     */
053    public LocalMetadataRequest getRequest() {
054        return request;
055    }
056
057    /**
058     * Gets the file to the requested metadata if the metadata is available in the local repository.
059     *
060     * @return The file to the requested metadata or {@code null}.
061     * @deprecated Use {@link #getPath()} instead.
062     */
063    @Deprecated
064    public File getFile() {
065        return path != null ? path.toFile() : null;
066    }
067
068    /**
069     * Gets the file to the requested metadata if the metadata is available in the local repository.
070     *
071     * @return The file to the requested metadata or {@code null}.
072     * @since 2.0.0
073     */
074    public Path getPath() {
075        return path;
076    }
077
078    /**
079     * Sets the file to requested metadata.
080     *
081     * @param file The metadata file, may be {@code null}.
082     * @return This result for chaining, never {@code null}.
083     * @deprecated Use {@link #setPath(Path)} instead.
084     */
085    @Deprecated
086    public LocalMetadataResult setFile(File file) {
087        return setPath(file != null ? file.toPath() : null);
088    }
089
090    /**
091     * Sets the file to requested metadata.
092     *
093     * @param path The metadata file, may be {@code null}.
094     * @return This result for chaining, never {@code null}.
095     * @since 2.0.0
096     */
097    public LocalMetadataResult setPath(Path path) {
098        this.path = path;
099        return this;
100    }
101
102    /**
103     * This value indicates whether the metadata is stale and should be updated.
104     *
105     * @return {@code true} if the metadata is stale and should be updated, {@code false} otherwise.
106     */
107    public boolean isStale() {
108        return stale;
109    }
110
111    /**
112     * Sets whether the metadata is stale.
113     *
114     * @param stale {@code true} if the metadata is stale and should be updated, {@code false} otherwise.
115     * @return This result for chaining, never {@code null}.
116     */
117    public LocalMetadataResult setStale(boolean stale) {
118        this.stale = stale;
119        return this;
120    }
121
122    @Override
123    public String toString() {
124        return request.toString() + "(" + getPath() + ")";
125    }
126}