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.util.Objects;
023
024/**
025 * A repository on the local file system used to cache contents of remote repositories and to store locally installed
026 * artifacts. Note that this class merely describes such a repository, actual access to the contained artifacts is
027 * handled by a {@link LocalRepositoryManager} which is usually determined from the {@link #getContentType() type} of
028 * the repository.
029 */
030public final class LocalRepository implements ArtifactRepository {
031
032    private final File basedir;
033
034    private final String type;
035
036    /**
037     * Creates a new local repository with the specified base directory and unknown type.
038     *
039     * @param basedir The base directory of the repository, may be {@code null}.
040     */
041    public LocalRepository(String basedir) {
042        this((basedir != null) ? new File(basedir) : null, "");
043    }
044
045    /**
046     * Creates a new local repository with the specified base directory and unknown type.
047     *
048     * @param basedir The base directory of the repository, may be {@code null}.
049     */
050    public LocalRepository(File basedir) {
051        this(basedir, "");
052    }
053
054    /**
055     * Creates a new local repository with the specified properties.
056     *
057     * @param basedir The base directory of the repository, may be {@code null}.
058     * @param type The type of the repository, may be {@code null}.
059     */
060    public LocalRepository(File basedir, String type) {
061        this.basedir = basedir;
062        this.type = (type != null) ? type : "";
063    }
064
065    public String getContentType() {
066        return type;
067    }
068
069    public String getId() {
070        return "local";
071    }
072
073    /**
074     * Gets the base directory of the repository.
075     *
076     * @return The base directory or {@code null} if none.
077     */
078    public File getBasedir() {
079        return basedir;
080    }
081
082    @Override
083    public String toString() {
084        return getBasedir() + " (" + getContentType() + ")";
085    }
086
087    @Override
088    public boolean equals(Object obj) {
089        if (this == obj) {
090            return true;
091        }
092        if (obj == null || !getClass().equals(obj.getClass())) {
093            return false;
094        }
095
096        LocalRepository that = (LocalRepository) obj;
097
098        return Objects.equals(basedir, that.basedir) && Objects.equals(type, that.type);
099    }
100
101    @Override
102    public int hashCode() {
103        int hash = 17;
104        hash = hash * 31 + hash(basedir);
105        hash = hash * 31 + hash(type);
106        return hash;
107    }
108
109    private static int hash(Object obj) {
110        return obj != null ? obj.hashCode() : 0;
111    }
112}