java.util.function.Consumer Scala Examples

The following examples show how to use java.util.function.Consumer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: DataGroup.scala    From ohara   with Apache License 2.0 5 votes vote down vote up
package oharastream.ohara.shabondi.sink

import java.time.{Duration => JDuration}
import java.util.concurrent.atomic.AtomicBoolean
import java.util.function.Consumer

import oharastream.ohara.common.util.Releasable
import com.typesafe.scalalogging.Logger
import oharastream.ohara.common.setting.{ObjectKey, TopicKey}
import oharastream.ohara.metrics.basic.Counter

private[sink] class DataGroup(
  val name: String,
  objectKey: ObjectKey,
  brokerProps: String,
  topicKeys: Set[TopicKey],
  pollTimeout: JDuration
) extends Releasable {
  private val log = Logger(classOf[RowQueue])

  private val rowCounter: Counter =
    Counter.builder
      .key(objectKey)
      .item(s"rows-$name")
      .unit("row")
      .document(s"The number of received rows of group $name")
      .value(0)
      .register()

  val queue                = new RowQueue
  val queueProducer        = new QueueProducer(name, queue, brokerProps, topicKeys, pollTimeout, rowCounter)
  private[this] val closed = new AtomicBoolean(false)

  def resume(): Unit =
    if (!closed.get) {
      queueProducer.resume()
    }

  def pause(): Unit =
    if (!closed.get) {
      queueProducer.pause()
    }

  def isIdle(idleTime: JDuration): Boolean = queue.isIdle(idleTime)

  override def close(): Unit = {
    if (closed.compareAndSet(false, true)) {
      var exception: Throwable = null
      val addSuppressedException: Consumer[Throwable] = (ex: Throwable) => {
        if (exception == null) exception = ex else exception.addSuppressed(ex)
      }
      Releasable.close(queueProducer, addSuppressedException)
      Releasable.close(rowCounter, addSuppressedException)
      if (exception != null) throw exception
      log.info("Group {} closed.", name)
    }
  }
} 
Example 2
Source File: files.scala    From spatial   with MIT License 5 votes vote down vote up
package utils.io

import java.io._
import java.nio.file._
import java.util.function.Consumer
import java.nio.file.{Files,Paths}

import scala.io.Source

object files {
  def sep: String = java.io.File.separator
  def cwd: String = new java.io.File("").getAbsolutePath
  final val BUFFER_SIZE: Int = 1024 * 4
  final val EOF = -1

  
  def copyResource(src: String, dest: String): Unit = {
    val outFile = new File(dest)
    val outPath = outFile.getParentFile
    outPath.mkdirs()
    val url = getClass.getResource(src)
    val in: InputStream = url.openStream()
    val out: OutputStream = new FileOutputStream(outFile)
    val buffer = new Array[Byte](BUFFER_SIZE)
    var n: Int = 0
    while ({n = in.read(buffer); n != EOF}) {
      out.write(buffer, 0, n)
    }
    out.close()
    in.close()
  }

  def listFiles(dir:String, exts:List[String]=Nil):List[java.io.File] = {
    val d = new java.io.File(dir)
    if (d.exists && d.isDirectory) {
      d.listFiles.filter { file =>
        file.isFile && exts.exists { ext => file.getName.endsWith(ext) }
      }.toList
    } else {
      Nil
    }
  }

  def splitPath(path:String) = {
    val file = new File(path)
    (file.getParent, file.getName)
  }

  def buildPath(parts:String*):String = {
    parts.mkString(sep)
  }

  def dirName(fullPath:String) = fullPath.split(sep).dropRight(1).mkString(sep)

  def createDirectories(dir:String) = {
    val path = Paths.get(dir)
    if (!Files.exists(path)) Files.createDirectories(path)
  }

} 
Example 3
Source File: DynamicConfigurations.scala    From maha   with Apache License 2.0 5 votes vote down vote up
// Copyright 2017, Yahoo Holdings Inc.
// Licensed under the terms of the Apache License 2.0. Please see LICENSE file in project root for terms.

package com.yahoo.maha.service.config.dynamic

import java.util.function.Consumer

import com.netflix.archaius.DefaultPropertyFactory
import com.netflix.archaius.api.{Config, Property}
import com.yahoo.maha.core.bucketing.BucketSelector
import com.yahoo.maha.service.config.JsonMahaServiceConfig._
import com.yahoo.maha.service.{DynamicMahaServiceConfig, MahaServiceConfig}
import grizzled.slf4j.Logging

import scala.collection.mutable

case class DynamicPropertyInfo(propertyKey: String, defaultValue: Object, objects: mutable.Map[String, Object])

class DynamicConfigurations(configurationSource: Config) extends Logging {
  val dynamicPropertyFactory = DefaultPropertyFactory.from(configurationSource)

  val map = new mutable.HashMap[String, Property[Integer]]()

  def addProperty(name: String, defaultValue: Int): Unit = {
    val dynamicProperty = dynamicPropertyFactory.getProperty(name).asInteger(defaultValue)
    info(s"Adding dynamic property: $name defaultValue: $defaultValue")
    map.put(name, dynamicProperty)
  }

  def addCallbacks(dynamicServiceConfig: DynamicMahaServiceConfig, registryName: String): Unit = {
    map.values.foreach(dynamicProperty => {
      info(s"Adding callback for dynamic property: $dynamicProperty")
      dynamicProperty.subscribe(
        new Consumer[Integer]() {
          override def accept(t: Integer): Unit = {
            DynamicWrapper.getCallback(dynamicProperty, dynamicServiceConfig, "er").run()
          }
        })
    })
  }

  def getDynamicConfiguration(configKey: String): Option[Int] = {
    if (map.contains(configKey)) {
      Option(map(configKey).get())
    } else {
      None
    }
  }
}

object DynamicWrapper extends Logging {

  def getCallback(dynamicProperty: Property[_ <: Any], dynamicMahaServiceConfig: DynamicMahaServiceConfig, registryName: String): Runnable = {
    val dynamicRegistryConfig = dynamicMahaServiceConfig.registry.get(registryName).get
    val registryConfig = dynamicMahaServiceConfig.jsonMahaServiceConfig.registryMap(registryName)
    val callbackImpl = new Runnable {
      override def run(): Unit = {
        val dependentObjects = dynamicMahaServiceConfig.dynamicProperties(dynamicProperty.getKey).objects
        for ((name, currentObject) <- dependentObjects) {
          info(s"Updating: $name - $currentObject")
          name match {
            case BUCKETING_CONFIG_MAP =>
              val newBucketingConfig = MahaServiceConfig.initBucketingConfig(
                dynamicMahaServiceConfig.jsonMahaServiceConfig.bucketingConfigMap)(dynamicMahaServiceConfig.context)
              if(newBucketingConfig.isSuccess) {
                val updatedBucketSelector = new BucketSelector(dynamicRegistryConfig.registry,
                  newBucketingConfig.toOption.get.get(registryConfig.bucketConfigName).get)
                dynamicRegistryConfig.updateBucketSelector(updatedBucketSelector)
                info(s"Replaced BucketSelector: ${updatedBucketSelector.bucketingConfig}")
              }
            case _ =>
              throw new UnsupportedOperationException(s"Unknown dynamic object name: $name")
          }
        }
      }
    }
    callbackImpl
  }

} 
Example 4
Source File: GroupByTask.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.dsl.task

import java.time.Instant
import java.util.function.Consumer

import com.gs.collections.impl.map.mutable.UnifiedMap
import org.apache.gearpump.Message
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.streaming.Constants.{GEARPUMP_STREAMING_GROUPBY_FUNCTION, GEARPUMP_STREAMING_OPERATOR}
import org.apache.gearpump.streaming.dsl.window.impl.{TimestampedValue, StreamingOperator}
import org.apache.gearpump.streaming.source.Watermark
import org.apache.gearpump.streaming.task.{Task, TaskContext, TaskUtil}


class GroupByTask[IN, GROUP, OUT](
    groupBy: IN => GROUP,
    taskContext: TaskContext,
    userConfig: UserConfig) extends Task(taskContext, userConfig) {

  def this(context: TaskContext, conf: UserConfig) = {
    this(
      conf.getValue[IN => GROUP](GEARPUMP_STREAMING_GROUPBY_FUNCTION)(context.system).get,
      context, conf
    )
  }

  private val groups: UnifiedMap[GROUP, StreamingOperator[IN, OUT]] =
    new UnifiedMap[GROUP, StreamingOperator[IN, OUT]]

  override def onNext(message: Message): Unit = {
    val input = message.value.asInstanceOf[IN]
    val group = groupBy(input)

    if (!groups.containsKey(group)) {
      val operator = userConfig.getValue[StreamingOperator[IN, OUT]](
        GEARPUMP_STREAMING_OPERATOR)(taskContext.system).get
      operator.setup()
      groups.put(group, operator)
    }

    groups.get(group).foreach(TimestampedValue(message.value.asInstanceOf[IN],
      message.timestamp))
  }

  override def onWatermarkProgress(watermark: Instant): Unit = {
    if (groups.isEmpty && watermark == Watermark.MAX) {
      taskContext.updateWatermark(Watermark.MAX)
    } else {
      groups.values.forEach(new Consumer[StreamingOperator[IN, OUT]] {
        override def accept(operator: StreamingOperator[IN, OUT]): Unit = {
          TaskUtil.trigger(watermark, operator, taskContext)
        }
      })
    }
  }

  override def onStop(): Unit = {
    groups.values.forEach(new Consumer[StreamingOperator[IN, OUT]] {
      override def accept(operator: StreamingOperator[IN, OUT]): Unit = {
        operator.teardown()
      }
    })
  }
} 
Example 5
Source File: OverlappingFieldsCanBeMerged.scala    From sangria   with Apache License 2.0 5 votes vote down vote up
package sangria.validation.rules.experimental

import java.util.function.Consumer

import sangria.annotations.ApiMayChange
import sangria.ast
import sangria.ast.AstVisitorCommand
import sangria.validation._
import sangria.validation.rules.experimental.overlappingfields._


@ApiMayChange
class OverlappingFieldsCanBeMerged extends ValidationRule {

  override def visitor(ctx: ValidationContext): AstValidatingVisitor =
    new AstValidatingVisitor {

      private val selectionBuilder: SelectionBuilder = new SelectionBuilder

      override val onEnter: ValidationVisit = {
        case field: ast.Field =>
          val parentType = ctx.typeInfo.previousParentType
          val outputType = parentType.flatMap(ctx.typeInfo.getFieldDef(_, field)).map(_.fieldType)
          selectionBuilder.enterField(parentType, field, outputType)
          AstVisitorCommand.RightContinue

        case fragment: ast.FragmentDefinition =>
          selectionBuilder.enterFragmentDefinition(fragment.name)
          AstVisitorCommand.RightContinue

        case fragmentSpread: ast.FragmentSpread =>
          selectionBuilder.spreadFragment(fragmentSpread.name)
          AstVisitorCommand.RightContinue

        case container: ast.SelectionContainer =>
          selectionBuilder.enterGenericSelectionContainer()
          AstVisitorCommand.RightContinue
      }

      override def onLeave: ValidationVisit = {
        case _: ast.SelectionContainer =>
          selectionBuilder.leaveSelectionContainer()
          AstVisitorCommand.RightContinue

        case _: ast.Document =>
          val roots = selectionBuilder.build()
          val violationsBuilder = new SelectionConflictViolationsBuilder(ctx.sourceMapper)
          val check = new CachedCheck
          roots.forEach {
            new Consumer[SelectionContainer] {
              override def accept(root: SelectionContainer): Unit = {
                check.checkFieldsInSetCanMerge(root.fieldSet, violationsBuilder)
              }
            }
          }
          val violations = violationsBuilder.result()
          if (violations.isEmpty) {
            AstVisitorCommand.RightContinue
          } else {
            Left(violations)
          }
      }
    }
} 
Example 6
Source File: SelectionConflictViolationsBuilder.scala    From sangria   with Apache License 2.0 5 votes vote down vote up
package sangria.validation.rules.experimental.overlappingfields

import java.util
import java.util.function.Consumer

import sangria.ast
import sangria.parser.SourceMapper

class SelectionConflictViolationsBuilder(sourceMapper: Option[SourceMapper]) {

  private val violations = Vector.newBuilder[SelectionConflictViolation]

  def addConflict(outputName: OutputName, reason: String, fields1: util.ArrayList[SelectionField], fields2: util.ArrayList[SelectionField]): Unit = {
    val locations = List.newBuilder[ast.AstLocation]
    fields1.forEach {
      new Consumer[SelectionField] {
        override def accept(field: SelectionField): Unit = {
          locations ++= field.astField.location
        }
      }
    }
    fields2.forEach {
      new Consumer[SelectionField] {
        override def accept(field: SelectionField): Unit = {
          locations ++= field.astField.location
        }
      }
    }
    violations += SelectionConflictViolation(outputName, reason, sourceMapper, locations.result())
  }

  def result(): Vector[SelectionConflictViolation] = {
    violations.result()
  }
} 
Example 7
Source File: SortedArraySet.scala    From sangria   with Apache License 2.0 5 votes vote down vote up
package sangria.validation.rules.experimental.overlappingfields

import java.util
import java.util.Comparator
import java.util.function.Consumer


class SortedArraySet[T](private val sortedMembers: util.ArrayList[T]) extends java.lang.Iterable[T] {

  //cache the hashCode for faster handling
  override val hashCode: Int = {
    sortedMembers.hashCode()
  }

  //equals and hash code delegate to the members
  override def equals(obj: Any): Boolean = {
    obj match {
      case other: SortedArraySet[_] => eq(other) || (hashCode == other.hashCode && size() == other.size() && sortedMembers == other.sortedMembers)
      case _                        => false
    }
  }

  def isEmpty: Boolean = {
    sortedMembers.isEmpty
  }

  def size(): Int = {
    sortedMembers.size()
  }

  override def iterator(): util.Iterator[T] = {
    sortedMembers.iterator()
  }

  override def forEach(action: Consumer[_ >: T]): Unit = {
    sortedMembers.forEach(action)
  }
}

object SortedArraySet {

  def newBuilder[T: Ordering](sizeHint: Int): Builder[T] = {
    new Builder[T](sizeHint, implicitly[Ordering[T]])
  }

  def newBuilder[T: Ordering](): Builder[T] = {
    new Builder[T](implicitly[Ordering[T]])
  }

  //Beware:
  //The comparator wont be used in the final set for equality or removing duplicates, it's only here for sorting.
  //As such it has to be compatible with the standard equality and hashCode implementations.
  class Builder[T] private(private val members: util.ArrayList[T], private val comparator: Comparator[T]) {

    def this(sizeHint: Int, ordering: Ordering[T]) {
      this(new util.ArrayList[T](sizeHint), ordering)
    }

    def this(ordering: Ordering[T]) {
      this(new util.ArrayList[T](), ordering)
    }


    def add(value: T): this.type = {
      members.add(value)
      this
    }

    def addAll(values: util.Collection[T]): this.type = {
      members.addAll(values)
      this
    }

    def build(): SortedArraySet[T] = {
      sortAndRemoveDuplicates()
      new SortedArraySet(members)
    }

    private def sortAndRemoveDuplicates(): Unit = {
      members.sort(comparator)
      var into = 0
      var from = 0
      while (from < members.size()) {
        val first_from = members.get(from)
        members.set(into, first_from)
        into += 1
        do {
          from += 1
        } while (from < members.size() && members.get(from) == first_from)
      }
      members.subList(into, members.size()).clear()
    }
  }

} 
Example 8
Source File: SelectionBuilder.scala    From sangria   with Apache License 2.0 5 votes vote down vote up
package sangria.validation.rules.experimental.overlappingfields

import java.util
import java.util.function
import java.util.function.Consumer

import sangria.ast
import sangria.schema.{CompositeType, OutputType}


  def build(): util.ArrayList[SelectionContainer] = {
    roots.forEach {
      new Consumer[SelectionContainer] {
        override def accept(root: SelectionContainer): Unit = {
          root.computeEffectiveSelections()
        }
      }
    }
    roots
  }

  private def getOrInitFragment(name: String): SelectionContainer = {
    fragments.computeIfAbsent(name, new function.Function[String, SelectionContainer] {
      override def apply(key: String): SelectionContainer = new SelectionContainer
    })
  }
} 
Example 9
Source File: SortedArraySetSpec.scala    From sangria   with Apache License 2.0 5 votes vote down vote up
package sangria.validation.rules.experimental.overlappingfields

import java.util.function.Consumer

import org.scalatest.funsuite.AnyFunSuite

class SortedArraySetSpec extends AnyFunSuite {

  test("zero elements can be build") {
    val set = SortedArraySet.newBuilder[Int]().build()
    assert(set.isEmpty)
    assert(set.size() === 0)
  }

  test("zero elements equality") {
    val set1 = SortedArraySet.newBuilder[Int]().build()
    val set2 = SortedArraySet.newBuilder[Int]().build()
    assert(set1 === set2)
    assert(set1.hashCode === set2.hashCode)
  }

  test("one element can be build") {
    val set = SortedArraySet.newBuilder[Int]().add(42).build()
    assert(!set.isEmpty)
    assert(set.size() === 1)
  }

  test("one element equality") {
    val set1 = SortedArraySet.newBuilder[Int]().add(42).build()
    val set2 = SortedArraySet.newBuilder[Int]().add(42).build()
    val set3 = SortedArraySet.newBuilder[Int]().add(41).build()
    assert(set1 === set2)
    assert(set1 !== set3)
    assert(set1.hashCode === set2.hashCode)
    assert(set1.hashCode !== set3.hashCode)
  }

  test("many elements removes duplicates and equals") {
    val set1 = SortedArraySet.newBuilder[Int]().add(42).add(7).add(3).add(7).add(42).build()
    val set2 = SortedArraySet.newBuilder[Int]().add(3).add(42).add(3).add(42).add(7).add(3).add(3).build()
    val set3 = SortedArraySet.newBuilder[Int]().add(3).add(42).add(2).add(42).add(7).build()
    assert(set1 === set2)
    assert(set1 !== set3)
    assert(set1.hashCode === set2.hashCode)
    assert(set1.hashCode !== set3.hashCode)
  }

  test("many elements are sorted") {
    val set = SortedArraySet.newBuilder[Int]().add(3).add(42).add(3).add(42).add(7).add(3).add(3).build()
    val list = List.newBuilder[Int]
    set.forEach {
      new Consumer[Int] {
        override def accept(member: Int): Unit = {
          list += member
        }
      }
    }
    assert(list.result() == List(3, 7, 42))
  }
} 
Example 10
Source File: PullExample.scala    From Hands-On-Data-Analysis-with-Scala   with MIT License 5 votes vote down vote up
package handson.example.extract

import java.io.{BufferedReader, InputStreamReader}
import java.util.function.Consumer

import scala.collection.mutable.ListBuffer


class DataConsumer extends Consumer[String] {
  val buf = ListBuffer[String]()
  override def accept(t: String): Unit = {
    buf += t
  }
}
object PullExample {
  def main(args: Array[String]): Unit = {
    val reader = new BufferedReader(
      new InputStreamReader(
        new java.net.URL("https://data.lacity.org/api/views/nxs9-385f/rows.csv?accessType=DOWNLOAD").openStream()
      )
    )
    val dataConsumer = new DataConsumer
    reader.lines().forEach(dataConsumer)
    dataConsumer.buf.toList.take(5).foreach(println)
  }

} 
Example 11
Source File: CsvParserExample.scala    From Hands-On-Data-Analysis-with-Scala   with MIT License 5 votes vote down vote up
package handson.example.csv

import java.io.{BufferedReader, InputStreamReader}
import java.util.function.Consumer

import org.apache.commons.csv.{CSVFormat, CSVPrinter, CSVRecord}

import scala.collection.mutable.ListBuffer


object CsvParserExample {
  def main(args: Array[String]): Unit = {
    val reader = new BufferedReader(
      new InputStreamReader(
        new java.net.URL("https://data.lacity.org/api/views/nxs9-385f/rows.csv?accessType=DOWNLOAD").openStream()
      )
    )
    val csvParser = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(reader)
    val dataConsumer = new DataConsumer
    csvParser.forEach(dataConsumer)
    val allRecords = dataConsumer.buf.toList
    allRecords.take(3).foreach(println)

    val csvPrinter = new CSVPrinter(System.out, CSVFormat.RFC4180.withHeader("fname", "lname", "age"))
    csvPrinter.printRecord("Jon", "Doe", "21")
    csvPrinter.printRecord("James", "Bond", "39")
    csvPrinter.flush()

  }

} 
Example 12
Source File: EventInputImpl.scala    From chatoverflow   with Eclipse Public License 2.0 5 votes vote down vote up
package org.codeoverflow.chatoverflow.requirement.impl

import java.util.function.Consumer

import org.codeoverflow.chatoverflow.api.io.event.Event
import org.codeoverflow.chatoverflow.api.io.input.event.EventInput
import org.codeoverflow.chatoverflow.connector.Connector

import scala.reflect.ClassTag


  override def registerEventHandler[S <: T](eventHandler: Consumer[S], eventClass: Class[S]): Unit = {
    registerEventHandler[S](x => eventHandler.accept(x))(ClassTag(eventClass), identifier)
  }

  override def shutdown(): Boolean = {
    if (sourceConnector.isDefined) {
      val stopped = stop()
      unregisterAllEventListeners
      stopped & sourceConnector.get.shutdown()
    } else {
      logger warn "Source connector not set."
      false
    }
  }
} 
Example 13
Source File: WS.scala    From scala-loci   with Apache License 2.0 5 votes vote down vote up
package loci
package communicator
package ws.javalin

import java.util.function.Consumer

import io.javalin.Javalin
import io.javalin.websocket.{WsContext, WsHandler}

import scala.concurrent.duration._
import scala.util.{Success, Try}

trait WS extends
    Protocol with
    SetupInfo with
    SecurityInfo with
    SymmetryInfo with Bidirectional {
  val path: String
  val host: Option[String]
  val port: Option[Int]
  val context: WsContext


  override def toString = s"WS($path, $host, $port)"
}

object WS extends WSSetupFactory {
  def unapply(ws: WS) = Some((ws.path, ws.host, ws.port))

  case class Properties(
    heartbeatDelay: FiniteDuration = 3.seconds,
    heartbeatTimeout: FiniteDuration = 10.seconds)

  def apply(javalin: Javalin, path: String): Listener[WS] =
    apply(javalin, path, Properties())

  def apply(javalin: Javalin, path: String, properties: Properties): Listener[WS] =
    new Listener[WS] {
      self =>
      protected def startListening(connectionEstablished: Connected[WS]): Try[Listening] = {
        javalin.ws(path, new Consumer[WsHandler] {
          override def accept(ws: WsHandler): Unit =
            WSHandler.handleConnection(ws, path, properties, self, connectionEstablished.fire)
        })
        Success(new Listening {
          def stopListening(): Unit = ()
        })
      }
    }


  def apply(url: String): Connector[WS] = ???
  def apply(url: String, properties: Properties): Connector[WS] = ???

  trait Secure extends WS with communicator.Secure {
    override def toString = s"WS.Secure($path, $host, $port)"
  }

  object Secure {
    def unapply(ws: Secure) = Some((ws.path, ws.host, ws.port))

    def apply(url: String): Connector[WS.Secure] = ???
    def apply(url: String, properties: Properties): Connector[WS.Secure] = ???
  }
} 
Example 14
Source File: StyledVerbatim.scala    From paradox   with Apache License 2.0 5 votes vote down vote up
package com.lightbend.paradox.markdown

import java.util.function.Consumer

import scala.collection.mutable.Buffer
import scala.collection.JavaConverters._
import org.parboiled.common.StringUtils
import org.pegdown.ast.{ VerbatimGroupNode, VerbatimNode }
import org.pegdown.{ Printer, VerbatimSerializer }


object PrettifyVerbatimSerializer extends StyledVerbatimSerializer {
  override def printPreAttributes(printer: Printer, nodeGroup: String, classes: Buffer[String]): Unit = {
    val allClasses = "prettyprint" +: (nodeGroup match {
      case "" => classes
      case g  => ("group-" + g) +: classes
    })
    printClass(printer, allClasses.mkString(" "))
  }

  override def printCodeAttributes(printer: Printer, nodeType: String): Unit = nodeType match {
    case "text" | "nocode" => printClass(printer, "nocode")
    case _                 => printClass(printer, s"language-$nodeType")
  }
}

object RawVerbatimSerializer extends VerbatimSerializer {

  val tag = "raw"

  override def serialize(node: VerbatimNode, printer: Printer): Unit = {
    printer.println()
    printer.print(node.getText)
    printer.printchkln()
  }
} 
Example 15
Source File: Impl.scala    From jsdependencies   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package org.scalajs.jsdependencies.core.json

import org.json.simple.JSONValue

import java.io.{Writer, Reader}
import java.util.function.{BiConsumer, Consumer}

private[json] object Impl {

  type Repr = Object

  def fromString(x: String): Repr = x
  def fromNumber(x: Number): Repr = x
  def fromBoolean(x: Boolean): Repr = java.lang.Boolean.valueOf(x)

  def fromList(x: List[Repr]): Repr = {
    val result = new java.util.LinkedList[Repr]
    x.foreach(result.add(_))
    result
  }

  def fromMap(x: Map[String, Repr]): Repr = {
    val result = new java.util.HashMap[String, Repr]
    for ((key, value) <- x)
      result.put(key, value)
    result
  }

  def toString(x: Repr): String = x.asInstanceOf[String]
  def toNumber(x: Repr): Number = x.asInstanceOf[Number]
  def toBoolean(x: Repr): Boolean =
    x.asInstanceOf[java.lang.Boolean].booleanValue()

  def toList(x: Repr): List[Repr] = {
    val builder = List.newBuilder[Repr]
    x.asInstanceOf[java.util.List[Repr]].forEach(new Consumer[Repr] {
      def accept(elem: Repr): Unit =
        builder += elem
    })
    builder.result()
  }

  def toMap(x: Repr): Map[String, Repr] = {
    val builder = Map.newBuilder[String, Repr]
    x.asInstanceOf[java.util.Map[String, Repr]].forEach(new BiConsumer[String, Repr] {
      def accept(key: String, value: Repr): Unit =
        builder += key -> value
    })
    builder.result()
  }

  def serialize(x: Repr): String =
    JSONValue.toJSONString(x)

  def serialize(x: Repr, writer: Writer): Unit =
    JSONValue.writeJSONString(x, writer)

  def deserialize(str: String): Repr = JSONValue.parseWithException(str)

  def deserialize(reader: Reader): Repr = JSONValue.parseWithException(reader)

} 
Example 16
Source File: IdeaDistInstaller.scala    From sbt-idea-plugin   with Apache License 2.0 5 votes vote down vote up
package org.jetbrains.sbtidea.download.idea


import java.nio.file.attribute.PosixFilePermissions
import java.nio.file.{Files, Path}
import java.util.function.Consumer

import org.jetbrains.sbtidea.download.{BuildInfo, FileDownloader, IdeaUpdater, NioUtils}
import org.jetbrains.sbtidea.{PluginLogger => log}
import org.jetbrains.sbtidea.download.api._
import org.jetbrains.sbtidea.pathToPathExt
import sbt._

class IdeaDistInstaller(buildInfo: BuildInfo) extends Installer[IdeaDist] {

  override def isInstalled(art: IdeaDist)(implicit ctx: InstallContext): Boolean =
    IdeaUpdater.isDumbIdea ||
      (ctx.baseDirectory.toFile.exists() &&
       ctx.baseDirectory.toFile.listFiles().nonEmpty)

  override def downloadAndInstall(art: IdeaDist)(implicit ctx: InstallContext): Unit =
    installDist(FileDownloader(ctx.baseDirectory.getParent).download(art.dlUrl))

  private def tmpDir(implicit ctx: InstallContext) =
    ctx.baseDirectory.getParent.resolve(s"${buildInfo.edition.name}-${buildInfo.buildNumber}-TMP")

  private[idea] def downloadArtifact(art: IdeaDist)(implicit ctx: InstallContext): Path =
    FileDownloader(ctx.baseDirectory.getParent).download(art.dlUrl)

  private[idea] def installDist(artifact: Path)(implicit ctx: InstallContext): Path = {
    import sys.process._
    import org.jetbrains.sbtidea.Keys.IntelliJPlatform.MPS

    log.info(s"Extracting ${buildInfo.edition.name} dist to $tmpDir")

    ctx.baseDirectory.toFile.getParentFile.mkdirs() // ensure "sdk" directory exists
    NioUtils.delete(ctx.baseDirectory)
    NioUtils.delete(tmpDir)
    Files.createDirectories(tmpDir)

    if (artifact.getFileName.toString.endsWith(".zip")) {
      val res = sbt.IO.unzip(artifact.toFile, tmpDir.toFile)
      if (res.isEmpty)
        throw new RuntimeException(s"Failed to unzip ${artifact.toFile} - bad archive")
    } else if (artifact.getFileName.toString.endsWith(".tar.gz")) {
      if (s"tar xfz $artifact -C $tmpDir --strip 1".! != 0) {
        throw new RuntimeException(s"Failed to install ${buildInfo.edition.name} dist: tar command failed")
      }
    } else throw new RuntimeException(s"Unexpected dist archive format(not zip or gzip): $artifact")

    if (ctx.baseDirectory.exists) {
      log.warn("IJ install directory already exists, removing...")
      NioUtils.delete(ctx.baseDirectory)
    }

    buildInfo.edition match {
      case MPS if Files.list(tmpDir).count() == 1 => // MPS may add additional folder level to the artifact
        log.info("MPS detected: applying install dir quirks")
        val actualDir = Files.list(tmpDir).iterator().next()
        Files.move(actualDir, ctx.baseDirectory)
        Files.deleteIfExists(tmpDir)
      case _ =>
        Files.move(tmpDir, ctx.baseDirectory)
    }

    fixAccessRights(ctx.baseDirectory)

    NioUtils.delete(artifact)
    log.info(s"Installed ${buildInfo.edition.name}($buildInfo) to ${ctx.baseDirectory}")
    ctx.baseDirectory
  }

    private def fixAccessRights(ideaDir: Path): Unit = {
      if (!System.getProperty("os.name").startsWith("Windows")) {
        val execPerms = PosixFilePermissions.fromString("rwxrwxr-x")
        val binDir    = ideaDir.resolve("bin")
        try {
          Files
            .walk(binDir)
            .forEach(new Consumer[Path] {
              override def accept(t: Path): Unit =
                Files.setPosixFilePermissions(t, execPerms)
            })
        } catch {
          case e: Exception => log.warn(s"Failed to fix access rights for $binDir: ${e.getMessage}")
        }
      }
    }

}