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

Scala example source code file (ProductCompletion.scala)

This example Scala source code file (ProductCompletion.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

completionaware, completionaware, int, list, none, none, product, productcompletion, productcompletion, seq, seqcompletion, some, string, string

The Scala ProductCompletion.scala source code

/* NSC -- new Scala compiler
 * Copyright 2005-2011 LAMP/EPFL
 * @author Paul Phillips
 */
 
package scala.tools.nsc
package interpreter

class SeqCompletion[T](elems: Seq[T]) extends CompletionAware {  
  lazy val completions = elems.indices.toList map ("(%d)" format _)
  def completions(verbosity: Int) = completions
  private def elemAt(name: String) =
    if (completions contains name) Some(elems(name drop 1 dropRight 1 toInt)) else None
  
  override def execute(name: String) = elemAt(name)
  override def follow(name: String) = elemAt(name) map (x => ProductCompletion(x))
}

/** TODO - deal with non-case products by giving them _1 _2 etc. */
class ProductCompletion(root: Product) extends CompletionAware {
  lazy val caseFields: List[Any] = root.productIterator.toList
  lazy val caseNames: List[String] = ByteCode caseParamNamesForPath root.getClass.getName getOrElse Nil
  private def isValid = caseFields.length == caseNames.length
  
  private def fieldForName(s: String) = (completions indexOf s) match {
    case idx if idx > -1 && isValid => Some(caseFields(idx))
    case _                          => None
  }

  lazy val completions = caseNames
  def completions(verbosity: Int) = completions
  override def execute(name: String) = fieldForName(name)    
  override def follow(name: String) = fieldForName(name) map (x => ProductCompletion(x))
}

object ProductCompletion {
  /** TODO: other traversables. */
  def apply(elem: Any): CompletionAware = elem match {
    case x: Seq[_]    => new SeqCompletion[Any](x)
    case x: Product   => new ProductCompletion(x)
    // case x: Map[_, _] =>
    case _            => CompletionAware.Empty
  }
}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.