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.util.UUID;
22  
23  /**
24   * A repository backed by an IDE workspace, the output of a build session or similar ad-hoc collection of artifacts. As
25   * far as the repository system is concerned, a workspace repository is read-only, i.e. can only be used for artifact
26   * resolution but not installation/deployment. Note that this class merely describes such a repository, actual access to
27   * the contained artifacts is handled by a {@link WorkspaceReader}.
28   */
29  public final class WorkspaceRepository implements ArtifactRepository {
30  
31      private final String type;
32  
33      private final Object key;
34  
35      /**
36       * Creates a new workspace repository of type {@code "workspace"} and a random key.
37       */
38      public WorkspaceRepository() {
39          this("workspace");
40      }
41  
42      /**
43       * Creates a new workspace repository with the specified type and a random key.
44       *
45       * @param type The type of the repository, may be {@code null}.
46       */
47      public WorkspaceRepository(String type) {
48          this(type, null);
49      }
50  
51      /**
52       * Creates a new workspace repository with the specified type and key. The key is used to distinguish one workspace
53       * from another and should be sensitive to the artifacts that are (potentially) available in the workspace.
54       *
55       * @param type The type of the repository, may be {@code null}.
56       * @param key The (comparison) key for the repository, may be {@code null} to generate a unique random key.
57       */
58      public WorkspaceRepository(String type, Object key) {
59          this.type = (type != null) ? type : "";
60          this.key = (key != null) ? key : UUID.randomUUID().toString().replace("-", "");
61      }
62  
63      public String getContentType() {
64          return type;
65      }
66  
67      public String getId() {
68          return "workspace";
69      }
70  
71      /**
72       * Gets the key of this workspace repository. The key is used to distinguish one workspace from another and should
73       * be sensitive to the artifacts that are (potentially) available in the workspace.
74       *
75       * @return The (comparison) key for this workspace repository, never {@code null}.
76       */
77      public Object getKey() {
78          return key;
79      }
80  
81      @Override
82      public String toString() {
83          return "(" + getContentType() + ")";
84      }
85  
86      @Override
87      public boolean equals(Object obj) {
88          if (this == obj) {
89              return true;
90          }
91          if (obj == null || !getClass().equals(obj.getClass())) {
92              return false;
93          }
94  
95          WorkspaceRepository that = (WorkspaceRepository) obj;
96  
97          return getContentType().equals(that.getContentType()) && getKey().equals(that.getKey());
98      }
99  
100     @Override
101     public int hashCode() {
102         int hash = 17;
103         hash = hash * 31 + getKey().hashCode();
104         hash = hash * 31 + getContentType().hashCode();
105         return hash;
106     }
107 }