Skip to content

Instantly share code, notes, and snippets.

@winteryoung
Created March 1, 2016 06:58
Show Gist options
  • Save winteryoung/274db2ca1509ee83a500 to your computer and use it in GitHub Desktop.
Save winteryoung/274db2ca1509ee83a500 to your computer and use it in GitHub Desktop.
PackageNamedKeyMap
/**
* 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