Skip to content
Permalink
Browse files

Improve code readability by consistently using orEmpty() calls

Signed-off-by: Sebastian Schuberth <sebastian.schuberth@here.com>
  • Loading branch information...
sschuberth committed May 3, 2019
1 parent bbde049 commit d58e0d95ddb619c85731bfe9b085a4b3ed11db54
@@ -50,21 +50,17 @@ class DotNetSupport(
private const val PROVIDER_NAME = "nuget"

private fun extractRepositoryType(node: JsonNode) =
node["repository"]?.get("type")?.textValue()
?: ""
node["repository"]?.get("type").textValueOrEmpty()

private fun extractRepositoryUrl(node: JsonNode) =
node["repository"]?.get("url")?.textValue()
?: node["projectURL"]?.textValue()
?: ""
?: node["projectURL"].textValueOrEmpty()

private fun extractRepositoryRevision(node: JsonNode): String =
node["repository"]?.get("commit")?.textValue()
?: ""
node["repository"]?.get("commit").textValueOrEmpty()

private fun extractRepositoryPath(node: JsonNode): String =
node["repository"]?.get("branch")?.textValue()
?: ""
node["repository"]?.get("branch").textValueOrEmpty()

private fun extractVcsInfo(node: JsonNode) =
VcsInfo(
@@ -78,13 +74,13 @@ class DotNetSupport(
Identifier(
type = PROVIDER_NAME,
namespace = "",
name = node["id"]?.textValue() ?: "",
version = node["version"]?.textValue() ?: ""
name = node["id"].textValueOrEmpty(),
version = node["version"].textValueOrEmpty()
)

private fun extractDeclaredLicenses(node: JsonNode) =
sortedSetOf<String>().apply {
val license = node["license"]?.textValue() ?: node["licenseUrl"]?.textValue() ?: ""
val license = node["license"]?.textValue() ?: node["licenseUrl"].textValueOrEmpty()
// Most nuget packages only provide a "licenseUrl" which counts as a declared license.
if (license.isNotEmpty()) {
add(license)
@@ -94,17 +90,17 @@ class DotNetSupport(
private fun extractRemoteArtifact(node: JsonNode, nupkgUrl: String) =
RemoteArtifact(
url = nupkgUrl,
hash = node["packageHash"]?.textValue() ?: "",
hash = node["packageHash"].textValueOrEmpty(),
hashAlgorithm = HashAlgorithm.fromString(
node["packageHashAlgorithm"]?.textValue() ?: ""
node["packageHashAlgorithm"].textValueOrEmpty()
)
)

private fun getCatalogURL(registrationNode: JsonNode): String =
registrationNode["catalogEntry"]?.textValue() ?: ""
registrationNode["catalogEntry"].textValueOrEmpty()

private fun getNuspecURL(registrationNode: JsonNode): String =
registrationNode["packageContent"]?.textValue() ?: ""
registrationNode["packageContent"].textValueOrEmpty()

private fun extractVersion(range: String): String {
if (range.isEmpty()) return ""
@@ -180,8 +176,8 @@ class DotNetSupport(
return Package(
id = extractPackageId(jsonCatalogNode),
declaredLicenses = extractDeclaredLicenses(jsonCatalogNode),
description = jsonCatalogNode["description"]?.textValue() ?: "",
homepageUrl = jsonCatalogNode["projectUrl"]?.textValue() ?: "",
description = jsonCatalogNode["description"].textValueOrEmpty(),
homepageUrl = jsonCatalogNode["projectUrl"].textValueOrEmpty(),
binaryArtifact = extractRemoteArtifact(jsonCatalogNode, packageContent.first),
sourceArtifact = RemoteArtifact.EMPTY,
vcs = vcsInfo,
@@ -209,9 +205,9 @@ class DotNetSupport(
name = if (packageID == packageJsonNode["id"]?.textValue()) {
packageID
} else {
packageJsonNode["id"]?.textValue() ?: ""
packageJsonNode["id"].textValueOrEmpty()
},
version = packageJsonNode["version"]?.textValue() ?: ""
version = packageJsonNode["version"].textValueOrEmpty()
)
)

@@ -271,7 +267,7 @@ class DotNetSupport(
}

val node = jacksonObjectMapper().readTree(
registrationInfo ?: ""
registrationInfo.orEmpty()
)

return if (node != null) {
@@ -284,8 +280,7 @@ class DotNetSupport(
private fun getIdUrl(packageID: String, version: String) =
getCreateSearchRestAPIURL(packageID).let { node ->
getRightVersionUrl(node["data"]?.elements(), packageID, version)
?: getFirstMatchingIdUrl(node["data"]?.elements(), packageID)
?: ""
?: getFirstMatchingIdUrl(node["data"]?.elements(), packageID).orEmpty()
}

private fun getRightVersionUrl(
@@ -124,8 +124,8 @@ class MavenSupport(workspaceReader: WorkspaceReader) {
}.toSortedSet()

private fun parseScm(scm: Scm?): VcsInfo {
val connection = scm?.connection ?: ""
val tag = scm?.tag?.takeIf { it != "HEAD" } ?: ""
val connection = scm?.connection.orEmpty()
val tag = scm?.tag?.takeIf { it != "HEAD" }.orEmpty()

if (connection.isEmpty()) return VcsInfo.EMPTY

@@ -512,7 +512,7 @@ class MavenSupport(workspaceReader: WorkspaceReader) {
}
}

val homepageUrl = mavenProject.url ?: ""
val homepageUrl = mavenProject.url.orEmpty()

val vcsProcessed = localDirectory?.let {
PackageManager.processProjectVcs(it, vcsFromPackage, homepageUrl)
@@ -526,7 +526,7 @@ class MavenSupport(workspaceReader: WorkspaceReader) {
version = mavenProject.version
),
declaredLicenses = parseLicenses(mavenProject),
description = mavenProject.description ?: "",
description = mavenProject.description.orEmpty(),
homepageUrl = homepageUrl,
binaryArtifact = binaryRemoteArtifact,
sourceArtifact = sourceRemoteArtifact,
@@ -148,7 +148,7 @@ abstract class PackageManager(
it.isNotEmpty()
} ?: normalizeVcsUrl(homepageUrl).takeIf {
VersionControlSystem.forUrl(it) != null
} ?: ""
}.orEmpty()

val vcsFromUrl = VersionControlSystem.splitUrl(normalizedUrl)

@@ -75,13 +75,11 @@ class Bower(
)

private fun extractRepositoryType(node: JsonNode) =
node["pkgMeta"]["repository"]?.get("type")?.textValue()
?: ""
node["pkgMeta"]["repository"]?.get("type").textValueOrEmpty()

private fun extractRepositoryUrl(node: JsonNode) =
node["pkgMeta"]["repository"]?.get("url")?.textValue()
?: node["pkgMeta"]["_source"]?.textValue()
?: ""
?: node["pkgMeta"]["_source"].textValueOrEmpty()

private fun extractRevision(node: JsonNode): String =
node["pkgMeta"]["_resolution"]?.get("commit")?.textValue()
@@ -165,8 +165,8 @@ class Maven(
definitionFilePath = VersionControlSystem.getPathInfo(definitionFile).path,
declaredLicenses = MavenSupport.parseLicenses(mavenProject),
vcs = vcsFromPackage,
vcsProcessed = processProjectVcs(projectDir, vcsFromPackage, mavenProject.url ?: ""),
homepageUrl = mavenProject.url ?: "",
vcsProcessed = processProjectVcs(projectDir, vcsFromPackage, mavenProject.url.orEmpty()),
homepageUrl = mavenProject.url.orEmpty(),
scopes = scopes.values.toSortedSet()
)

@@ -125,7 +125,7 @@ object PythonVersion : CommandLineTool {
}
}
} else {
getPathFromEnvironment("python$version")?.path ?: ""
getPathFromEnvironment("python$version")?.path.orEmpty()
}
}
}
@@ -181,7 +181,7 @@ class Stack(
type = if (childName == "rts") "GHC" else "Hackage",
namespace = "",
name = childName,
version = versionMap[childName] ?: ""
version = versionMap[childName].orEmpty()
),
declaredLicenses = sortedSetOf(),
description = "",
@@ -326,9 +326,9 @@ class Stack(

val id = Identifier(
type = "Hackage",
namespace = map["category"] ?: "",
name = map["name"] ?: "",
version = map["version"] ?: ""
namespace = map["category"].orEmpty(),
name = map["name"].orEmpty(),
version = map["version"].orEmpty()
)

val artifact = RemoteArtifact(
@@ -338,17 +338,17 @@ class Stack(
)

val vcs = VcsInfo(
type = map["source-repository-this-type"] ?: map["source-repository-head-type"] ?: "",
revision = map["source-repository-this-tag"] ?: "",
url = map["source-repository-this-location"] ?: map["source-repository-head-location"] ?: ""
type = map["source-repository-this-type"] ?: map["source-repository-head-type"].orEmpty(),
revision = map["source-repository-this-tag"].orEmpty(),
url = map["source-repository-this-location"] ?: map["source-repository-head-location"].orEmpty()
)

val homepageUrl = map["homepage"] ?: ""
val homepageUrl = map["homepage"].orEmpty()

return Package(
id = id,
declaredLicenses = map["license"]?.let { sortedSetOf(it) } ?: sortedSetOf(),
description = map["description"] ?: "",
description = map["description"].orEmpty(),
homepageUrl = homepageUrl,
binaryArtifact = RemoteArtifact.EMPTY,
sourceArtifact = artifact,
@@ -116,7 +116,7 @@ object Main : CommandWithHelp() {
val env = Environment()
val variables = env.variables.entries.map { (key, value) -> "$key = $value" }

val command = commandName?.let { " '$commandName'" } ?: ""
val command = commandName?.let { " '$commandName'" }.orEmpty()
val with = if (variables.isNotEmpty()) "with" else ""

var variableIndex = 0
@@ -172,7 +172,7 @@ abstract class VersionControlSystem {
}
}

val type = forUrl(url)?.type ?: ""
val type = forUrl(url)?.type.orEmpty()
if (type == "Git") {
url += ".git"
}
@@ -132,9 +132,9 @@ class Subversion : VersionControlSystem(), CommandLineTool {

override fun isShallow() = false

override fun getRemoteUrl() = runSvnInfoCommand()?.url ?: ""
override fun getRemoteUrl() = runSvnInfoCommand()?.url.orEmpty()

override fun getRevision() = runSvnInfoCommand()?.commit?.revision ?: ""
override fun getRevision() = runSvnInfoCommand()?.commit?.revision.orEmpty()

override fun getRootPath() =
runSvnInfoCommand()?.workingCopy?.absolutePath?.let { File(it) } ?: workingDir
@@ -143,7 +143,7 @@ class Subversion : VersionControlSystem(), CommandLineTool {
val remoteUrl = getRemoteUrl()

val projectRoot = if (directoryNamespaces.any { "/$it/" in remoteUrl }) {
runSvnInfoCommand()?.repository?.root ?: ""
runSvnInfoCommand()?.repository?.root.orEmpty()
} else {
remoteUrl
}
@@ -400,7 +400,7 @@ class ScanCode(name: String, config: ScannerConfiguration) : LocalScanner(name,
}
} ?: return ""

return rootLicenseFile["licenses"].singleOrNull()?.let { getLicenseId(it) } ?: ""
return rootLicenseFile["licenses"].singleOrNull()?.let { getLicenseId(it) }.orEmpty()
}

/**
@@ -83,7 +83,7 @@ interface CommandLineTool {
it.isNotBlank()
}

return versionString ?: ""
return versionString.orEmpty()
}

/**
@@ -210,7 +210,7 @@ fun JsonNode?.fieldsOrEmpty(): Iterator<Map.Entry<String, JsonNode>> = this?.fie
* Convenience function for [JsonNode] that returns an empty string if [JsonNode.textValue] is called on a null object,
* or the text value is null.
*/
fun JsonNode?.textValueOrEmpty(): String = this?.textValue()?.let { it } ?: ""
fun JsonNode?.textValueOrEmpty(): String = this?.textValue()?.let { it }.orEmpty()

/**
* Merge two maps by iterating over the combined key set of both maps and applying [operation] to the entries for the
@@ -23,7 +23,7 @@ package com.here.ort.utils
* Operating-System-specific utility functions.
*/
object Os {
val name = System.getProperty("os.name") ?: ""
val name = System.getProperty("os.name").orEmpty()
private val nameLowerCase = name.toLowerCase()

val isLinux = "linux" in nameLowerCase
@@ -137,7 +137,7 @@ fun filterVersionNames(version: String, names: List<String>, project: String? =

return filteredNames.filter {
// startsWith("") returns "true" for any string, so we get an unfiltered list if "project" is "null".
it.startsWith(project ?: "")
it.startsWith(project.orEmpty())
}.let {
// Fall back to the original list if filtering by project results in an empty list.
if (it.isEmpty()) filteredNames else it

0 comments on commit d58e0d9

Please sign in to comment.
You can’t perform that action at this time.