Skip to content

Instantly share code, notes, and snippets.

View viktorklang's full-sized avatar

Viktor Klang (√) viktorklang

View GitHub Profile
package akka.util
import java.util.concurrent.locks.ReentrantLock
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.{ TimeUnit, BlockingQueue }
import java.util.{ AbstractQueue, Queue, Collection, Iterator }
class BoundedBlockingQueue[E <: AnyRef](val maxCapacity: Int, private val backing: Queue[E]) extends AbstractQueue[E] with BlockingQueue[E] {
backing match {
@viktorklang
viktorklang / ScalaEnum.scala
Created June 30, 2011 23:12
DIY Scala Enums (with optional exhaustiveness checking)
trait Enum { //DIY enum type
import java.util.concurrent.atomic.AtomicReference //Concurrency paranoia
type EnumVal <: Value //This is a type that needs to be found in the implementing class
private val _values = new AtomicReference(Vector[EnumVal]()) //Stores our enum values
//Adds an EnumVal to our storage, uses CCAS to make sure it's thread safe, returns the ordinal
private final def addEnumVal(newVal: EnumVal): Int = { import _values.{get, compareAndSet => CAS}
val oldVec = get
/**
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.zeromq
import akka.actor.{Actor, ActorRef}
import akka.dispatch.MessageDispatcher
import akka.util.Duration
import akka.zeromq.SocketType._
import java.util.concurrent.atomic.AtomicReference
@viktorklang
viktorklang / serialize_Dyn_proxy.scala
Created November 23, 2011 14:34
Serialization of dynamic Proxies
//An interface we'll secretly inject into the mix, in order to get Java Serialization to think we've implemented writeReplace
trait Replaceable {
@throws(classOf[java.io.ObjectStreamException])
def writeReplace(): AnyRef
}
//An external representation of our Proxy
case class SerializedProxy(...) { //Whatever you need to store away in order to rematerialize
@throws(classOf[java.io.ObjectStreamException])
def readResolve(): AnyRef = {
@viktorklang
viktorklang / na.scala
Created December 21, 2011 13:13
Fast, faster, Scala
object Scala extends App {
import scala.collection.mutable.ArrayBuffer
import scala.annotation.tailrec
val items = List("foo", 23, true)
val before = System.currentTimeMillis()
@tailrec def adds(n: Int, absoluteResult: ArrayBuffer[Any] = ArrayBuffer()): ArrayBuffer[Any] = {
def foo(obj : Any) = obj match {
case _:String => "String"
case _:Boolean => "Boolean"
case _:Integer => "Integer"
@viktorklang
viktorklang / minscalaactors.scala
Last active March 25, 2024 19:01
Minimalist Scala Actors
/*
Copyright 2012-2021 Viktor Klang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
@viktorklang
viktorklang / swingactors.scala
Created April 19, 2012 17:25
Swing Actors using Akka
// ©2012 Viktor Klang
package akka.klang
import akka.dispatch.{ DispatcherPrerequisites, ExecutorServiceFactory, ExecutorServiceConfigurator }
import com.typesafe.config.Config
import java.util.concurrent.{ ExecutorService, AbstractExecutorService, ThreadFactory, TimeUnit }
import java.util.Collections
import javax.swing.SwingUtilities
@viktorklang
viktorklang / swingfutures.scala
Created April 19, 2012 17:58
Akka Futures in the Swing Event Dispatch Thread
// © 2012 Viktor Klang
import akka.dispatch.ExecutionContext
import javax.swing.SwingUtilities
import java.util.concurrent.Executor
//
object SwingExecutionContext {
implicit val swingExecutionContext: ExecutionContext = ExecutionContext.fromExecutor(new Executor {
def execute(command: Runnable): Unit = SwingUtilities invokeLater command
@viktorklang
viktorklang / Actor.java
Last active February 13, 2023 12:13
Minimalist Java Actors
/*
Copyright 2012-2021 Viktor Klang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
@viktorklang
viktorklang / StripedExecutor.scala
Created May 12, 2012 09:54
A StripedExecutor in Scala using only JDK primitives, to serialize execution per stripe
// ©2012 Viktor Klang
// 4,192 bytes jarred
package java.klang
import java.util.concurrent. { Executor, ConcurrentHashMap, ConcurrentLinkedQueue }
import java.util.concurrent.atomic. { AtomicBoolean }
import scala.annotation.tailrec
trait Striped {