@@ -119,10 +119,12 @@ object GenerateFunctionConverters {
119119 def text = Predef .augmentString(showCode(t).replace(" $" , " " )).lines.toVector
120120 }
121121
122+ /** Blocks of code with priority (lower priority has higher precedence in implicit search). */
122123 case class Prioritized (lines : Vector [String ], priority : Int ) {
123124 def withPriority (i : Int ) = copy(priority = i)
124125 }
125126
127+ /** Contains code for conversion between functions and SAMs (single abstract methods). */
126128 case class SamConversionCode (
127129 base : String ,
128130 wrappedAsScala : Vector [String ],
@@ -140,20 +142,21 @@ object GenerateFunctionConverters {
140142 }
141143 object SamConversionCode {
142144 def apply (scc : SamConversionCode * ): (Vector [String ], Vector [String ], Vector [Vector [String ]]) = {
143- val sccDepthSet = scc.map(_.implicitToJava.priority).toSet
145+ val sccDepthSet = scc.map(_.implicitToJava.priority).to[collection. SortedSet ]
144146 val codes =
145147 {
146- if (sccDepthSet != (0 to sccDepthSet.max).toSet) {
147- val sccDepthMap = sccDepthSet.toList.sorted.zipWithIndex.toMap
148+ val size = sccDepthSet.size
149+ // normalize the elements to a fix [0..size-1] range
150+ if (sccDepthSet.firstKey != 0 || sccDepthSet.lastKey != size - 1 ) {
151+ val sccDepthMap = sccDepthSet.iterator.zipWithIndex.toMap
148152 scc.map(x => x.withPriority(sccDepthMap(x.implicitToJava.priority)))
149153 }
150154 else scc
151155 }.toVector.sortBy(_.base)
152- def priorityName (n : Int , pure : Boolean = false ): String = {
153- val pre =
154- if (pure) s " Priority ${n}FunctionExtensions "
155- else s " trait ${priorityName(n, pure = true )}"
156- if (! pure && n < (sccDepthSet.size- 1 )) s " $pre extends ${priorityName(n+ 1 , pure = true )}" else pre
156+ def priorityName (n : Int ): String = s " Priority ${n}FunctionExtensions "
157+ def priorityHeader (n : Int ): String = {
158+ val pre = s " trait ${priorityName(n)}"
159+ if (n < sccDepthSet.size - 1 ) s " $pre extends ${priorityName(n+ 1 )}" else pre
157160 }
158161 val impls =
159162 " object FunctionWrappers {" +: {
@@ -163,7 +166,7 @@ object GenerateFunctionConverters {
163166 val traits = codes.groupBy(_.implicitToJava.priority).toVector.sortBy(- _._1).map{ case (k,vs) =>
164167 s " import language.implicitConversions " +:
165168 " " +:
166- s " ${priorityName (k)} { " +:
169+ s " ${priorityHeader (k)} { " +:
167170 s " import FunctionWrappers._ " +:
168171 s " " +:
169172 {
0 commit comments