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) 2023 Jeremy Long. All Rights Reserved.
17 */
18 package org.owasp.dependencycheck.dependency;
19
20 import org.apache.commons.lang3.builder.CompareToBuilder;
21 import org.jspecify.annotations.NonNull;
22
23 import java.io.Serializable;
24 import java.util.Objects;
25
26 /**
27 * POJO to store a reference to the "included by" node in a dependency tree;
28 * where included by is the root node that caused a dependency to be included.
29 *
30 * @author Jeremy Long
31 */
32 public class IncludedByReference implements Serializable, Comparable<IncludedByReference> {
33
34 /**
35 * The serial version UID for serialization.
36 */
37 private static final long serialVersionUID = 4339975160204621746L;
38
39 /**
40 * The reference.
41 */
42 private final String reference;
43 /**
44 * The reference's type.
45 */
46 private final String type;
47
48 /**
49 * Constructs a new reference.
50 *
51 * @param reference the reference
52 * @param type the reference's type
53 */
54 public IncludedByReference(String reference, String type) {
55 this.reference = reference;
56 this.type = type;
57 }
58
59 /**
60 * Get the value of reference.
61 *
62 * @return the value of reference
63 */
64 public String getReference() {
65 return reference;
66 }
67
68 /**
69 * Get the value of type.
70 *
71 * @return the value of type
72 */
73 public String getType() {
74 return type;
75 }
76
77 @Override
78 public boolean equals(Object o) {
79 if (!(o instanceof IncludedByReference)) return false;
80 IncludedByReference that = (IncludedByReference) o;
81 return Objects.equals(type, that.type) && Objects.equals(reference, that.reference);
82 }
83
84 @Override
85 public int hashCode() {
86 return Objects.hash(type, reference);
87 }
88
89 @Override
90 public int compareTo(@NonNull IncludedByReference o) {
91 return new CompareToBuilder()
92 .append(type, o.type) // Group by type (nulls-first)
93 .append(reference, o.reference) // then the actual reference
94 .toComparison();
95 }
96
97 @Override
98 public String toString() {
99 return "IncludedByReference{reference='" + reference + "', type='" + type + "'}";
100 }
101 }