Skip to content

Commit

Permalink
chore: Use ifEmpty and ifBlank to simplify code
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Viernau <frank_viernau@epam.com>
  • Loading branch information
fviernau committed Sep 9, 2024
1 parent 6ecdb9e commit 54a2e4e
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 12 deletions.
3 changes: 1 addition & 2 deletions analyzer/src/main/kotlin/PackageManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,7 @@ abstract class PackageManager(

protected fun requireLockfile(workingDir: File, condition: () -> Boolean) {
require(analyzerConfig.allowDynamicVersions || condition()) {
val relativePathString = workingDir.relativeTo(analysisRoot).invariantSeparatorsPath
.takeUnless { it.isEmpty() } ?: "."
val relativePathString = workingDir.relativeTo(analysisRoot).invariantSeparatorsPath.ifEmpty { "." }

"No lockfile found in '$relativePathString'. This potentially results in unstable versions of " +
"dependencies. To support this, enable the 'allowDynamicVersions' option in '$ORT_CONFIG_FILENAME'."
Expand Down
2 changes: 1 addition & 1 deletion downloader/src/main/kotlin/VcsHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ enum class VcsHost(
* Return all [VcsInfo] that can be parsed from the [vcsUrl] without actually making a network request.
*/
fun parseUrl(vcsUrl: String): VcsInfo {
val projectUrl = vcsUrl.takeUnless { it.isBlank() } ?: return VcsInfo.EMPTY
val projectUrl = vcsUrl.ifBlank { return VcsInfo.EMPTY }
val unknownVcsInfo = VcsInfo.EMPTY.copy(url = projectUrl)
val projectUri = projectUrl.toUri().getOrElse { return unknownVcsInfo }

Expand Down
10 changes: 5 additions & 5 deletions plugins/package-managers/bundler/src/main/kotlin/Bundler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,11 @@ internal data class GemInfo(
return GemInfo(
name,
version,
homepageUrl.takeUnless { it.isEmpty() } ?: other.homepageUrl,
authors.takeUnless { it.isEmpty() } ?: other.authors,
declaredLicenses.takeUnless { it.isEmpty() } ?: other.declaredLicenses,
description.takeUnless { it.isEmpty() } ?: other.description,
runtimeDependencies.takeUnless { it.isEmpty() } ?: other.runtimeDependencies,
homepageUrl.ifEmpty { other.homepageUrl },
authors.ifEmpty { other.authors },
declaredLicenses.ifEmpty { other.declaredLicenses },
description.ifEmpty { other.description },
runtimeDependencies.ifEmpty { other.runtimeDependencies },
vcs.takeUnless { it == VcsInfo.EMPTY } ?: other.vcs,
artifact.takeUnless { it == RemoteArtifact.EMPTY } ?: other.artifact
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ internal fun Collection<NuGetInspector.PackageData>.toOrtPackages(): Set<Package

Package(
id = id,
purl = pkg.purl.takeUnless { it.isEmpty() } ?: id.toPurl(),
purl = pkg.purl.ifEmpty { id.toPurl() },
authors = pkg.parties.toAuthors(),
declaredLicenses = declaredLicenses,
declaredLicensesProcessed = DeclaredLicenseProcessor.process(declaredLicenses),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class SpdxDocumentFile(
* Create a [Package] out of this [SpdxPackage].
*/
private fun SpdxPackage.toPackage(definitionFile: File?, doc: SpdxResolvedDocument): Package {
val packageDescription = description.takeUnless { it.isEmpty() } ?: summary
val packageDescription = description.ifEmpty { summary }

// If the VCS information cannot be determined from the VCS working tree itself, fall back to try getting it
// from the download location.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class CtrlXAutomationReporter : Reporter {
}

// The specification requires at least one license.
val componentLicenses = licenses.takeUnless { it.isNullOrEmpty() } ?: listOf(LICENSE_NOASSERTION)
val componentLicenses = licenses.orEmpty().ifEmpty { listOf(LICENSE_NOASSERTION) }

Component(
name = qualifiedName,
Expand Down
2 changes: 1 addition & 1 deletion utils/ort/src/main/kotlin/OrtProxySelector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class OrtProxySelector(private val fallback: ProxySelector? = null) : ProxySelec

// Quote from the upstream documentation for select: When no proxy is available, the list will contain one
// element of type Proxy that represents a direct connection.
return proxies.takeUnless { it.isEmpty() } ?: fallback?.select(uri) ?: NO_PROXY_LIST
return proxies.ifEmpty { fallback?.select(uri) ?: NO_PROXY_LIST }
}

override fun connectFailed(uri: URI?, sa: SocketAddress?, ioe: IOException?) {
Expand Down

0 comments on commit 54a2e4e

Please sign in to comment.