Skip to content

Commit 1f19aee

Browse files
committed
Refactor out Map.safeGet
1 parent a553ece commit 1f19aee

File tree

6 files changed

+25
-7
lines changed

6 files changed

+25
-7
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2019 Björn Kautler
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+
17+
package net.kautler
18+
19+
fun <K, V> Map<K, V>.safeGet(key: K): V = get(key) ?: error("Key '$key' is missing")

buildSrc/src/main/kotlin/net/kautler/codenarc.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ plugins {
2424
val versions: Map<String, String> by project
2525

2626
codenarc {
27-
toolVersion = versions["codenarc"] ?: error("codenarc version is missing")
27+
toolVersion = versions.safeGet("codenarc")
2828
// customized copy from http://codenarc.sourceforge.net/StarterRuleSet-AllRulesByCategory.groovy.txt
2929
// update with new rules when version is updated (previous version: 1.4)
3030
config = resources.text.fromFile("config/codenarc/codenarc.groovy")

buildSrc/src/main/kotlin/net/kautler/pmd.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ plugins {
2323
val versions: Map<String, String> by project
2424

2525
pmd {
26-
toolVersion = versions["pmd"] ?: error("pmd version is missing")
26+
toolVersion = versions.safeGet("pmd")
2727
// necessary due to https://github.com/gradle/gradle/issues/8514
2828
ruleSets.clear()
2929
ruleSetConfig = resources.text.fromFile("config/pmd/pmd.xml")

buildSrc/src/main/kotlin/net/kautler/spotbugs.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ plugins {
3232
val versions: Map<String, String> by project
3333

3434
spotbugs {
35-
toolVersion = versions["spotbugs"] ?: error("spotbugs version is missing")
35+
toolVersion = versions.safeGet("spotbugs")
3636
reportLevel = string(project, "spotbugs.reportLevel", "low").getValue()
3737
@Suppress("UnstableApiUsage")
3838
excludeFilterConfig = resources.text.fromFile("config/spotbugs/spotbugs-exclude.xml")

buildSrc/src/main/kotlin/net/kautler/tests.gradle.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package net.kautler
1818

19-
import info.solidsoft.gradle.pitest.PitestTask
2019
import org.pitest.mutationtest.engine.gregor.config.Mutator
2120
import java.util.concurrent.TimeUnit.SECONDS
2221
import javax.naming.ConfigurationException
@@ -63,7 +62,7 @@ dependencies {
6362
}
6463

6564
jacoco {
66-
toolVersion = versions["jacoco"] ?: error("jacoco version is missing")
65+
toolVersion = versions.safeGet("jacoco")
6766
}
6867

6968
tasks.test {
@@ -118,7 +117,7 @@ tasks.check {
118117
}
119118

120119
pitest {
121-
pitestVersion(versions["pitest"] ?: error("pitest version is missing"))
120+
pitestVersion(versions.safeGet("pitest"))
122121
// work-around for https://github.com/hcoles/pitest/pull/687
123122
testPlugin("spock")
124123
mutators(listOf(

buildSrc/src/main/kotlin/net/kautler/versions.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ configurations.configureEach {
8989
resolutionStrategy {
9090
eachDependency {
9191
if (requested.group == "org.codehaus.groovy") {
92-
useVersion(versions["groovy"] ?: error("groovy version is missing"))
92+
useVersion(versions.safeGet("groovy"))
9393
}
9494
}
9595
}

0 commit comments

Comments
 (0)