Compare commits

...

2 Commits

Author SHA1 Message Date
Ilya Gorbunov
c5a1d628ea Replace some usages of check to takeIfNot 2017-02-11 23:38:03 +03:00
Ilya Gorbunov
83aec23052 Introduce 'takeIfNot' function which is like 'takeIf' but with the predicate inverted.
#KT-7858
2017-02-11 20:37:32 +03:00
12 changed files with 28 additions and 13 deletions

View File

@@ -57,9 +57,9 @@ object KotlinCompilerClient {
): CompileService? {
val flagFile = System.getProperty(COMPILE_DAEMON_CLIENT_ALIVE_PATH_PROPERTY)
?.let(String::trimQuotes)
?.check { !it.isBlank() }
?.takeIfNot(String::isBlank)
?.let(::File)
?.check(File::exists)
?.takeIf(File::exists)
?: makeAutodeletingFlagFile(baseDir = File(daemonOptions.runFilesPathOrDefault))
return connectToCompileService(compilerId, flagFile, daemonJVMOptions, daemonOptions, reportingTargets, autostart)
}

View File

@@ -412,12 +412,12 @@ class QualifiedExpressionResolver {
val qualifierDescriptor = when (receiver) {
is PackageQualifier -> {
val childPackageFQN = receiver.descriptor.fqName.child(name)
receiver.descriptor.module.getPackage(childPackageFQN).check { !it.isEmpty() } ?:
receiver.descriptor.module.getPackage(childPackageFQN).takeIfNot { it.isEmpty() } ?:
receiver.descriptor.memberScope.getContributedClassifier(name, location)
}
is ClassQualifier -> receiver.staticScope.getContributedClassifier(name, location)
null -> context.scope.findClassifier(name, location) ?:
context.scope.ownerDescriptor.module.getPackage(FqName.ROOT.child(name)).check { !it.isEmpty() }
context.scope.ownerDescriptor.module.getPackage(FqName.ROOT.child(name)).takeIfNot { it.isEmpty() }
is ReceiverValue -> receiver.type.memberScope.memberScopeAsImportingScope().findClassifier(name, location)
else -> null
}

View File

@@ -56,7 +56,7 @@ class DefaultImportProvider(
defaultImports
.filter { it.isAllUnder }
.mapNotNull {
it.fqnPart().check { !it.isSubpackageOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) }
it.fqnPart().takeIfNot { it.isSubpackageOf(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) }
}
val nonKotlinAliasedTypeFqNames =
builtinTypeAliases

View File

@@ -167,7 +167,7 @@ fun getFunctionTypeArgumentProjections(
arguments.addIfNotNull(receiverType?.asTypeProjection())
parameterTypes.mapIndexedTo(arguments) { index, type ->
val name = parameterNames?.get(index)?.check { !it.isSpecial }
val name = parameterNames?.get(index)?.takeIfNot { it.isSpecial }
val typeToUse = if (name != null) {
val annotationClass = builtIns.getBuiltInClassByName(KotlinBuiltIns.FQ_NAMES.parameterName.shortName())
val nameValue = ConstantValueFactory(builtIns).createStringValue(name.asString())

View File

@@ -105,10 +105,10 @@ fun <T : Any> constant(calculator: () -> T): T {
private val constantMap = ConcurrentHashMap<Function0<*>, Any>()
fun String.indexOfOrNull(char: Char, startIndex: Int = 0, ignoreCase: Boolean = false): Int? =
indexOf(char, startIndex, ignoreCase).takeIf { it >= 0 }
indexOf(char, startIndex, ignoreCase).takeIfNot { it < 0 }
fun String.lastIndexOfOrNull(char: Char, startIndex: Int = 0, ignoreCase: Boolean = false): Int? =
lastIndexOf(char, startIndex, ignoreCase).takeIf { it >= 0 }
lastIndexOf(char, startIndex, ignoreCase).takeIfNot { it < 0 }
inline fun <T, R : Any> Iterable<T>.firstNotNullResult(transform: (T) -> R?): R? {
for (element in this) {

View File

@@ -182,7 +182,7 @@ class FuzzyType(
valueTransform = {
val typeProjection = TypeProjectionImpl(Variance.INVARIANT, it.defaultType)
val substitutedProjection = substitutorToKeepCapturedTypes.substitute(typeProjection)
substitutedProjection?.check { !ErrorUtils.containsUninferredParameter(it.type) } ?: typeProjection
substitutedProjection?.takeIfNot { ErrorUtils.containsUninferredParameter(it.type) } ?: typeProjection
})
return TypeConstructorSubstitution.createByConstructorsMap(substitutionMap, approximateCapturedTypes = true).buildSubstitutor()
}

View File

@@ -23,4 +23,6 @@ class C {
// ORDER: nullableX
// ORDER: takeIf
// ORDER: takeIf
// ORDER: takeIfNot
// ORDER: takeIfNot
// ORDER: C

View File

@@ -582,7 +582,7 @@ class ExpectedInfos(
val loopVar = forExpression.loopParameter
val loopVarType = if (loopVar != null && loopVar.typeReference != null)
(bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, loopVar] as VariableDescriptor).type.check { !it.isError }
(bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, loopVar] as VariableDescriptor).type.takeIfNot { it.isError }
else
null

View File

@@ -398,7 +398,7 @@ class KotlinCodeFragmentFactory: CodeFragmentFactory() {
val sb = StringBuilder()
javaFile?.packageName?.check { !it.isBlank() }?.let {
javaFile?.packageName?.takeIfNot { it.isBlank() }?.let {
sb.append("package ").append(it.quoteIfNeeded()).append("\n")
}

View File

@@ -76,7 +76,7 @@ abstract class ChangeCallableReturnTypeFix(
val name = element.name
if (name != null) {
val container = element.resolveToDescriptor().containingDeclaration as? ClassDescriptor
val containerName = container?.name?.check { !it.isSpecial }?.asString()
val containerName = container?.name?.takeIfNot { it.isSpecial }?.asString()
val fullName = if (containerName != null) "'$containerName.$name'" else "'$name'"
if (element is KtParameter) {
return "property $fullName"

View File

@@ -51,7 +51,7 @@ open class ChangeVariableTypeFix(element: KtVariableDeclaration, type: KotlinTyp
val name = element.name
if (name != null) {
val container = element.resolveToDescriptor().containingDeclaration as? ClassDescriptor
val containerName = container?.name?.check { !it.isSpecial }?.asString()
val containerName = container?.name?.takeIfNot { it.isSpecial }?.asString()
return if (containerName != null) "'$containerName.$name'" else "'$name'"
}
else {

View File

@@ -68,6 +68,19 @@ public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
@SinceKotlin("1.1")
public inline fun <T> T.takeIf(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null
/**
* Returns `this` value if it _does not_ satisfy the given [predicate] or `null`, if it does.
*/
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.takeIfNot(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null
/**
* Returns `this` value if it _does not_ satisfy the given [predicate] or `null`, if it does.
*/
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.takeUnless(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null
/**
* Executes the given function [action] specified number of [times].
*