Skip to content

Instantly share code, notes, and snippets.

@AdamMc331
Last active May 21, 2017 04:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamMc331/17b1d7a781ed8e1cd96d60242f1aa9b4 to your computer and use it in GitHub Desktop.
Save AdamMc331/17b1d7a781ed8e1cd96d60242f1aa9b4 to your computer and use it in GitHub Desktop.
Demonstrates how much boilerplate is removed by using Kotlin's `firstOrNull{}` extension.
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();
}
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