1 /*
2 * This file is part of dependency-check-core.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
17 */
18 package org.owasp.dependencycheck.dependency;
19
20 import org.apache.commons.lang3.StringUtils;
21 import org.apache.commons.lang3.builder.CompareToBuilder;
22 import org.apache.commons.lang3.builder.EqualsBuilder;
23 import org.apache.commons.lang3.builder.HashCodeBuilder;
24 import org.jetbrains.annotations.NotNull;
25
26 import java.io.Serializable;
27 import javax.annotation.concurrent.ThreadSafe;
28
29 /**
30 * Evidence is a piece of information about a Dependency.
31 *
32 * @author Jeremy Long
33 */
34 @ThreadSafe
35 public class Evidence implements Serializable, Comparable<Evidence> {
36
37 /**
38 * The serial version UID for serialization.
39 */
40 private static final long serialVersionUID = 2402386455919067874L;
41
42 /**
43 * The name of the evidence.
44 */
45 private String name;
46
47 /**
48 * The source of the evidence.
49 */
50 private String source;
51
52 /**
53 * The value of the evidence.
54 */
55 private String value;
56
57 /**
58 * The confidence level for the evidence.
59 */
60 private Confidence confidence;
61
62 /**
63 * Whether the evidence originates from a hint.
64 */
65 private boolean fromHint;
66
67 /**
68 * Creates a new Evidence object.
69 */
70 public Evidence() {
71 }
72
73 /**
74 * Creates a new Evidence objects.
75 *
76 * @param source the source of the evidence.
77 * @param name the name of the evidence.
78 * @param value the value of the evidence.
79 * @param confidence the confidence of the evidence.
80 */
81 public Evidence(String source, String name, String value, Confidence confidence) {
82 this(source, name, value, confidence, false);
83 }
84
85 /**
86 * Creates a new Evidence objects.
87 *
88 * @param source the source of the evidence.
89 * @param name the name of the evidence.
90 * @param value the value of the evidence.
91 * @param confidence the confidence of the evidence.
92 * @param fromHint whether the evidence was introduced by a hint.
93 */
94 public Evidence(String source, String name, String value, Confidence confidence, boolean fromHint) {
95 this.source = source;
96 this.name = name;
97 this.value = value;
98 this.confidence = confidence;
99 this.fromHint = fromHint;
100 }
101
102 /**
103 * Get the value of name.
104 *
105 * @return the value of name
106 */
107 public String getName() {
108 return name;
109 }
110
111 /**
112 * Set the value of name.
113 *
114 * @param name new value of name
115 */
116 public void setName(String name) {
117 this.name = name;
118 }
119
120 /**
121 * Get the value of source.
122 *
123 * @return the value of source
124 */
125 public String getSource() {
126 return source;
127 }
128
129 /**
130 * Set the value of source.
131 *
132 * @param source new value of source
133 */
134 public void setSource(String source) {
135 this.source = source;
136 }
137
138 /**
139 * Get the value of value.
140 *
141 * @return the value of value
142 */
143 public String getValue() {
144 return value;
145 }
146
147 /**
148 * Set the value of value.
149 *
150 * @param value new value of value
151 */
152 public void setValue(String value) {
153 this.value = value;
154 }
155
156 /**
157 * Get the value of confidence.
158 *
159 * @return the value of confidence
160 */
161 public Confidence getConfidence() {
162 return confidence;
163 }
164
165 /**
166 * Set the value of confidence.
167 *
168 * @param confidence new value of confidence
169 */
170 public void setConfidence(Confidence confidence) {
171 this.confidence = confidence;
172 }
173
174 /**
175 * Get the value of fromHint.
176 *
177 * @return the value of fromHint
178 */
179 public boolean isFromHint() {
180 return fromHint;
181 }
182
183 /**
184 * Set the value of fromHint.
185 *
186 * @param fromHint new value of fromHint
187 */
188 public void setFromHint(boolean fromHint) {
189 this.fromHint = fromHint;
190 }
191
192 /**
193 * Implements the hashCode for Evidence.
194 *
195 * @return hash code.
196 */
197 @Override
198 public int hashCode() {
199 return new HashCodeBuilder(303, 367)
200 .append(StringUtils.lowerCase(name))
201 .append(StringUtils.lowerCase(source))
202 .append(StringUtils.lowerCase(value))
203 .append(confidence)
204 .toHashCode();
205 }
206
207 /**
208 * Implements equals for Evidence.
209 *
210 * @param obj an object to check the equality of.
211 * @return whether the two objects are equal.
212 */
213 @Override
214 public boolean equals(Object obj) {
215 if (obj == null || !(obj instanceof Evidence)) {
216 return false;
217 }
218 if (this == obj) {
219 return true;
220 }
221 final Evidence o = (Evidence) obj;
222 return new EqualsBuilder()
223 .append(this.source == null ? null : this.source.toLowerCase(), o.source == null ? null : o.source.toLowerCase())
224 .append(this.name == null ? null : this.name.toLowerCase(), o.name == null ? null : o.name.toLowerCase())
225 .append(this.value == null ? null : this.value.toLowerCase(), o.value == null ? null : o.value.toLowerCase())
226 .append(this.confidence, o.getConfidence())
227 .append(this.fromHint, o.isFromHint())
228 .build();
229 }
230
231 /**
232 * Implementation of the comparable interface.
233 *
234 * @param o the evidence being compared
235 * @return an integer indicating the ordering of the two objects
236 */
237 @Override
238 public int compareTo(@NotNull Evidence o) {
239 return new CompareToBuilder()
240 .append(this.source == null ? null : this.source.toLowerCase(), o.source == null ? null : o.source.toLowerCase())
241 .append(this.name == null ? null : this.name.toLowerCase(), o.name == null ? null : o.name.toLowerCase())
242 .append(this.value == null ? null : this.value.toLowerCase(), o.value == null ? null : o.value.toLowerCase())
243 .append(this.confidence, o.getConfidence())
244 .append(this.fromHint, o.isFromHint())
245 .toComparison();
246 }
247
248 /**
249 * Standard toString() implementation.
250 *
251 * @return the string representation of the object
252 */
253 @Override
254 public String toString() {
255 return "Evidence{" + "name=" + name + ", source=" + source + ", value=" + value + ", confidence=" + confidence
256 + ", fromHint=" + fromHint + '}';
257 }
258 }