【发布时间】:2015-09-12 13:44:35
【问题描述】:
在实现 ip-lookup 结构时,我试图在类似 trie 的结构中维护一组键,允许我搜索键的“地板”(即小于或等于的最大键到给定的键)。我决定使用 Apache Collections 4 PatriciaTrie 但遗憾的是,我发现 floorEntry 和相关方法不是 public。我目前的“肮脏”解决方案是通过反射(在 Scala 中)强制它们:
val pt = new PatriciaTrie[String]()
val method = pt.getClass.getSuperclass.getDeclaredMethod("floorEntry", classOf[Object])
method.setAccessible(true)
// and then for retrieving the entry for floor(key)
val entry = method.invoke(pt, key).asInstanceOf[Entry[String, String]]
是否有任何干净的方式来拥有相同的功能?为什么这些方法不公开?
【问题讨论】:
标签: java scala reflection apache-commons-collection patricia-trie