PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/com/comcast/xfinity/sirius/util/RichJTreeMap.scala

https://github.com/devakidikshit90/sirius
Scala | 108 lines | 41 code | 8 blank | 59 comment | 2 complexity | 5ad0263708887eb0a69480c17d35b3d3 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2012-2014 Comcast Cable Communications Management, LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.comcast.xfinity.sirius.util
  17. import collection.JavaConversions.asScalaIterator
  18. import java.util.{TreeMap => JTreeMap}
  19. import scala.util.control.Breaks._
  20. object RichJTreeMap {
  21. /**
  22. * Create a RichJTreeMap and populate with provided
  23. * elements
  24. *
  25. * @param kvs varargs of key/value pairs, same as you would
  26. * construct standard Scala Maps
  27. */
  28. def apply[K, V](kvs: (K, V)*): RichJTreeMap[K, V] = {
  29. val map = new RichJTreeMap[K, V]
  30. kvs.foreach(kv => map.put(kv._1, kv._2))
  31. map
  32. }
  33. /**
  34. * Create a RichJTreeMap and populate with elements
  35. * from an existing Scala Map
  36. *
  37. * @param from Map to populate instance from
  38. */
  39. def apply[K, V](from: Map[K, V]): RichJTreeMap[K, V] = apply(from.toSeq: _*)
  40. }
  41. /**
  42. * A Java TreeMap with some functional style helpers
  43. * for mutating the underling collection (contradictory eh?)
  44. *
  45. * The JavaConversions stuff doesn't appear to have anything
  46. * that allows us to mutate the underlying collection
  47. */
  48. class RichJTreeMap[K, V] private extends JTreeMap[K, V] {
  49. /**
  50. * Apply an operation to each element in order
  51. *
  52. * @param fun function to execute on each entry
  53. */
  54. def foreach(fun: (K, V) => Unit) {
  55. asScalaIterator(entrySet.iterator).foreach(
  56. entry => fun(entry.getKey, entry.getValue)
  57. )
  58. }
  59. /**
  60. * Remove all elements from this collection not
  61. * satisfying the predicate function
  62. *
  63. * @param predicate a function to be applied to each
  64. * key value pair in the map. Only elements which
  65. * for which this function evaluates to true are
  66. * retained
  67. */
  68. def filter(predicate: (K, V) => Boolean) {
  69. var toDelete = List[K]()
  70. foreach(
  71. (k, v) =>
  72. if (!predicate(k, v))
  73. toDelete = k :: toDelete
  74. )
  75. toDelete.foreach(remove(_))
  76. }
  77. /**
  78. * Remove elements from the beginning of the collection
  79. * until predicate evaluates to false
  80. *
  81. * @param predicate a function to be applied to each
  82. * key value pair in the map. All elements for
  83. * which this function evaluates true up to
  84. * the first element (not inclusive) for which
  85. * it returns false are removed.
  86. */
  87. def dropWhile(predicate: (K, V) => Boolean) {
  88. var toDelete = List[K]()
  89. breakable {
  90. foreach(
  91. (k, v) =>
  92. if (predicate(k, v))
  93. toDelete = k :: toDelete
  94. else
  95. break()
  96. )
  97. }
  98. toDelete.foreach(remove(_))
  99. }
  100. }