alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (IndexModelFactory.scala)

This example Scala source code file (IndexModelFactory.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Scala source code examples by using tags.

All credit for the original source code belongs to scala-lang.org; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Scala tags/keywords

collection, doctemplateentity, index, indexmodelfactory, map, memberentity, sortedset, symbolmap, unit, universe

The IndexModelFactory.scala Scala example source code

/* NSC -- new Scala compiler
 * Copyright 2007-2013 LAMP/EPFL
 * @author  Pedro Furlanetto
 */

package scala
package tools.nsc
package doc
package model

import scala.collection._

object IndexModelFactory {

  def makeIndex(universe: Universe): Index = new Index {

    lazy val firstLetterIndex: Map[Char, SymbolMap] = {

      object result extends mutable.HashMap[Char,SymbolMap] {

        /* symbol name ordering */
        implicit def orderingMap = math.Ordering.String

        def addMember(d: MemberEntity) = {
          val firstLetter = {
            val ch = d.name.head.toLower
            if(ch.isLetterOrDigit) ch else '_'
          }
          val letter = this.get(firstLetter).getOrElse {
            immutable.SortedMap[String, SortedSet[MemberEntity]]()
          }
          val members = letter.get(d.name).getOrElse {
            SortedSet.empty[MemberEntity](Ordering.by { _.toString })
          } + d
          this(firstLetter) = letter + (d.name -> members)
        }
      }

      //@scala.annotation.tailrec // TODO
      def gather(owner: DocTemplateEntity): Unit =
        for(m <- owner.members if m.inDefinitionTemplates.isEmpty || m.inDefinitionTemplates.head == owner)
          m match {
            case tpl: DocTemplateEntity =>
              result.addMember(tpl)
              gather(tpl)
            case non: MemberEntity if !non.isConstructor =>
              result.addMember(non)
            case x @ _ =>
          }

      gather(universe.rootPackage)

      result.toMap
    }
  }
}

Other Scala source code examples

Here is a short list of links related to this Scala IndexModelFactory.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.