Debian Codegraph @

bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/internal/p2/metadata/ArtifactKey.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2007, 2023 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.equinox.internal.p2.metadata;
15
16 import java.util.Objects;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.equinox.p2.metadata.IArtifactKey;
19 import org.eclipse.equinox.p2.metadata.Version;
20 import org.eclipse.equinox.p2.metadata.expression.IMemberProvider;
21
22 /**
23 * The concrete type for representing IArtifactKey's.
24 * <p>
25 * See {link IArtifact for a description of the lifecycle of artifact keys)
26 */
27 public class ArtifactKey implements IArtifactKey, IMemberProvider {
28 private static final String SEPARATOR = ","; //$NON-NLS-1$
29
30 public static final String MEMBER_ID = "id"; //$NON-NLS-1$
31 public static final String MEMBER_CLASSIFIER = "classifier"; //$NON-NLS-1$
32 public static final String MEMBER_VERSION = "version"; //$NON-NLS-1$
33
34 private final String id;
35 private final String classifier;
36 private final Version version;
37
38 public static IArtifactKey parse(String specification) {
39 String[] parts = specification.split(SEPARATOR, -1);
40 if (parts.length < 2 || parts.length > 3) {
41 throw new IllegalArgumentException("Unexpected number of parts in artifact key: " + specification); //$NON-NLS-1$
42 }
43 Version version = Version.emptyVersion;
44 if (parts.length == 3 && !parts[2].isBlank()) {
45 version = Version.parseVersion(parts[2]);
46 }
47 try {
48 return new ArtifactKey(parts[0], parts[1], version);
49 } catch (IllegalArgumentException e) {
50 throw (IllegalArgumentException) new IllegalArgumentException(
51 "Wrong version syntax in artifact key: " + specification).initCause(e); //$NON-NLS-1$
52 }
53 }
54
55 public ArtifactKey(String classifier, String id, Version version) {
56 Assert.isNotNull(classifier);
57 Assert.isNotNull(id);
58 Assert.isNotNull(version);
59 if (classifier.contains(SEPARATOR)) {
60 throw new IllegalArgumentException("comma not allowed in classifier"); //$NON-NLS-1$
61 }
62 if (id.contains(SEPARATOR)) {
63 throw new IllegalArgumentException("comma not allowed in id"); //$NON-NLS-1$
64 }
65 this.classifier = classifier;
66 this.id = id;
67 this.version = version;
68 }
69
70 public ArtifactKey(IArtifactKey artifactKey) {
71 this.classifier = artifactKey.getClassifier();
72 this.id = artifactKey.getId();
73 this.version = artifactKey.getVersion();
74 }
75
76 @Override
77 public String getClassifier() {
78 return classifier;
79 }
80
81 @Override
82 public Version getVersion() {
83 return version;
84 }
85
86 @Override
87 public int hashCode() {
88 return Objects.hash(id, getVersion(), classifier);
89 }
90
91 @Override
92 public String toString() {
93 return classifier + SEPARATOR + id + SEPARATOR + getVersion();
94 }
95
96 @Override
97 public boolean equals(Object obj) {
98 if (this == obj) {
99 return true;
100 }
101 return obj instanceof IArtifactKey ak //
102 && ak.getId().equals(id) && ak.getVersion().equals(getVersion())
103 && ak.getClassifier().equals(classifier);
104 }
105
106 @Override
107 public String getId() {
108 return id;
109 }
110
111 @Override
112 public String toExternalForm() {
113 return String.join(SEPARATOR, classifier, id, version.toString());
114 }
115
116 @Override
117 public Object getMember(String memberName) {
118 return switch (memberName) {
119 case MEMBER_ID -> id;
120 case MEMBER_VERSION -> version;
121 case MEMBER_CLASSIFIER -> classifier;
122 default -> throw new IllegalArgumentException("No such member: " + memberName); //$NON-NLS-1$
123 };
124 }
125 }