001 /*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014 /*
015 * This file has been modified by Chris Pheby in accordance with Section 4.2
016 * of the Apache Software License
017 */
018 package org.jadira.dependencynavigator.model;
019
020 import java.util.List;
021 import java.util.Stack;
022
023 public abstract class Artifact {
024
025 public static final String DEFAULT_SCOPE = "compile";
026
027 private String groupId;
028 private String artifactId;
029 private String version;
030 private String scope;
031
032 private Artifact parent;
033 private ExclusionList exclusions;
034 private int selected;
035
036 public static final int SELECTED_NONE = 0;
037 public static final int SELECTED_PATH = 1;
038 public static final int SELECTED_PRINCIPLE = 2;
039
040 protected Artifact(Artifact parent, String groupId, String artifactId, String version, String scope) throws ArtifactInitialisationException {
041 this.parent = parent;
042 this.groupId = validate("GroupId", groupId);
043 this.artifactId = validate("ArtifactId", artifactId);
044 this.version = validate("Version", version);
045 this.scope = (scope == null || scope.trim().equals("")) ? "" : scope;
046 exclusions = new ExclusionList();
047 selected = SELECTED_NONE;
048 }
049
050 public void addExclusion(String groupId, String artifactId) {
051 exclusions.addExclusion(groupId, artifactId);
052 }
053
054 public boolean isExcluded(String groupId, String artifactId, Stack<Artifact> path) {
055 if (exclusions.isExcluded(groupId, artifactId)) {
056 return true;
057 }
058 if (path == null) {
059 path = new Stack<Artifact>();
060 }
061 // may fail on circular dependency
062 if (parent != null && !path.contains(parent)) {
063 path.push(this);
064 return parent.isExcluded(groupId, artifactId, path);
065 }
066 return false;
067 }
068
069 private String validate(String type, String value) throws ArtifactInitialisationException {
070 if (value == null || value.trim().length() == 0) {
071 throw new ArtifactInitialisationException(type + " is missing");
072 }
073 return value.trim();
074 }
075
076 public abstract boolean isLeaf();
077
078 public abstract List<Artifact> getDependencies();
079
080 public abstract int dependencyCount();
081
082 public String getArtifactId() {
083 return artifactId;
084 }
085
086 public String getGroupId() {
087 return groupId;
088 }
089
090 public String getScope() {
091 return scope;
092 }
093
094 public String getVersion() {
095 return version;
096 }
097
098 public Artifact getParent() {
099 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 }