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