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.net.URI;
023import java.nio.file.Path;
024import java.nio.file.Paths;
025import java.util.Objects;
026
027/**
028 * A repository on the local file system used to cache contents of remote repositories and to store locally installed
029 * artifacts. Note that this class merely describes such a repository, actual access to the contained artifacts is
030 * handled by a {@link LocalRepositoryManager} which is usually determined from the {@link #getContentType() type} of
031 * the repository.
032 */
033public final class LocalRepository implements ArtifactRepository {
034    public static final String ID = "local";
035
036    private final Path basePath;
037
038    private final String type;
039
040    private final int hashCode;
041
042    /**
043     * Creates a new local repository with the specified base directory and unknown type.
044     *
045     * @param basedir The base directory of the repository, may be {@code null}.
046     * @deprecated Use {@link LocalRepository(Path)} instead.
047     */
048    @Deprecated
049    public LocalRepository(String basedir) {
050        this.basePath = Paths.get(RepositoryUriUtils.toUri(basedir)).toAbsolutePath();
051        this.type = "";
052        this.hashCode = Objects.hash(this.basePath, this.type);
053    }
054
055    /**
056     * Creates a new local repository with the specified base directory and unknown type.
057     *
058     * @param basedir The base directory of the repository, may be {@code null}.
059     * @since 2.0.0
060     */
061    public LocalRepository(URI basedir) {
062        this((basedir != null) ? Paths.get(basedir) : null, "");
063    }
064
065    /**
066     * Creates a new local repository with the specified base directory and unknown type.
067     *
068     * @param basedir The base directory of the repository, may be {@code null}.
069     * @deprecated Use {@link #LocalRepository(Path)} instead.
070     */
071    @Deprecated
072    public LocalRepository(File basedir) {
073        this(basedir, "");
074    }
075
076    /**
077     * Creates a new local repository with the specified base directory and unknown type.
078     *
079     * @param basePath The base directory of the repository, may be {@code null}.
080     * @since 2.0.0
081     */
082    public LocalRepository(Path basePath) {
083        this(basePath, "");
084    }
085
086    /**
087     * Creates a new local repository with the specified properties.
088     *
089     * @param basedir The base directory of the repository, may be {@code null}.
090     * @param type The type of the repository, may be {@code null}.
091     * @deprecated Use {@link #LocalRepository(Path, String)} instead.
092     */
093    @Deprecated
094    public LocalRepository(File basedir, String type) {
095        this(basedir != null ? basedir.toPath() : null, type);
096    }
097
098    /**
099     * Creates a new local repository with the specified properties.
100     *
101     * @param basePath The base directory of the repository, may be {@code null}.
102     * @param type The type of the repository, may be {@code null}.
103     * @since 2.0.0
104     */
105    public LocalRepository(Path basePath, String type) {
106        this.basePath = basePath;
107        this.type = (type != null) ? type : "";
108        this.hashCode = Objects.hash(this.basePath, this.type);
109    }
110
111    @Override
112    public String getContentType() {
113        return type;
114    }
115
116    @Override
117    public String getId() {
118        return ID;
119    }
120
121    /**
122     * Gets the base directory of the repository.
123     *
124     * @return The base directory or {@code null} if none.
125     * @deprecated Use {@link #getBasePath()} instead.
126     */
127    @Deprecated
128    public File getBasedir() {
129        return basePath != null ? basePath.toFile() : null;
130    }
131
132    /**
133     * Gets the base directory of the repository.
134     *
135     * @return The base directory or {@code null} if none.
136     * @since 2.0.0
137     */
138    public Path getBasePath() {
139        return basePath;
140    }
141
142    @Override
143    public String toString() {
144        return getBasePath() + " (" + getContentType() + ")";
145    }
146
147    @Override
148    public boolean equals(Object obj) {
149        if (this == obj) {
150            return true;
151        }
152        if (obj == null || !getClass().equals(obj.getClass())) {
153            return false;
154        }
155
156        LocalRepository that = (LocalRepository) obj;
157
158        return Objects.equals(basePath, that.basePath) && Objects.equals(type, that.type);
159    }
160
161    @Override
162    public int hashCode() {
163        return hashCode;
164    }
165}