本文整理汇总了Scala中scala.collection.mutable.ListBuffer类的典型用法代码示例。如果您正苦于以下问题:Scala ListBuffer类的具体用法?Scala ListBuffer怎么用?Scala ListBuffer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ListBuffer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: HDFS
//设置package包名称以及导入依赖的类
package org.mireynol.util
import java.io.{BufferedInputStream, OutputStreamWriter}
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.slf4j.{Logger, LoggerFactory}
import scala.collection.mutable.ListBuffer
import scala.io.Source
object HDFS {
def log : Logger = LoggerFactory.getLogger( HDFS.getClass )
val hadoop : FileSystem = {
val conf = new Configuration( )
conf.set( "fs.defaultFS", "hdfs://localhost:9000" )
FileSystem.get( conf )
}
def readAndMap( path : String, mapper : ( String ) => Unit ) = {
if ( hadoop.exists( new Path( path ) ) ) {
val is = new BufferedInputStream( hadoop.open( new Path( path ) ) )
Source.fromInputStream( is ).getLines( ).foreach( mapper )
}
else {
// TODO - error logic here
}
}
def write( filename : String, content : Iterator[ String ] ) = {
val path = new Path( filename )
val out = new OutputStreamWriter( hadoop.create( path, false ) )
content.foreach( str => out.write( str + "\n" ) )
out.flush( )
out.close( )
}
def ls( path : String ) : List[ String ] = {
val files = hadoop.listFiles( new Path( path ), false )
val filenames = ListBuffer[ String ]( )
while ( files.hasNext ) filenames += files.next( ).getPath( ).toString( )
filenames.toList
}
def rm( path : String, recursive : Boolean ) : Unit = {
if ( hadoop.exists( new Path( path ) ) ) {
println( "deleting file : " + path )
hadoop.delete( new Path( path ), recursive )
}
else {
println( "File/Directory" + path + " does not exist" )
log.warn( "File/Directory" + path + " does not exist" )
}
}
def cat( path : String ) = Source.fromInputStream( hadoop.open( new Path( path ) ) ).getLines( ).foreach( println )
}
示例2: StoryController
//设置package包名称以及导入依赖的类
package controllers
import controllers.StoryModel.StoryData
import javax.inject.Inject
import play.api.Configuration
import play.api.data.Form
import play.api.data.Forms._
import play.api.mvc.{Action, Controller}
import model.StoryDao
import model.DataModel.Story
import scala.collection.mutable.ListBuffer
class StoryController @Inject()(implicit storyDao:StoryDao,config:Configuration) extends Controller{
def index = Action {
val result = storyDao.create();
Ok(views.html.story("Welcome to StoryBoard !!",listStory))
}
def createStory = Action { implicit Request =>
var result = storyDao.insert(new Story(storyForm.bindFromRequest().get.title,storyForm.bindFromRequest().get.description))
Ok(views.html.story("Story Created:" + "(" + storyForm.bindFromRequest().get.title + "," + storyForm.bindFromRequest().get.description + ")",listStory))
}
def listStory:List[StoryData] = {
val stories:List[Story] = storyDao.list
var storiesData = new ListBuffer[StoryData]
for(s <- stories){
storiesData += (new StoryData(s.title,s.description))
}
storiesData.toList
}
val storyForm = Form(
mapping(
"title" -> text,
"description" -> text
)(StoryData.apply)(StoryData.unapply)
)
}
object StoryModel{
case class StoryData(
title:String,
description:String
)
}
示例3: Screen
//设置package包名称以及导入依赖的类
package no.vestein.webapp
import no.vestein.webapp.App.Ctx2D
import org.scalajs.dom
import scala.collection.mutable.ListBuffer
import scala.scalajs.js
class Screen(val width: Int, val height: Int, val canvas: dom.html.Canvas, val game: Game) {
type KeyboardEvent = dom.KeyboardEvent
type FocusEvent = dom.FocusEvent
val updateInterval: Double = 16
val keys: ListBuffer[Int] = new ListBuffer[Int]
val ctx: Ctx2D = canvas.getContext("2d").asInstanceOf[Ctx2D]
var active: Boolean = true
var prevTime: Double = js.Date.now()
canvas.tabIndex = 1
canvas.width = width
canvas.height = height
canvas.onkeydown = (e : KeyboardEvent) => if (!keys.contains(e.keyCode)) keys += e.keyCode
canvas.onkeyup = (e: KeyboardEvent) => keys -= e.keyCode
canvas.onfocus = (e: FocusEvent) => active = true
canvas.onblur = (e: FocusEvent) => active = false
def update(): Unit = {
val now = js.Date.now()
if (active) {
val delta = now - prevTime
if (delta > updateInterval) {
game.update(delta, keys.toSet[Int])
game.render(this)
prevTime = now
}
} else {
prevTime = now
}
}
}
示例4: InputParsers
//设置package包名称以及导入依赖的类
package tul.poiis.decision_tree
import scala.collection.mutable.ListBuffer
object InputParsers {
def parseMovieEntry(csv_entry: Map[String, String]): (Int, Movie) = {
val fieldsList = ListBuffer[MovieField]()
fieldsList += new PopularityField(csv_entry("popularity"))
fieldsList += new BudgetField(csv_entry("budget"))
fieldsList += new VoteAverageField(csv_entry("vote_average"))
fieldsList += new ReleaseYearField(csv_entry("release_date"))
(csv_entry("Id").toInt, new Movie(fieldsList.toList))
}
def readMoviesFile(filepath: String) : Map[Int, Movie] ={
val reader = MyCsvReader.reader(filepath)
val csv_list = reader.allWithHeaders()
reader.close()
csv_list.map { entry =>
val parseResult: (Int, Movie) = parseMovieEntry(entry)
parseResult._1 -> parseResult._2
}(collection.breakOut): Map[Int, Movie]
}
def parseTrainEntry(csv_entry: List[String], moviesMap: Map[Int, Movie]): (Int, Evaluation) ={
val personId = csv_entry(1)
val movieId = csv_entry(2)
val grade = csv_entry(3)
(personId.toInt, new Evaluation(grade = grade.toInt, movie = moviesMap(movieId.toInt)))
}
def readTrainSetFile(filepath: String, moviesMap: Map[Int, Movie]): Map[Int, List[Evaluation]] ={
val reader = MyCsvReader.reader(filepath)
val csv_list: List[List[String]] = reader.all()
reader.close()
val parsedTuples = csv_list.map { entry =>
val parseResult: (Int, Evaluation) = parseTrainEntry(entry, moviesMap)
parseResult._1 -> parseResult._2
}
parsedTuples.groupBy(_._1).mapValues(_.map(_._2))
}
def parseUnknownEntry(csv_entry: List[String]): (Int, Int, Int) ={
val evalId = csv_entry(0)
val personId = csv_entry(1)
val movieId = csv_entry(2)
(evalId.toInt, personId.toInt, movieId.toInt)
}
def readUnknowns(filepath: String): List[(Int, Int, Int)] ={
val reader = MyCsvReader.reader(filepath)
val csv_list: List[List[String]] = reader.all()
reader.close()
csv_list.map { entry =>
parseUnknownEntry(entry)
}
}
}
示例5: ReportServiceActor
//设置package包名称以及导入依赖的类
package com.github.unknownnpc.remotedebugtool.actor
import akka.actor.{Actor, ActorLogging, ActorRef, Props}
import com.github.unknownnpc.remotedebugtool.config.{AppConfig, RemoteDebugToolConfig}
import com.github.unknownnpc.remotedebugtool.domain._
import com.github.unknownnpc.remotedebugtool.exception.ReportException
import com.github.unknownnpc.remotedebugtool.message.{MainAppActorStop, ReportServicePayload, ReportServicePrint}
import scala.collection.mutable.ListBuffer
class ReportServiceActor(mainAppActorRef: ActorRef) extends Actor with ActorLogging {
self: AppConfig =>
val values = ListBuffer.empty[ReportRow]
override def receive = {
case ReportServicePayload(payload) =>
log.debug(s"Print service received incoming payload: [$payload]")
values += reportRowFrom(payload)
case ReportServicePrint =>
log.debug(s"Received print command")
log.info(systemConfig.reportFormatter.format(values.toList))
mainAppActorRef ! MainAppActorStop
}
private def reportRowFrom(payload: BreakpointPayload) = {
val testTarget = findServerById(payload.breakpoint.targetId)
JvmReportRow(testTarget.id,
testTarget.address,
testTarget.port,
payload.breakpoint.line,
payload.breakpoint.className,
payload.breakpointValue
)
}
private def findServerById(id: ID) = {
servers.find(_.id == id).getOrElse(
throw ReportException("Unable to match payload to server instance")
)
}
}
object ReportServiceActor {
def props(mainAppActorRef: ActorRef) =
Props(new ReportServiceActor(mainAppActorRef) with RemoteDebugToolConfig)
}
示例6: ChunkedHermesGameFileEntries
//设置package包名称以及导入依赖的类
package proton.game.hermes
import java.util.UUID
import akka.actor.ActorLogging
import akka.event.LoggingReceive
import akka.persistence.PersistentActor
import scala.collection.mutable.ListBuffer
object ChunkedHermesGameFileEntries {
trait EntriesMessage
case class AppendEntries(entries: Seq[HermesGameFileEntry]) extends EntriesMessage
case class GetEntries() extends EntriesMessage
trait EntriesEvent
case class EntriesAppended(entries: Seq[HermesGameFileEntry]) extends EntriesEvent
trait EntriesResult
case class EntriesAppendedResult(id: UUID, count: Int) extends EntriesResult
case class GetEntriesResult(id: UUID, entries: Seq[HermesGameFileEntry]) extends EntriesResult
val gameFileEntriesRegionName = "hermesGameFileEntries"
}
class ChunkedHermesGameFileEntries(moduleSettings: HermesGameTickerModuleSettings) extends PersistentActor with ActorLogging {
import context._
import ChunkedHermesGameFileEntries._
private val _id: UUID = UUID.fromString(self.path.name)
private val _entries = new ListBuffer[HermesGameFileEntry]()
setReceiveTimeout(moduleSettings.chunkedTimeout)
override def receiveRecover: Receive = {
case event: EntriesEvent => updateState(event)
}
def updateState(e: EntriesEvent) = e match {
case EntriesAppended(entries) => _entries ++= entries
}
override def receiveCommand: Receive = LoggingReceive {
case AppendEntries(entries) =>
if (entries.nonEmpty) {
persist(EntriesAppended(entries))(e => {
updateState(e)
sender ! EntriesAppendedResult(_id, entries.size)
})
} else {
sender ! EntriesAppendedResult(_id, 0)
}
case GetEntries() => sender ! GetEntriesResult(_id, _entries)
}
override def persistenceId: String = "hermes-game-file-entries-" + _id.toString
}
示例7: StatisticsWindow
//设置package包名称以及导入依赖的类
package org.hpi.esb.datavalidator.validation
import org.hpi.esb.datavalidator.data.{SimpleRecord, Statistics}
import scala.collection.mutable.ListBuffer
class StatisticsWindow(firstTimestamp: Long, windowSize: Long) extends Window(firstTimestamp, windowSize) {
var stats = new Statistics()()
override def update(): Unit = {
super.update()
stats = new Statistics()()
}
def addValue(value: Long, timestamp: Long): Unit = {
stats = stats.getUpdatedWithValue(timestamp, value)
}
def takeRecords(records: ListBuffer[SimpleRecord]): ListBuffer[SimpleRecord] = {
val (windowValues, rest) = records.span(r => containsTimestamp(r.timestamp))
windowValues.foreach(r => addValue(r.value, r.timestamp))
rest
}
}
示例8: BagSpecs
//设置package包名称以及导入依赖的类
package de.htwg.se.SevenSteps.model.bag.basicImpl
import org.junit.runner.RunWith
import org.scalatest.Matchers._
import org.scalatest._
import org.scalatest.junit.JUnitRunner
import scala.collection.mutable.ListBuffer
@RunWith(classOf[JUnitRunner])
class BagSpecs extends WordSpec {
def getBag: Bag = Bag(Vector(), Vector("a", "b"))
"A Bag" should {
"have a insert function" in {
val bag = getBag
bag.insert('a')
bag.insert('b')
bag.insert('c')
bag.bag should be(ListBuffer("a", "b", "c"))
}
"have a reset function" in {
val bag = getBag
bag.insert('a')
bag.reset.bag should be(ListBuffer())
}
"have a draw function" in {
val bag = getBag
bag.insert('a')
bag.get() should be(Some('a'))
bag.get() should be(None)
}
"can fillUp with the colors" in {
val bag = getBag
bag.fillup()
}
"can get the stone number" in {
val bag = getBag
bag.getStoneNumber should be(0)
bag.insert('a')
bag.getStoneNumber should be(1)
}
"have a toXML functions" in {
val bag = getBag
bag.insert('a')
bag.insert('b')
bag.insert('c')
bag.toXML().toString() should be("<alles a='<blub bag2=\"a\"></blub><blub bag2=\"b\"></blub><blub" +
" bag2=\"c\"></blub>' b='<bag col=\"a\"></bag><bag col=\"b\"></bag>'></alles>")
}
}
}
示例9: Field
//设置package包名称以及导入依赖的类
package de.htwg.se.scotlandyard.model.impl
import com.google.inject.Singleton
import de.htwg.se.scotlandyard.model.TField
import scala.collection.mutable.ListBuffer
@Singleton
case class Field() extends TField {
override var fieldM = scala.collection.mutable.Map.empty[Int, Node]
var stream = Field.getClass.getResourceAsStream("/Knoten.txt")
for (line <- scala.io.Source.fromInputStream( stream ).getLines) {
var data = line.split(" ")
val pcon : ListBuffer[(Int, String)] = ListBuffer()
fieldM += (data(0).toInt -> Node(data(0).toInt,false,data(1).toInt,data(2).toInt, pcon))
}
stream = Field.getClass.getResourceAsStream("/Kanten.txt")
for (line <- scala.io.Source.fromInputStream( stream ).getLines) {
var data = line.split(" ")
fieldM(data(0).toInt).pcon.+=:(data(1).toInt,data(2))
}
stream = Field.getClass.getResourceAsStream("/Kanten.txt")
for (line <- scala.io.Source.fromInputStream( stream ).getLines) {
var data = line.split(" ")
fieldM(data(1).toInt).pcon.+=:(data(0).toInt,data(2))
}
override def reset = new Field()
override def prettyPconList(fieldNo: Int): String = {
var sb = new StringBuilder
var i = 0
val rfield = fieldM(fieldNo).pcon.toList
sb.append("Possible Moves: \n")
for (a <- 0 until rfield.length) {
sb.append("(" + a + ")" + rfield(a) + "\n")
}
sb.toString()
}
override def toString = {
var a = 199
var sb = new StringBuilder()
for (a <- 1 to 199) {
if (fieldM(a).occ == true) {
sb.append(fieldM(a).no.toString + " " + fieldM(a).occ.toString + "\n")
}
}
sb.toString()
}
}
示例10: restart
//设置package包名称以及导入依赖的类
package de.htwg.se.scotlandyard.controller
import java.util
import de.htwg.se.scotlandyard.model.impl.Player
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.swing.Publisher
trait TScotlandYardController extends Publisher {
var field : de.htwg.se.scotlandyard.model.TField
var players: Array[Player]
var caught : Boolean
var statusText : String
var startPosList : ListBuffer[Int]
var playerlist : Array[Player]
var moveList : util.ArrayList[String]
var moveListPublic : util.ArrayList[String]
var moveStackAll : mutable.Stack[(Int, Int, String, Int)]
var roundCounter : Int
var currentPlayer : Int
var previousPlayer : Int
var chosenCard : String
def restart(): Unit
def move(connectionPos: Int): Unit
def moveGUI(newPosition: Int, usedCard: String, optionsNeeded: Boolean): Unit
def checkOcc(pos: Int): Boolean
def enoughCards(typ: String, player: Int): Boolean
def useCard(typ: String, player: Int): Unit
def startPos(player:Int): Unit
def misterxMoves(): util.ArrayList[String]
def addMisterxMoves(newPos: Int, usedCard: String): Unit
def playerPositions(): String
def playerInfo(player: Int) : String
def undoMove(): Unit
def toXml() : scala.xml.Node
def fromXml (node: scala.xml.Node): String
def saveGame() : Unit
}
示例11: TranscriberDemo
//设置package包名称以及导入依赖的类
package org.rwalk.legalchatter
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import edu.cmu.sphinx.api.Configuration
import edu.cmu.sphinx.api.SpeechResult
import edu.cmu.sphinx.api.StreamSpeechRecognizer
import edu.cmu.sphinx.decoder.adaptation.Transform
import org.rwalk.legalchatter.LegalChatterUtils
import collection.JavaConversions._
import scala.collection.mutable.ListBuffer
object TranscriberDemo {
def main(args:Array[String]) {
println("Performing recognition.")
val recognizer = new StreamSpeechRecognizer(Configurator.sphinxConfig)
recognizer.startRecognition(LegalChatterUtils.decodeMP3Stream(args(0)))
var result = recognizer.getResult
val words = new ListBuffer[String]
while (result != null) {
println(s"Hypothesis: ${result.getHypothesis()}\n")
println("List of recognized words and their times:")
result.getWords() foreach println
println("Best 3 hypothesis:")
result.getNbest(3) foreach println
words.append(result.getHypothesis)
result = recognizer.getResult
}
recognizer.stopRecognition()
println(s"TRANSCRIPTION: \n ${words.mkString(" ")}")
}
}
示例12: ProcessBuilderUtils
//设置package包名称以及导入依赖的类
package util
import java.io.ByteArrayInputStream
import java.nio.charset.{Charset, CodingErrorAction}
import text.StringOption
import scala.collection.mutable.ListBuffer
import scala.io.{Codec, Source}
import scala.sys.process.ProcessBuilder
object ProcessBuilderUtils {
implicit def processToProcessUtils(repr: ProcessBuilder): ProcessBuilderUtils = {
new ProcessBuilderUtils(repr)
}
}
class ProcessBuilderUtils(repr: ProcessBuilder) {
def lineStream(encoding: Charset,
onMalformedInput: CodingErrorAction,
onUnmappableCharacter: CodingErrorAction,
replacementOpt: StringOption): Iterator[String] = {
val lines: Iterator[String] = repr.lineStream_!.iterator
val byteBuffer = ListBuffer.empty[Byte]
while (lines.hasNext) {
val line: String = lines.next.trim concat "\n"
byteBuffer ++= line.getBytes
}
implicit val codec = Codec(encoding).
onMalformedInput(onMalformedInput).
onUnmappableCharacter(onUnmappableCharacter)
if (replacementOpt.nonEmpty) {
codec.decodingReplaceWith(replacementOpt.get)
}
Source.fromInputStream(new ByteArrayInputStream(byteBuffer.toArray)).getLines
}
}
示例13: GpxFileReader
//设置package包名称以及导入依赖的类
package eu.kraml.io
import java.io.File
import java.nio.file.Path
import java.time.Instant
import java.time.format.DateTimeFormatter
import eu.kraml.model.{GpsCoordinate, Record}
import scala.collection.mutable.ListBuffer
import scala.xml.XML
object GpxFileReader {
val formatter = DateTimeFormatter.ISO_INSTANT
def read(gpxFile: Path): Option[List[Record]] = {
read(gpxFile.toFile)
}
def read(gpxFile: File): Option[List[Record]] = {
try {
Some(readInternal(gpxFile))
} catch {
case e:Exception =>
None
}
}
private def readInternal(gpxFile: File): List[Record] = {
val gpx = XML.loadFile(gpxFile)
val segment = gpx \ "trk" \ "trkseg"
val points = segment \ "trkpt"
val records = ListBuffer[Record]()
points.foreach(n => {
val lat = n.attribute("lat").get.head.text
val lon = n.attribute("lon").get.head.text
val timestampText = (n \ "time").text
val timestamp = Instant.parse(timestampText)
records += Record(new GpsCoordinate(lat.toDouble, lon.toDouble), timestamp)
})
records.toList
}
}
示例14: NamedEntityController
//设置package包名称以及导入依赖的类
package controllers
import scala.collection.JavaConverters._
import scala.collection.mutable.ListBuffer
import scala.util.{Failure, Success, Try}
import javax.inject._
import play.api.libs.json.{JsArray, JsValue, Json}
import play.api.mvc._
import IndonesianNLP._
@Singleton
class NamedEntityController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {
val nETagger = new IndonesianNETagger
def tagger: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
val string = (request.body \ "string").as[String]
Try {
nETagger.extractNamedEntity(string)
} match {
case Success(extracted) =>
val namedEntityList = new ListBuffer[JsValue]()
for(tag <- extracted.asScala) {
namedEntityList += Json.toJson(tag)
}
Ok(Json.obj("status" -> "success", "data" -> JsArray(namedEntityList)))
case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
}
}
}
示例15: PhraseController
//设置package包名称以及导入依赖的类
package controllers
import scala.collection.JavaConverters._
import scala.collection.mutable.ListBuffer
import scala.util.{Failure, Success, Try}
import javax.inject._
import play.api.libs.json.{JsArray, JsValue, Json}
import play.api.mvc._
import IndonesianNLP._
@Singleton
class PhraseController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {
val phraseChunker = new IndonesianPhraseChunker
def chunker: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
val string = (request.body \ "string").as[String]
Try {
phraseChunker.doPhraseChunker(string)
} match {
case Success(chunked) =>
val chunkListString = new ListBuffer[String]()
val chunkListJs = new ListBuffer[JsValue]()
for(chunk <- chunked.asScala) {
for(phrase <- chunk) {
chunkListString += phrase
if(chunk.indexOf(phrase) % 2 == 1) {
chunkListJs += Json.toJson(phrase)
}
}
}
val chunkMap = chunkListString.grouped(2).collect { case ListBuffer(k, v) => k -> v }.toMap
Ok(Json.obj("status" -> "success", "data" -> Json.obj("map" -> Json.toJson(chunkMap), "list" -> JsArray(chunkListJs))))
case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
}
}
}