| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Evidence |
|
| 3.380952380952381;3.381 |
| 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 | /** | |
| 21 | * Evidence is a piece of information about a Dependency. | |
| 22 | * | |
| 23 | * @author Jeremy Long <jeremy.long@owasp.org> | |
| 24 | */ | |
| 25 | 39714 | public class Evidence implements Comparable<Evidence> { |
| 26 | ||
| 27 | /** | |
| 28 | * Creates a new Evidence object. | |
| 29 | */ | |
| 30 | public Evidence() { | |
| 31 | } | |
| 32 | ||
| 33 | /** | |
| 34 | * Creates a new Evidence objects. | |
| 35 | * | |
| 36 | * @param source the source of the evidence. | |
| 37 | * @param name the name of the evidence. | |
| 38 | * @param value the value of the evidence. | |
| 39 | * @param confidence the confidence of the evidence. | |
| 40 | */ | |
| 41 | public Evidence(String source, String name, String value, Confidence confidence) { | |
| 42 | this.source = source; | |
| 43 | this.name = name; | |
| 44 | this.value = value; | |
| 45 | this.confidence = confidence; | |
| 46 | } | |
| 47 | /** | |
| 48 | * The name of the evidence. | |
| 49 | */ | |
| 50 | private String name; | |
| 51 | ||
| 52 | /** | |
| 53 | * Get the value of name. | |
| 54 | * | |
| 55 | * @return the value of name | |
| 56 | */ | |
| 57 | public String getName() { | |
| 58 | return name; | |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * Set the value of name. | |
| 63 | * | |
| 64 | * @param name new value of name | |
| 65 | */ | |
| 66 | public void setName(String name) { | |
| 67 | this.name = name; | |
| 68 | } | |
| 69 | /** | |
| 70 | * The source of the evidence. | |
| 71 | */ | |
| 72 | private String source; | |
| 73 | ||
| 74 | /** | |
| 75 | * Get the value of source. | |
| 76 | * | |
| 77 | * @return the value of source | |
| 78 | */ | |
| 79 | public String getSource() { | |
| 80 | return source; | |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Set the value of source. | |
| 85 | * | |
| 86 | * @param source new value of source | |
| 87 | */ | |
| 88 | public void setSource(String source) { | |
| 89 | this.source = source; | |
| 90 | } | |
| 91 | /** | |
| 92 | * The value of the evidence. | |
| 93 | */ | |
| 94 | private String value; | |
| 95 | ||
| 96 | /** | |
| 97 | * Get the value of value. | |
| 98 | * | |
| 99 | * @return the value of value | |
| 100 | */ | |
| 101 | public String getValue() { | |
| 102 | 859 | used = true; |
| 103 | 859 | return value; |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Get the value of value. If setUsed is set to false this call to get will not mark the evidence as used. | |
| 108 | * | |
| 109 | * @param setUsed whether or not this call to getValue should cause the used flag to be updated | |
| 110 | * @return the value of value | |
| 111 | */ | |
| 112 | public String getValue(Boolean setUsed) { | |
| 113 | 50 | used = used || setUsed; |
| 114 | 50 | return value; |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * Set the value of value. | |
| 119 | * | |
| 120 | * @param value new value of value | |
| 121 | */ | |
| 122 | public void setValue(String value) { | |
| 123 | this.value = value; | |
| 124 | } | |
| 125 | /** | |
| 126 | * A value indicating if the Evidence has been "used" (aka read). | |
| 127 | */ | |
| 128 | private boolean used; | |
| 129 | ||
| 130 | /** | |
| 131 | * Get the value of used. | |
| 132 | * | |
| 133 | * @return the value of used | |
| 134 | */ | |
| 135 | public boolean isUsed() { | |
| 136 | return used; | |
| 137 | } | |
| 138 | ||
| 139 | /** | |
| 140 | * Set the value of used. | |
| 141 | * | |
| 142 | * @param used new value of used | |
| 143 | */ | |
| 144 | public void setUsed(boolean used) { | |
| 145 | this.used = used; | |
| 146 | } | |
| 147 | /** | |
| 148 | * The confidence level for the evidence. | |
| 149 | */ | |
| 150 | private Confidence confidence; | |
| 151 | ||
| 152 | /** | |
| 153 | * Get the value of confidence. | |
| 154 | * | |
| 155 | * @return the value of confidence | |
| 156 | */ | |
| 157 | public Confidence getConfidence() { | |
| 158 | return confidence; | |
| 159 | } | |
| 160 | ||
| 161 | /** | |
| 162 | * Set the value of confidence. | |
| 163 | * | |
| 164 | * @param confidence new value of confidence | |
| 165 | */ | |
| 166 | public void setConfidence(Confidence confidence) { | |
| 167 | this.confidence = confidence; | |
| 168 | } | |
| 169 | ||
| 170 | /** | |
| 171 | * Implements the hashCode for Evidence. | |
| 172 | * | |
| 173 | * @return hash code. | |
| 174 | */ | |
| 175 | @Override | |
| 176 | public int hashCode() { | |
| 177 | 2 | int hash = 3; |
| 178 | 2 | hash = 67 * hash + (this.name != null ? this.name.hashCode() : 0); |
| 179 | 2 | hash = 67 * hash + (this.source != null ? this.source.hashCode() : 0); |
| 180 | 2 | hash = 67 * hash + (this.value != null ? this.value.hashCode() : 0); |
| 181 | 2 | hash = 67 * hash + (this.confidence != null ? this.confidence.hashCode() : 0); |
| 182 | 2 | return hash; |
| 183 | } | |
| 184 | ||
| 185 | /** | |
| 186 | * Implements equals for Evidence. | |
| 187 | * | |
| 188 | * @param that an object to check the equality of. | |
| 189 | * @return whether the two objects are equal. | |
| 190 | */ | |
| 191 | @Override | |
| 192 | public boolean equals(Object that) { | |
| 193 | 0 | if (this == that) { |
| 194 | 0 | return true; |
| 195 | } | |
| 196 | 0 | if (!(that instanceof Evidence)) { |
| 197 | 0 | return false; |
| 198 | } | |
| 199 | 0 | final Evidence e = (Evidence) that; |
| 200 | ||
| 201 | 0 | return testEquality(name, e.name) && testEquality(source, e.source) && testEquality(value, e.value) |
| 202 | && (confidence == null ? e.confidence == null : confidence == e.confidence); | |
| 203 | } | |
| 204 | ||
| 205 | /** | |
| 206 | * Simple equality test for use within the equals method. This does a case insensitive compare. | |
| 207 | * | |
| 208 | * @param l a string to compare. | |
| 209 | * @param r another string to compare. | |
| 210 | * @return whether the two strings are the same. | |
| 211 | */ | |
| 212 | private boolean testEquality(String l, String r) { | |
| 213 | 0 | return l == null ? r == null : l.equalsIgnoreCase(r); |
| 214 | } | |
| 215 | ||
| 216 | /** | |
| 217 | * Implementation of the comparable interface. | |
| 218 | * | |
| 219 | * @param o the evidence being compared | |
| 220 | * @return an integer indicating the ordering of the two objects | |
| 221 | */ | |
| 222 | public int compareTo(Evidence o) { | |
| 223 | 39714 | if (o == null) { |
| 224 | 0 | return 1; |
| 225 | } | |
| 226 | 39714 | if (equalsWithNullCheck(source, o.source)) { |
| 227 | 25251 | if (equalsWithNullCheck(name, o.name)) { |
| 228 | 25158 | if (equalsWithNullCheck(value, o.value)) { |
| 229 | 15445 | if (equalsWithNullCheck(confidence, o.confidence)) { |
| 230 | 15432 | return 0; //they are equal |
| 231 | } else { | |
| 232 | 13 | return compareToWithNullCheck(confidence, o.confidence); |
| 233 | } | |
| 234 | } else { | |
| 235 | 9713 | return compareToIgnoreCaseWithNullCheck(value, o.value); |
| 236 | } | |
| 237 | } else { | |
| 238 | 93 | return compareToIgnoreCaseWithNullCheck(name, o.name); |
| 239 | } | |
| 240 | } else { | |
| 241 | 14463 | return compareToIgnoreCaseWithNullCheck(source, o.source); |
| 242 | } | |
| 243 | } | |
| 244 | ||
| 245 | /** | |
| 246 | * Equality check with an exhaustive, possibly duplicative, check against nulls. | |
| 247 | * | |
| 248 | * @param me the value to be compared | |
| 249 | * @param other the other value to be compared | |
| 250 | * @return true if the values are equal; otherwise false | |
| 251 | */ | |
| 252 | private boolean equalsWithNullCheck(String me, String other) { | |
| 253 | 90123 | if (me == null && other == null) { |
| 254 | 0 | return true; |
| 255 | 90123 | } else if (me == null || other == null) { |
| 256 | 0 | return false; |
| 257 | } | |
| 258 | 90123 | return me.equals(other); |
| 259 | } | |
| 260 | ||
| 261 | /** | |
| 262 | * Equality check with an exhaustive, possibly duplicative, check against nulls. | |
| 263 | * | |
| 264 | * @param me the value to be compared | |
| 265 | * @param other the other value to be compared | |
| 266 | * @return true if the values are equal; otherwise false | |
| 267 | */ | |
| 268 | private boolean equalsWithNullCheck(Confidence me, Confidence other) { | |
| 269 | 15445 | if (me == null && other == null) { |
| 270 | 0 | return true; |
| 271 | 15445 | } else if (me == null || other == null) { |
| 272 | 0 | return false; |
| 273 | } | |
| 274 | 15445 | return me.equals(other); |
| 275 | } | |
| 276 | ||
| 277 | /** | |
| 278 | * Wrapper around {@link java.lang.String#compareToIgnoreCase(java.lang.String) String.compareToIgnoreCase} with an | |
| 279 | * exhaustive, possibly duplicative, check against nulls. | |
| 280 | * | |
| 281 | * @param me the value to be compared | |
| 282 | * @param other the other value to be compared | |
| 283 | * @return true if the values are equal; otherwise false | |
| 284 | */ | |
| 285 | private int compareToIgnoreCaseWithNullCheck(String me, String other) { | |
| 286 | 24269 | if (me == null && other == null) { |
| 287 | 0 | return 0; |
| 288 | 24269 | } else if (me == null) { |
| 289 | 0 | return -1; //the other string is greater then me |
| 290 | 24269 | } else if (other == null) { |
| 291 | 0 | return 1; //me is greater then the other string |
| 292 | } | |
| 293 | 24269 | return me.compareToIgnoreCase(other); |
| 294 | } | |
| 295 | ||
| 296 | /** | |
| 297 | * Wrapper around {@link java.lang.Enum#compareTo(java.lang.Enum) Enum.compareTo} with an exhaustive, possibly | |
| 298 | * duplicative, check against nulls. | |
| 299 | * | |
| 300 | * @param me the value to be compared | |
| 301 | * @param other the other value to be compared | |
| 302 | * @return true if the values are equal; otherwise false | |
| 303 | */ | |
| 304 | private int compareToWithNullCheck(Confidence me, Confidence other) { | |
| 305 | 13 | if (me == null && other == null) { |
| 306 | 0 | return 0; |
| 307 | 13 | } else if (me == null) { |
| 308 | 0 | return -1; //the other string is greater then me |
| 309 | 13 | } else if (other == null) { |
| 310 | 0 | return 1; //me is greater then the other string |
| 311 | } | |
| 312 | 13 | return me.compareTo(other); |
| 313 | } | |
| 314 | } |