View Javadoc
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.apache.maven.repository.metadata;
20  
21  import org.apache.maven.artifact.ArtifactScopeEnum;
22  
23  /**
24   * metadata graph vertice - just a wrapper around artifact's metadata
25   *
26   * @author <a href="oleg@codehaus.org">Oleg Gusakov</a>
27   */
28  public class MetadataGraphVertex implements Comparable<MetadataGraphVertex> {
29      ArtifactMetadata md;
30  
31      // indications to use these in comparison
32      private boolean compareVersion = false;
33      private boolean compareScope = false;
34  
35      public MetadataGraphVertex(ArtifactMetadata md) {
36          super();
37          this.md = md;
38      }
39  
40      public MetadataGraphVertex(ArtifactMetadata md, boolean compareVersion, boolean compareScope) {
41          this(md);
42          this.compareVersion = compareVersion;
43          this.compareScope = compareScope;
44      }
45  
46      public ArtifactMetadata getMd() {
47          return md;
48      }
49  
50      public void setMd(ArtifactMetadata md) {
51          this.md = md;
52      }
53  
54      // ---------------------------------------------------------------------
55      public boolean isCompareVersion() {
56          return compareVersion;
57      }
58  
59      public void setCompareVersion(boolean compareVersion) {
60          this.compareVersion = compareVersion;
61      }
62  
63      public boolean isCompareScope() {
64          return compareScope;
65      }
66  
67      public void setCompareScope(boolean compareScope) {
68          this.compareScope = compareScope;
69      }
70  
71      // ---------------------------------------------------------------------
72      @Override
73      public String toString() {
74          return "[" + (md == null ? "no metadata" : md.toString()) + "]";
75      }
76  
77      // ---------------------------------------------------------------------
78      private static int compareStrings(String s1, String s2) {
79          if (s1 == null && s2 == null) {
80              return 0;
81          }
82  
83          if (s1 == null /* && s2 != null */) {
84              return -1;
85          }
86  
87          if (
88          /* s1 != null && */ s2 == null) {
89              return 1;
90          }
91  
92          return s1.compareTo(s2);
93      }
94  
95      // ---------------------------------------------------------------------
96      public int compareTo(MetadataGraphVertex vertex) {
97          if (vertex == null || vertex.getMd() == null) {
98              return 1;
99          }
100 
101         ArtifactMetadata vmd = vertex.getMd();
102 
103         if (vmd == null) {
104             if (md == null) {
105                 return 0;
106             } else {
107                 return 1;
108             }
109         }
110 
111         int g = compareStrings(md.groupId, vmd.groupId);
112 
113         if (g == 0) {
114             int a = compareStrings(md.artifactId, vmd.artifactId);
115             if (a == 0) {
116                 if (compareVersion) {
117                     int v = compareStrings(md.version, vmd.version);
118                     if (v == 0) {
119                         if (compareScope) {
120                             String s1 = ArtifactScopeEnum.checkScope(md.artifactScope)
121                                     .getScope();
122                             String s2 = ArtifactScopeEnum.checkScope(vmd.artifactScope)
123                                     .getScope();
124                             return s1.compareTo(s2);
125                         } else {
126                             return 0;
127                         }
128                     } else {
129                         return v;
130                     }
131                 } else {
132                     return 0;
133                 }
134             } else {
135                 return a;
136             }
137         }
138 
139         return g;
140     }
141 
142     // ---------------------------------------------------------------------
143     @Override
144     public boolean equals(Object vo) {
145         if (!(vo instanceof MetadataGraphVertex)) {
146             return false;
147         }
148         return compareTo((MetadataGraphVertex) vo) == 0;
149     }
150 
151     // ---------------------------------------------------------------------
152 
153     @Override
154     public int hashCode() {
155         if (md == null) {
156             return super.hashCode();
157         }
158         StringBuilder hashString = new StringBuilder(128);
159         hashString.append(md.groupId).append('|');
160         hashString.append(md.artifactId).append('|');
161 
162         if (compareVersion) {
163             hashString.append(md.version).append('|');
164         }
165 
166         if (compareScope) {
167             hashString.append(md.getArtifactScope()).append('|');
168         }
169 
170         return hashString.toString().hashCode();
171 
172         // BASE64Encoder b64 = new BASE64Encoder();
173         // return b64.encode( hashString.toString().getBytes() ).hashCode();
174     }
175 
176     // ---------------------------------------------------------------------
177     // ---------------------------------------------------------------------
178 }