Skip to content

Commit

Permalink
refactor(reporter)!: Use default interface implementations to reduce …
Browse files Browse the repository at this point in the history
…code

Use default implementations for the `getLicenseText()` and
`hasLicenseText()` interface functions that refer to
`getLicenseTextReader()`, so usually only that function needs to be
implemented. That allows to remove the code for some function overrides
as well as the `hasLicenseText()` stand-alone function itself.

Note that this is only an intermediate step towards a bigger refactoring
that plans to make license text providers first-class plugins in ORT.

Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Dec 8, 2023
1 parent 1109a7a commit ce6839d
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 24 deletions.
16 changes: 0 additions & 16 deletions reporter/src/main/kotlin/DefaultLicenseTextProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,13 @@ package org.ossreviewtoolkit.reporter

import java.io.File

import org.ossreviewtoolkit.utils.spdx.getLicenseText
import org.ossreviewtoolkit.utils.spdx.getLicenseTextReader
import org.ossreviewtoolkit.utils.spdx.hasLicenseText

class DefaultLicenseTextProvider(private val licenseTextDirectories: List<File> = emptyList()) : LicenseTextProvider {
override fun getLicenseText(licenseId: String): String? =
getLicenseText(
id = licenseId,
handleExceptions = true,
licenseTextDirectories = licenseTextDirectories
)

override fun getLicenseTextReader(licenseId: String): (() -> String)? =
getLicenseTextReader(
id = licenseId,
handleExceptions = true,
licenseTextDirectories = licenseTextDirectories
)

override fun hasLicenseText(licenseId: String): Boolean =
hasLicenseText(
id = licenseId,
handleExceptions = true,
licenseTextDirectories = licenseTextDirectories
)
}
4 changes: 2 additions & 2 deletions reporter/src/main/kotlin/LicenseTextProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface LicenseTextProvider {
/**
* Return the license text for the license identified by [licenseId] or null if the license text is not available.
*/
fun getLicenseText(licenseId: String): String?
fun getLicenseText(licenseId: String): String? = getLicenseTextReader(licenseId)?.invoke()

/**
* Return a lambda that can read the license text for the license identified by [licenseId] or null if no license
Expand All @@ -37,5 +37,5 @@ interface LicenseTextProvider {
/**
* Return true if a license text for the license identified by [licenseId] is available.
*/
fun hasLicenseText(licenseId: String): Boolean
fun hasLicenseText(licenseId: String): Boolean = getLicenseTextReader(licenseId) != null
}
6 changes: 0 additions & 6 deletions utils/spdx/src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@ fun getLicenseText(
licenseTextDirectories: List<File> = emptyList()
): String? = getLicenseTextReader(id, handleExceptions, addScanCodeLicenseTextsDir(licenseTextDirectories))?.invoke()

fun hasLicenseText(
id: String,
handleExceptions: Boolean = false,
licenseTextDirectories: List<File> = emptyList()
): Boolean = getLicenseTextReader(id, handleExceptions, addScanCodeLicenseTextsDir(licenseTextDirectories)) != null

fun getLicenseTextReader(
id: String,
handleExceptions: Boolean = false,
Expand Down

0 comments on commit ce6839d

Please sign in to comment.