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