001package org.eclipse.aether.repository; 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 java.io.File; 023import static java.util.Objects.requireNonNull; 024 025/** 026 * A result from the local repository about the existence of an artifact. 027 * 028 * @see LocalRepositoryManager#find(org.eclipse.aether.RepositorySystemSession, LocalArtifactRequest) 029 */ 030public final class LocalArtifactResult 031{ 032 033 private final LocalArtifactRequest request; 034 035 private File file; 036 037 private boolean available; 038 039 private RemoteRepository repository; 040 041 /** 042 * Creates a new result for the specified request. 043 * 044 * @param request The local artifact request, must not be {@code null}. 045 */ 046 public LocalArtifactResult( LocalArtifactRequest request ) 047 { 048 this.request = requireNonNull( request, "local artifact request cannot be null" ); 049 } 050 051 /** 052 * Gets the request corresponding to this result. 053 * 054 * @return The corresponding request, never {@code null}. 055 */ 056 public LocalArtifactRequest getRequest() 057 { 058 return request; 059 } 060 061 /** 062 * Gets the file to the requested artifact. Note that this file must not be used unless {@link #isAvailable()} 063 * returns {@code true}. An artifact file can be found but considered unavailable if the artifact was cached from a 064 * remote repository that is not part of the list of remote repositories used for the query. 065 * 066 * @return The file to the requested artifact or {@code null} if the artifact does not exist locally. 067 */ 068 public File getFile() 069 { 070 return file; 071 } 072 073 /** 074 * Sets the file to requested artifact. 075 * 076 * @param file The artifact file, may be {@code null}. 077 * @return This result for chaining, never {@code null}. 078 */ 079 public LocalArtifactResult setFile( File file ) 080 { 081 this.file = file; 082 return this; 083 } 084 085 /** 086 * Indicates whether the requested artifact is available for use. As a minimum, the file needs to be physically 087 * existent in the local repository to be available. Additionally, a local repository manager can consider the list 088 * of supplied remote repositories to determine whether the artifact is logically available and mark an artifact 089 * unavailable (despite its physical existence) if it is not known to be hosted by any of the provided repositories. 090 * 091 * @return {@code true} if the artifact is available, {@code false} otherwise. 092 * @see LocalArtifactRequest#getRepositories() 093 */ 094 public boolean isAvailable() 095 { 096 return available; 097 } 098 099 /** 100 * Sets whether the artifact is available. 101 * 102 * @param available {@code true} if the artifact is available, {@code false} otherwise. 103 * @return This result for chaining, never {@code null}. 104 */ 105 public LocalArtifactResult setAvailable( boolean available ) 106 { 107 this.available = available; 108 return this; 109 } 110 111 /** 112 * Gets the (first) remote repository from which the artifact was cached (if any). 113 * 114 * @return The remote repository from which the artifact was originally retrieved or {@code null} if unknown or if 115 * the artifact has been locally installed. 116 * @see LocalArtifactRequest#getRepositories() 117 */ 118 public RemoteRepository getRepository() 119 { 120 return repository; 121 } 122 123 /** 124 * Sets the (first) remote repository from which the artifact was cached. 125 * 126 * @param repository The remote repository from which the artifact was originally retrieved, may be {@code null} if 127 * unknown or if the artifact has been locally installed. 128 * @return This result for chaining, never {@code null}. 129 */ 130 public LocalArtifactResult setRepository( RemoteRepository repository ) 131 { 132 this.repository = repository; 133 return this; 134 } 135 136 @Override 137 public String toString() 138 { 139 return getFile() + " (" + ( isAvailable() ? "available" : "unavailable" ) + ")"; 140 } 141 142}