Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7d1d20d9f71d70b2c97b8413ba891a00e7ab775d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.embedder;

import java.io.Serializable;

import org.eclipse.osgi.util.NLS;

import org.apache.maven.artifact.Artifact;


public class ArtifactKey implements Serializable {
  private static final long serialVersionUID = -8984509272834024387L;

  private final String groupId;

  private final String artifactId;

  private final String version;

  private final String classifier;

  /**
   * Note that this constructor uses Artifact.getBaseVersion
   */
  public ArtifactKey(Artifact a) {
    this(a.getGroupId(), a.getArtifactId(), a.getBaseVersion(), null);
  }

  public ArtifactKey(org.eclipse.aether.artifact.Artifact a) {
    this(a.getGroupId(), a.getArtifactId(), a.getBaseVersion(), null);
  }

  public ArtifactKey(String groupId, String artifactId, String version, String classifier) {
    this.groupId = groupId;
    this.artifactId = artifactId;
    this.version = version;
    this.classifier = classifier;
  }

  public boolean equals(Object o) {
    if(this == o)
      return true;
    if(o instanceof ArtifactKey) {
      ArtifactKey other = (ArtifactKey) o;
      return equals(groupId, other.groupId) && equals(artifactId, other.artifactId) && equals(version, other.version)
          && equals(classifier, other.classifier);
    }
    return false;
  }

  public int hashCode() {
    int hash = 17;
    hash = hash * 31 + (groupId != null ? groupId.hashCode() : 0);
    hash = hash * 31 + (artifactId != null ? artifactId.hashCode() : 0);
    hash = hash * 31 + (version != null ? version.hashCode() : 0);
    hash = hash * 31 + (classifier != null ? classifier.hashCode() : 0);
    return hash;
  }

  private static boolean equals(Object o1, Object o2) {
    return o1 == null ? o2 == null : o1.equals(o2);
  }

  // XXX this method does not belong here, it compares versions, while ArtifactKey uses baseVersions in many cases
  public static boolean equals(Artifact a1, Artifact a2) {
    if(a1 == null) {
      return a2 == null;
    }
    if(a2 == null) {
      return false;
    }
    return equals(a1.getGroupId(), a2.getGroupId()) && equals(a1.getArtifactId(), a2.getArtifactId())
        && equals(a1.getVersion(), a2.getVersion()) && equals(a1.getClassifier(), a2.getClassifier());
  }

  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(groupId).append(':').append(artifactId).append(':').append(version);
    if(classifier != null) {
      sb.append(':').append(classifier);
    }
    return sb.toString();
  }

  public static ArtifactKey fromPortableString(String str) {
    int p, c;

    p = 0;
    c = nextColonIndex(str, p);
    String groupId = substring(str, p, c);

    p = c + 1;
    c = nextColonIndex(str, p);
    String artifactId = substring(str, p, c);

    p = c + 1;
    c = nextColonIndex(str, p);
    String version = substring(str, p, c);

    p = c + 1;
    c = nextColonIndex(str, p);
    String classifier = substring(str, p, c);

    return new ArtifactKey(groupId, artifactId, version, classifier);
  }

  private static String substring(String str, int start, int end) {
    String substring = str.substring(start, end);
    return "".equals(substring) ? null : substring; //$NON-NLS-1$
  }

  private static int nextColonIndex(String str, int pos) {
    int idx = str.indexOf(':', pos);
    if(idx < 0)
      throw new IllegalArgumentException(NLS.bind("Invalid portable string: {0}", str));
    return idx;
  }

  public String toPortableString() {
    StringBuilder sb = new StringBuilder();
    if(groupId != null)
      sb.append(groupId);
    sb.append(':');
    if(artifactId != null)
      sb.append(artifactId);
    sb.append(':');
    if(version != null)
      sb.append(version);
    sb.append(':');
    if(classifier != null)
      sb.append(classifier);
    sb.append(':');
    return sb.toString();
  }

  public String getGroupId() {
    return groupId;
  }

  public String getArtifactId() {
    return artifactId;
  }

  public String getVersion() {
    return version;
  }

  public String getClassifier() {
    return classifier;
  }

}

Back to the top