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 /**
32 * The identifier of this workspace repository type, returned by {@link #getId()}.
33 */
34 public static final String ID = "workspace";
35
36 private final String type;
37
38 private final Object key;
39
40 private final int hashCode;
41
42 /**
43 * Creates a new workspace repository of type {@code "workspace"} and a random key.
44 */
45 public WorkspaceRepository() {
46 this("workspace");
47 }
48
49 /**
50 * Creates a new workspace repository with the specified type and a random key.
51 *
52 * @param type The type of the repository, may be {@code null}.
53 */
54 public WorkspaceRepository(String type) {
55 this(type, null);
56 }
57
58 /**
59 * Creates a new workspace repository with the specified type and key. The key is used to distinguish one workspace
60 * from another and should be sensitive to the artifacts that are (potentially) available in the workspace.
61 *
62 * @param type The type of the repository, may be {@code null}.
63 * @param key The (comparison) key for the repository, may be {@code null} to generate a unique random key.
64 */
65 public WorkspaceRepository(String type, Object key) {
66 this.type = (type != null) ? type : "";
67 this.key = (key != null) ? key : UUID.randomUUID().toString().replace("-", "");
68 this.hashCode = Objects.hash(type, key);
69 }
70
71 public String getContentType() {
72 return type;
73 }
74
75 public String getId() {
76 return ID;
77 }
78
79 /**
80 * Gets the key of this workspace repository. The key is used to distinguish one workspace from another and should
81 * be sensitive to the artifacts that are (potentially) available in the workspace.
82 *
83 * @return The (comparison) key for this workspace repository, never {@code null}.
84 */
85 public Object getKey() {
86 return key;
87 }
88
89 @Override
90 public String toString() {
91 return "(" + getContentType() + ")";
92 }
93
94 @Override
95 public boolean equals(Object obj) {
96 if (this == obj) {
97 return true;
98 }
99 if (obj == null || !getClass().equals(obj.getClass())) {
100 return false;
101 }
102
103 WorkspaceRepository that = (WorkspaceRepository) obj;
104
105 return getContentType().equals(that.getContentType()) && getKey().equals(that.getKey());
106 }
107
108 @Override
109 public int hashCode() {
110 return hashCode;
111 }
112 }