1 package org.eclipse.aether.repository;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.File;
23
24 /**
25 * A repository on the local file system used to cache contents of remote repositories and to store locally installed
26 * artifacts. Note that this class merely describes such a repository, actual access to the contained artifacts is
27 * handled by a {@link LocalRepositoryManager} which is usually determined from the {@link #getContentType() type} of
28 * the repository.
29 */
30 public final class LocalRepository
31 implements ArtifactRepository
32 {
33
34 private final File basedir;
35
36 private final String type;
37
38 /**
39 * Creates a new local repository with the specified base directory and unknown type.
40 *
41 * @param basedir The base directory of the repository, may be {@code null}.
42 */
43 public LocalRepository( String basedir )
44 {
45 this( ( basedir != null ) ? new File( basedir ) : null, "" );
46 }
47
48 /**
49 * Creates a new local repository with the specified base directory and unknown type.
50 *
51 * @param basedir The base directory of the repository, may be {@code null}.
52 */
53 public LocalRepository( File basedir )
54 {
55 this( basedir, "" );
56 }
57
58 /**
59 * Creates a new local repository with the specified properties.
60 *
61 * @param basedir The base directory of the repository, may be {@code null}.
62 * @param type The type of the repository, may be {@code null}.
63 */
64 public LocalRepository( File basedir, String type )
65 {
66 this.basedir = basedir;
67 this.type = ( type != null ) ? type : "";
68 }
69
70 public String getContentType()
71 {
72 return type;
73 }
74
75 public String getId()
76 {
77 return "local";
78 }
79
80 /**
81 * Gets the base directory of the repository.
82 *
83 * @return The base directory or {@code null} if none.
84 */
85 public File getBasedir()
86 {
87 return basedir;
88 }
89
90 @Override
91 public String toString()
92 {
93 return getBasedir() + " (" + getContentType() + ")";
94 }
95
96 @Override
97 public boolean equals( Object obj )
98 {
99 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 }