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