Last active
May 21, 2017 04:33
-
-
Save AdamMc331/17b1d7a781ed8e1cd96d60242f1aa9b4 to your computer and use it in GitHub Desktop.
Demonstrates how much boilerplate is removed by using Kotlin's `firstOrNull{}` extension.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String getKeyForClass(Class<?> classToCheck) { | |
int index = -1; | |
int internalClassesLength = ManagerRegistry.INTERNAL_CLASSES.size(); | |
// Check if this class has a superclass defined in this project. | |
for (int i = 0; i < internalClassesLength; i++) { | |
if (ManagerRegistry.INTERNAL_CLASSES.get(i).isAssignableFrom(classToCheck)) { | |
index = i; | |
break; | |
} | |
} | |
Class keyClass = index > -1 | |
// Use the class of the internal version | |
? ManagerRegistry.INTERNAL_CLASSES.get(index) | |
// Otherwise use the original class | |
: classToCheck; | |
return keyClass.getCanonicalName(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getKeyForClass(classToCheck: Class<*>): String { | |
val keyClass = RManagerRegistry.INTERNAL_CLASSES.firstOrNull { it.isAssignableFrom(classToCheck) } ?: classToCheck | |
return keyClass.canonicalName | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment