View Javadoc

1   /*
2    *  Licensed under the Apache License, Version 2.0 (the "License");
3    *  you may not use this file except in compliance with the License.
4    *  You may obtain a copy of the License at
5    *
6    *      http://www.apache.org/licenses/LICENSE-2.0
7    *
8    *  Unless required by applicable law or agreed to in writing, software
9    *  distributed under the License is distributed on an "AS IS" BASIS,
10   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   *  See the License for the specific language governing permissions and
12   *  limitations under the License.
13   */
14  /*
15   * This file has been modified by Chris Pheby in accordance with Section 4.2
16   * of the Apache Software License
17   */
18  package org.jadira.dependencynavigator.model;
19  
20  import java.util.List;
21  import java.util.Stack;
22  
23  public abstract class Artifact {
24  
25      public static final String DEFAULT_SCOPE = "compile";
26  
27      private String groupId;
28      private String artifactId;
29      private String version;
30      private String scope;
31  
32      private Artifact parent;
33      private ExclusionList exclusions;
34      private int selected;
35  
36      public static final int SELECTED_NONE = 0;
37      public static final int SELECTED_PATH = 1;
38      public static final int SELECTED_PRINCIPLE = 2;
39  
40      protected Artifact(Artifact parent, String groupId, String artifactId, String version, String scope) throws ArtifactInitialisationException {
41          this.parent = parent;
42          this.groupId = validate("GroupId", groupId);
43          this.artifactId = validate("ArtifactId", artifactId);
44          this.version = validate("Version", version);
45          this.scope = (scope == null || scope.trim().equals("")) ? "" : scope;
46          exclusions = new ExclusionList();
47          selected = SELECTED_NONE;
48      }
49  
50      public void addExclusion(String groupId, String artifactId) {
51          exclusions.addExclusion(groupId, artifactId);
52      }
53  
54      public boolean isExcluded(String groupId, String artifactId, Stack<Artifact> path) {
55          if (exclusions.isExcluded(groupId, artifactId)) {
56              return true;
57          }
58          if (path == null) {
59              path = new Stack<Artifact>();
60          }
61          // may fail on circular dependency
62          if (parent != null && !path.contains(parent)) {
63              path.push(this);
64              return parent.isExcluded(groupId, artifactId, path);
65          }
66          return false;
67      }
68  
69      private String validate(String type, String value) throws ArtifactInitialisationException {
70          if (value == null || value.trim().length() == 0) {
71              throw new ArtifactInitialisationException(type + " is missing");
72          }
73          return value.trim();
74      }
75  
76      public abstract boolean isLeaf();
77  
78      public abstract List<Artifact> getDependencies();
79  
80      public abstract int dependencyCount();
81  
82      public String getArtifactId() {
83          return artifactId;
84      }
85  
86      public String getGroupId() {
87          return groupId;
88      }
89  
90      public String getScope() {
91          return scope;
92      }
93  
94      public String getVersion() {
95          return version;
96      }
97  
98      public Artifact getParent() {
99          return parent;
100     }
101 
102     public int getSelected() {
103         return selected;
104     }
105 
106     public void select(int state) {
107         selected = state;
108         if (parent == null) {
109             return;
110         }
111         if (SELECTED_NONE == state) {
112             if (SELECTED_NONE != parent.getSelected()) {
113                 parent.select(SELECTED_NONE);
114             }
115         } else {
116             if (SELECTED_PATH != parent.getSelected()) {
117                 parent.select(SELECTED_PATH);
118             }
119         }
120     }
121 
122     public String getId() {
123         return groupId + ":" + artifactId + ":" + version + "(" + scope + ")";
124     }
125 
126     @Override
127     public boolean equals(Object obj) {
128         if (obj instanceof Artifact) {
129             Artifact compareTo = (Artifact) obj;
130             if (!safeCompare(groupId, compareTo.groupId)) {
131                 return false;
132             }
133             if (!safeCompare(artifactId, compareTo.artifactId)) {
134                 return false;
135             }
136             if (!safeCompare(version, compareTo.version)) {
137                 return false;
138             }
139             return true;
140         }
141         return false;
142     }
143 
144     private boolean safeCompare(String s1, String s2) {
145         return ((s1 == null) == (s2 == null)) && ((s1 == null || s1.equalsIgnoreCase(s2)));
146     }
147 
148     @Override
149     public int hashCode() {
150         return getId().hashCode();
151     }
152 
153     @Override
154     public String toString() {
155         return getId() + "[" + dependencyCount() + "]";
156     }
157 
158 }