-
Notifications
You must be signed in to change notification settings - Fork 315
/
LicenseClassifications.kt
119 lines (101 loc) · 4.48 KB
/
LicenseClassifications.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright (C) 2017-2019 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package org.ossreviewtoolkit.model.licenses
import com.fasterxml.jackson.annotation.JsonAlias
import com.fasterxml.jackson.annotation.JsonInclude
import java.util.SortedSet
import org.ossreviewtoolkit.spdx.SpdxExpression
import org.ossreviewtoolkit.spdx.SpdxSingleLicenseExpression
import org.ossreviewtoolkit.spdx.getDuplicates
/**
* Classifications for licenses which allow to assign metadata to licenses. This allows defining rather generic
* categories and assigning licenses to these. That way flexible classifications can be created based on
* customizable categories. The available license categories need to be declared explicitly; when creating an
* instance, it is checked that all the references from the [categorizations] point to existing [categories].
*/
data class LicenseClassifications(
/**
* Defines metadata for the license categories.
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonAlias("license_sets")
val categories: List<LicenseCategory> = emptyList(),
/**
* Defines metadata for licenses.
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonAlias("licenses")
val categorizations: List<LicenseCategorization> = emptyList()
) {
/** A property for fast look-ups of licenses for a given category. */
val licensesByCategory: Map<String, Set<SpdxSingleLicenseExpression>> by lazy {
val result = mutableMapOf<String, MutableSet<SpdxSingleLicenseExpression>>()
categorizations.forEach { license ->
license.categories.forEach { category ->
result.getOrPut(category) { mutableSetOf() } += license.id
}
}
result
}
/** A property for fast look-ups of categories for a given license. */
val categoriesByLicense: Map<SpdxSingleLicenseExpression, Set<String>> by lazy {
val result = mutableMapOf<SpdxSingleLicenseExpression, Set<String>>()
categorizations.forEach { license ->
result[license.id] = license.categories
}
result
}
/** A property allowing convenient access to the names of all categories defined. */
val categoryNames: SortedSet<String> by lazy {
categories.mapTo(sortedSetOf()) { it.name }
}
init {
categories.getDuplicates { it.name }.let { duplicates ->
require(duplicates.isEmpty()) {
"Found multiple license category entries with the same name: $duplicates"
}
}
categorizations.getDuplicates { it.id }.let { duplicates ->
require(duplicates.isEmpty()) {
"Found multiple license entries with the same id: $duplicates"
}
}
categorizations.associateWith { it.categories.filterNot(categoryNames::contains) }
.filterNot { it.value.isEmpty() }
.let { invalidCategorizations ->
require(invalidCategorizations.isEmpty()) {
val licenseIds = invalidCategorizations.keys.joinToString { it.id.toString() }
val categories = invalidCategorizations.values.flatten().toSet()
"Found licenses that reference non-existing categories: $licenseIds; " +
"unknown categories are $categories."
}
}
}
/**
* A convenience operator to return the categories for the given license [id], or null if the license is not
* categorized.
*/
operator fun get(id: SpdxExpression) = categoriesByLicense[id]
/** A convenience function to check whether there is a categorization for the given license [id]. */
fun isCategorized(id: SpdxExpression) = id in categoriesByLicense
}
/**
* A convenience extension function to return empty LicenseClassifications for null.
*/
fun LicenseClassifications?.orEmpty(): LicenseClassifications = this ?: LicenseClassifications()