Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 63c4f27d0a5888d66f888d4c4812e220a739b715 (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
163
164
165
/*******************************************************************************
 * 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.internal.index;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

import org.apache.maven.artifact.versioning.ArtifactVersion;


public class IndexedArtifact implements Comparable<IndexedArtifact> {

  public static final Comparator<IndexedArtifactFile> FILE_INFO_COMPARATOR = new Comparator<IndexedArtifactFile>() {

    public int compare(IndexedArtifactFile f1, IndexedArtifactFile f2) {
      ArtifactVersion v1 = f1.getArtifactVersion();
      ArtifactVersion v2 = f2.getArtifactVersion();
      int r = -v1.compareTo(v2);
      if(r != 0) {
        return r;
      }

      String c1 = f1.classifier;
      String c2 = f2.classifier;
      if(c1 == null) {
        return c2 == null ? 0 : -1;
      }
      if(c2 == null) {
        return 1;
      }
      return c1.compareTo(c2);
    }

  };

  private final String group;

  private final String artifact;

  private final String packageName;

  private final String className;

  private final String packaging;

  //a non-zero odd-prime hash seed
  private static final int SEED = 17;

  /**
   * Set<IndexedArtifactFile>
   */
  private final Set<IndexedArtifactFile> files = new TreeSet<IndexedArtifactFile>(FILE_INFO_COMPARATOR);

  public IndexedArtifact(String group, String artifact, String packageName, String className, String packaging) {
    this.group = group;
    this.artifact = artifact;
    this.packageName = packageName;
    this.className = className;
    this.packaging = packaging;
  }

  public void addFile(IndexedArtifactFile indexedArtifactFile) {
    getFiles().add(indexedArtifactFile);
  }

  public String getPackageName() {
    if(packageName != null && packageName.startsWith(".") && packageName.length() > 1) { //$NON-NLS-1$
      return packageName.substring(1);
    }
    return packageName;
  }

  public String toString() {
    StringBuffer sb = new StringBuffer(
        "\n" + getClassname() + "  " + packageName + "  " + getGroupId() + " : " + getArtifactId()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    return sb.toString();
  }

  public String getGroupId() {
    return group;
  }

  public String getArtifactId() {
    return artifact;
  }

  public String getPackaging() {
    return packaging;
  }

  public String getClassname() {
    return className;
  }

  public Set<IndexedArtifactFile> getFiles() {
    return files;
  }

  /* (non-Javadoc)
   * @see java.lang.Object#hashCode()
   */
  public int hashCode() {
    int result = SEED;
    result *= fieldHash(getGroupId());
    result *= fieldHash(getArtifactId());
    result *= fieldHash(getPackaging());
    result *= fieldHash(getClassname());
    result *= fieldHash(getPackageName());
    return result;
  }

  private int fieldHash(Object field) {
    if(field == null) {
      return SEED;
    }
    return field.hashCode();
  }

  /**
   * Assumes all the fields are important for equals.
   */
  public boolean equals(Object artifact) {
    if(this == artifact) {
      return true;
    } else if(!(artifact instanceof IndexedArtifact)) {
      return false;
    } else {
      IndexedArtifact other = (IndexedArtifact) artifact;
      return fieldsEqual(this.getGroupId(), other.getGroupId())
          && fieldsEqual(this.getArtifactId(), other.getArtifactId())
          && fieldsEqual(this.getPackageName(), other.getPackageName())
          && fieldsEqual(this.getPackaging(), other.getPackaging())
          && fieldsEqual(this.getClassname(), other.getClassname());
    }
  }

  private boolean fieldsEqual(Object field1, Object field2) {
    return field1 == null ? field2 == null : field1.equals(field2);
  }

  public int compareTo(IndexedArtifact o) {
    if(this.equals(o))
      return 0;
    int comparison = 0;
    if(group != null && (comparison = group.compareTo(o.getGroupId())) != 0)
      return comparison;
    if(artifact != null && (comparison = artifact.compareTo(o.getArtifactId())) != 0)
      return comparison;
    if(packageName != null && (comparison = packageName.compareTo(o.getPackageName())) != 0)
      return comparison;
    if(className != null && (comparison = className.compareTo(o.getClassname())) != 0)
      return comparison;
    return 0;
  }
}

Back to the top