Created
March 1, 2016 06:58
-
-
Save winteryoung/274db2ca1509ee83a500 to your computer and use it in GitHub Desktop.
PackageNamedKeyMap
This file contains 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
/** | |
* Package name keyed map finds an entry if it has the given package name registered in it, | |
* or the ancestor package of the given package name registered in it. | |
*/ | |
internal class PackageNamedKeyMap<T> { | |
private val map = TreeMap<String, T>() | |
/** | |
* Returns the value if this map has the given package name registered in it, | |
* or the ancestor package of the given package name registered in it. | |
*/ | |
operator fun get(packageName: String): T? { | |
if (packageName.isEmpty()) { | |
return null | |
} | |
map[packageName]?.let { | |
return it | |
} | |
return get(packageName.substringBeforeLast(".", missingDelimiterValue = "")) | |
} | |
/** | |
* Sets the value associated with the given package name. | |
*/ | |
operator fun set(packageName: String, value: T) { | |
map[packageName] = value | |
} | |
/** | |
* If this contains the given key. | |
*/ | |
fun containsKey(key: String) = map.containsKey(key) | |
/** | |
* Clear this map. | |
*/ | |
fun clear() { | |
map.clear() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment