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