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;
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
031    implements ArtifactRepository
032{
033
034    private final File basedir;
035
036    private final String type;
037
038    /**
039     * Creates a new local repository with the specified base directory and unknown type.
040     * 
041     * @param basedir The base directory of the repository, may be {@code null}.
042     */
043    public LocalRepository( String basedir )
044    {
045        this( ( basedir != null ) ? new File( basedir ) : null, "" );
046    }
047
048    /**
049     * Creates a new local repository with the specified base directory and unknown type.
050     * 
051     * @param basedir The base directory of the repository, may be {@code null}.
052     */
053    public LocalRepository( File basedir )
054    {
055        this( basedir, "" );
056    }
057
058    /**
059     * Creates a new local repository with the specified properties.
060     * 
061     * @param basedir The base directory of the repository, may be {@code null}.
062     * @param type The type of the repository, may be {@code null}.
063     */
064    public LocalRepository( File basedir, String type )
065    {
066        this.basedir = basedir;
067        this.type = ( type != null ) ? type : "";
068    }
069
070    public String getContentType()
071    {
072        return type;
073    }
074
075    public String getId()
076    {
077        return "local";
078    }
079
080    /**
081     * Gets the base directory of the repository.
082     * 
083     * @return The base directory or {@code null} if none.
084     */
085    public File getBasedir()
086    {
087        return basedir;
088    }
089
090    @Override
091    public String toString()
092    {
093        return getBasedir() + " (" + getContentType() + ")";
094    }
095
096    @Override
097    public boolean equals( Object obj )
098    {
099        if ( this == obj )
100        {
101            return true;
102        }
103        if ( obj == null || !getClass().equals( obj.getClass() ) )
104        {
105            return false;
106        }
107
108        LocalRepository that = (LocalRepository) obj;
109
110        return eq( basedir, that.basedir ) && eq( type, that.type );
111    }
112
113    private static <T> boolean eq( T s1, T s2 )
114    {
115        return s1 != null ? s1.equals( s2 ) : s2 == null;
116    }
117
118    @Override
119    public int hashCode()
120    {
121        int hash = 17;
122        hash = hash * 31 + hash( basedir );
123        hash = hash * 31 + hash( type );
124        return hash;
125    }
126
127    private static int hash( Object obj )
128    {
129        return obj != null ? obj.hashCode() : 0;
130    }
131
132}