View Javadoc

1   package org.sonatype.aether.repository;
2   
3   /*******************************************************************************
4    * Copyright (c) 2010-2011 Sonatype, Inc.
5    * All rights reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    *   http://www.eclipse.org/legal/epl-v10.html
9    *******************************************************************************/
10  
11  import java.io.File;
12  
13  /**
14   * A repository on the local file system used to cache contents of remote repositories and to store locally installed
15   * artifacts.
16   * 
17   * @author Benjamin Bentmann
18   */
19  public final class LocalRepository
20      implements ArtifactRepository
21  {
22  
23      private final File basedir;
24  
25      private final String type;
26  
27      /**
28       * Creates a new local repository with the specified base directory and unknown type.
29       * 
30       * @param basedir The base directory of the repository, may be {@code null}.
31       */
32      public LocalRepository( String basedir )
33      {
34          this( ( basedir != null ) ? new File( basedir ) : null, "" );
35      }
36  
37      /**
38       * Creates a new local repository with the specified base directory and unknown type.
39       * 
40       * @param basedir The base directory of the repository, may be {@code null}.
41       */
42      public LocalRepository( File basedir )
43      {
44          this( basedir, "" );
45      }
46  
47      /**
48       * Creates a new local repository with the specified properties.
49       * 
50       * @param basedir The base directory of the repository, may be {@code null}.
51       * @param type The type of the repository, may be {@code null}.
52       */
53      public LocalRepository( File basedir, String type )
54      {
55          this.basedir = basedir;
56          this.type = ( type != null ) ? type : "";
57      }
58  
59      public String getContentType()
60      {
61          return type;
62      }
63  
64      public String getId()
65      {
66          return "local";
67      }
68  
69      /**
70       * Gets the base directory of the repository.
71       * 
72       * @return The base directory or {@code null} if none.
73       */
74      public File getBasedir()
75      {
76          return basedir;
77      }
78  
79      @Override
80      public String toString()
81      {
82          return getBasedir() + " (" + getContentType() + ")";
83      }
84  
85      @Override
86      public boolean equals( Object obj )
87      {
88          if ( this == obj )
89          {
90              return true;
91          }
92          if ( obj == null || !getClass().equals( obj.getClass() ) )
93          {
94              return false;
95          }
96  
97          LocalRepository that = (LocalRepository) obj;
98  
99          return eq( basedir, that.basedir ) && eq( type, that.type );
100     }
101 
102     private static <T> boolean eq( T s1, T s2 )
103     {
104         return s1 != null ? s1.equals( s2 ) : s2 == null;
105     }
106 
107     @Override
108     public int hashCode()
109     {
110         int hash = 17;
111         hash = hash * 31 + hash( basedir );
112         hash = hash * 31 + hash( type );
113         return hash;
114     }
115 
116     private static int hash( Object obj )
117     {
118         return obj != null ? obj.hashCode() : 0;
119     }
120 
121 }