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) 2019 Jeremy Long. All Rights Reserved.
17 */
18 package org.owasp.dependencycheck.dependency.naming;
19
20 import javax.annotation.concurrent.ThreadSafe;
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 import org.owasp.dependencycheck.dependency.Confidence;
26
27 /**
28 * In identifier such as a CPE or dependency coordinates (i.e. GAV).
29 *
30 * @author Jeremy Long
31 */
32 @ThreadSafe
33 public class GenericIdentifier implements Identifier {
34
35 /**
36 * The serial version UID for serialization.
37 */
38 private static final long serialVersionUID = 8683243972735598200L;
39
40 /**
41 * The confidence that this is the correct identifier.
42 */
43 private Confidence confidence;
44 /**
45 * The value of the identifier
46 */
47 private final String value;
48 /**
49 * The URL for the identifier.
50 */
51 private String url;
52 /**
53 * Notes about the vulnerability. Generally used for suppression
54 * information.
55 */
56 private String notes;
57
58 /**
59 * Constructs a new Identifier with the specified data.
60 *
61 * @param value the identifier value
62 * @param confidence the confidence level that the identifier is correct
63 */
64 public GenericIdentifier(String value, Confidence confidence) {
65 this.confidence = confidence;
66 this.value = value;
67 this.url = null;
68 }
69
70 /**
71 * Constructs a new Identifier with the specified data.
72 *
73 * @param confidence the confidence level that the identifier is correct
74 * @param value the identifier value
75 * @param url the identifier URL
76 */
77 public GenericIdentifier(String value, String url, Confidence confidence) {
78 this.confidence = confidence;
79 this.value = value;
80 this.url = url;
81 }
82
83 /**
84 * {@inheritDoc}
85 */
86 @Override
87 public Confidence getConfidence() {
88 return confidence;
89 }
90
91 /**
92 * {@inheritDoc}
93 */
94 @Override
95 public String getValue() {
96 return value;
97 }
98
99 /**
100 * {@inheritDoc}
101 */
102 @Override
103 public String getUrl() {
104 return url;
105 }
106
107 /**
108 * {@inheritDoc}
109 */
110 @Override
111 public String getNotes() {
112 return notes;
113 }
114
115 /**
116 * {@inheritDoc}
117 */
118 @Override
119 public void setConfidence(Confidence confidence) {
120 this.confidence = confidence;
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 @Override
127 public void setUrl(String url) {
128 this.url = url;
129 }
130
131 /**
132 * {@inheritDoc}
133 */
134 @Override
135 public void setNotes(String notes) {
136 this.notes = notes;
137 }
138
139 /**
140 * Basic implementation of equals.
141 *
142 * @param obj the identifier to compare
143 * @return true if the objects are equal
144 */
145 @Override
146 public boolean equals(Object obj) {
147 if (obj == null || !(obj instanceof GenericIdentifier)) {
148 return false;
149 }
150 if (this == obj) {
151 return true;
152 }
153 final GenericIdentifier other = (GenericIdentifier) obj;
154 return new EqualsBuilder()
155 .append(this.value, other.value)
156 .append(this.url, other.url)
157 .append(this.confidence, other.confidence)
158 .isEquals();
159 }
160
161 /**
162 * Basic implementation of hasCode.
163 *
164 * @return the hash code
165 */
166 @Override
167 public int hashCode() {
168 return new HashCodeBuilder(5, 49)
169 .append(value)
170 .append(url)
171 .append(confidence)
172 .toHashCode();
173 }
174
175 /**
176 * Standard implementation of toString; displays identifier value and type.
177 *
178 * @return a String representation of the object
179 */
180 @Override
181 public String toString() {
182 return value;
183 }
184
185 /**
186 * Implementation of the comparator interface.
187 *
188 * @param o the object being compared
189 * @return an integer indicating the ordering
190 */
191 @Override
192 public int compareTo(@NotNull Identifier o) {
193 return new CompareToBuilder()
194 .append(this.value, o.toString())
195 .append(this.url, o.getUrl())
196 .append(this.confidence, o.getConfidence())
197 .toComparison();
198 }
199 }