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

Scala example source code file (SimpleHistory.scala)

This example Scala source code file (SimpleHistory.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

buffer, charsequence, charsequence, entry, int, int, jentry, jlistiterator, seq, simplehistory, string, string, unit, unit

The Scala SimpleHistory.scala source code

/* NSC -- new Scala compiler
 * Copyright 2005-2011 LAMP/EPFL
 * @author Paul Phillips
 */

package scala.tools.nsc
package interpreter
package session

import scala.collection.mutable.{ Buffer, ListBuffer }
import scala.collection.JavaConverters._

class SimpleHistory extends JLineHistory {
  private var _index: Int = 0
  private val buf: Buffer[String] = new ListBuffer[String]  
  private def toEntries(): Seq[JEntry] = buf.zipWithIndex map { case (x, i) => Entry(i, x) }
  private def setTo(num: Int)          = { _index = num ; true }
  private def minusOne                 = { _index -= 1 ; true }
  private def plusOne                  = { _index += 1 ; true }
  private def lastIndex                = size - 1
  private def fail(msg: String): String = {
    repldbg("Internal error in history(size %d, index %d): %s".format(
      size, index, msg)
    )
    ""
  }
  
  case class Entry(index: Int, value: CharSequence) extends JEntry {
    override def toString = value
  }
  
  def maxSize: Int = 2500
  def last = if (isEmpty) fail("last") else buf.last
  
  def size = buf.size
  def index = _index
  def isEmpty = buf.isEmpty
  def clear() = buf.clear()
  def get(idx: Int): CharSequence = buf(idx)
  def add(item: CharSequence): Unit = buf += item
  def replace(item: CharSequence): Unit = {
    buf trimEnd 1
    add(item)
  }  
  def entries(idx: Int): JListIterator[JEntry] = toEntries().asJava.listIterator(idx)
  def entries(): JListIterator[JEntry]         = toEntries().asJava.listIterator()
  def iterator: JIterator[JEntry]              = toEntries().iterator.asJava
  
  def current()         = if (index >= 0 && index < buf.size) buf(index) else fail("current()")
  def previous()        = (index > 0) && minusOne
  def next()            = (index <= lastIndex) && plusOne
  def moveToFirst()     = (size > 0) && (index != 0) && setTo(0)
  def moveToLast()      = (size > 0) && (index < lastIndex) && setTo(lastIndex)
  def moveTo(idx: Int)  = (idx > 0) && (idx <= lastIndex) && setTo(idx)
  def moveToEnd(): Unit = setTo(size)

  // scala legacy interface
  def asList: List[JEntry] = toEntries().toList
  def asJavaList           = entries()
  def asStrings            = buf.toList
  def grep(s: String)      = buf.toList filter (_ contains s)
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala SimpleHistory.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.