/* Copyright 2010 Aaron J. Radke 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 distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package cc.drx /**nice scala based function constructors and flat names to remove import requirements*/ object Java{ // https://twitter.github.io/scala_school/concurrency.html has a nice list of these common java concurrency methods def Runnable(f: => Any):Runnable = new java.lang.Runnable{def run() = {f; ()}} //returns a unit no matter what def Thread(f: => Any):Thread = {val t = new java.lang.Thread{Runnable(f)}; t.start; t} //requires a start call to start the thread running def Throwable(msg:String):Throwable = new java.lang.Throwable(msg) //returns a unit no matter what // def Callable[A](f: => A):Callable[A] = new java.lang.Callable{def run():A = f} //return the type of the function //def TimerTask(f: => Unit) = new java.util.TimerTask{def run() = f} //--dep resolution via a string // private lazy val systemLoader = java.lang.ClassLoader.getSystemClassLoader // systemLoader.loadClass(className) //resolve=true throws the error early private def classByName(className:String) = java.lang.Class.forName(className) def instanceOf[A](className:String):A = classByName(className).getConstructor().newInstance().asInstanceOf[A] def instanceOf[A,B](className:String,arg0:B):A = classByName(className).getConstructor(arg0.getClass).newInstance( arg0.asInstanceOf[java.lang.Object] ).asInstanceOf[A] type JBool = java.lang.Boolean type JChar = java.lang.Character // 16bit unicode type I8 = java.lang.Byte //wraps a `byte` type I16 = java.lang.Short //wraps a `short` type I32 = java.lang.Integer //wraps an `int` type I64 = java.lang.Long //wraps a `long` type F32 = java.lang.Float //wraps a `float` type F64 = java.lang.Double //wraps a `double` //--simple collection conversions def apply[A](xs:Set[A]):java.util.HashSet[A] = { val ys = new java.util.HashSet[A] xs.foreach(ys add _) ys } def toScala[A](javaIt:java.util.Iterator[A]):Iterator[A] = new Iterator[A]{ def next():A = javaIt.next() def hasNext:Boolean = javaIt.hasNext } def toScala[A](javaIt:java.lang.Iterable[A]):Iterable[A] = new Iterable[A]{ def iterator:Iterator[A] = toScala(javaIt.iterator) } def toScala[A,B](javaMap:java.util.Map[A,B]):Map[A,B] = { toScala(javaMap.keySet).map{k => k -> javaMap.get(k) }.toMap } }