All Downloads are FREE. Search and download functionalities are using the official Maven repository.

.13.e3.source-code.predef.scala Maven / Gradle / Ivy

/*
   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.predef   //note this package and scope will not include the predef from the cc.drx package object

import cc.drx

trait Constants{
  //--constants
  //note having multiple version provides type check to prevent use of this variable names
  val E = math.E
  val Pi = math.Pi
  val pi = Pi
  val π = Pi
  val Tau = 2.0*math.Pi
  val tau = Tau
  val τ = Tau
 
  val deg2rad = pi/180.0
  val rad2deg = 180.0/pi
}

trait Style{
   //-- TODO some Style attributes should be top level
   //-- popular colors should have top level values without an import 
   val Red             = drx.Color.Red
   val Blue            = drx.Color.Blue
   val Green           = drx.Color.Green
   val Purple          = drx.Color.Purple
   val Orange          = drx.Color.Orange
   val Yellow          = drx.Color.Yellow
   val Brown           = drx.Color.Brown
   val Pink            = drx.Color.Pink
   val Grey            = drx.Color.Grey

   val Black           = drx.Color.Black
   val Charcoal        = drx.Color.Charcoal
   val White           = drx.Color.White

   val Cyan            = drx.Color.Cyan
   val Magenta         = drx.Color.Magenta

   val Transparent     = drx.Color.Transparent
}

trait FlatScala{
  //--flatten the following frequently used types to
  //  1) prevent constantly require imports
  //  2) easier to remember
  //  3) prevent confusion with very common types if you make your own type it better have a good reason
  //  4) follow the drx flat type structure
  //--type aliase's
  type Future[A]        = scala.concurrent.Future[A]
  type Try[A]           = scala.util.Try[A]
  type Success[A]       = scala.util.Success[A]
  type Failure[A]       = scala.util.Failure[A]

  //--reserved words, these are words that have such as special meaning that they should cause errors if someone uses them
  type Blob             = Array[Byte]

  @scala.annotation.implicitNotFound("implicit value not found; try using `import Implicit.sc` (drx alias for java.util.concurrent.ScheduledThreadPoolExecutor)")
  type ScheduledContext = java.util.concurrent.ScheduledThreadPoolExecutor
  @scala.annotation.implicitNotFound("implict context not found; try using `import Implicit.ec` (drx alias for scala global)")
  type ExecutionContext = scala.concurrent.ExecutionContext //flatten the structure
  @scala.annotation.implicitNotFound("implict context not found; try using `import Implicit.ec` (drx alias for scala global)")
  type EC               = scala.concurrent.ExecutionContext //since this is used so often in implicit use this alias

  //-- common scala collection imports that could be part of the prelude
  type ArrayBuffer[A]   = scala.collection.mutable.ArrayBuffer[A]
  type BitSet           = scala.collection.immutable.BitSet  //integer bit set
  // type ByteBuffer       = java.nio.ByteBuffer //TODO think about using this alias since it is used so often
  // type Ordering[A]      = scala.math.Ordering[A]
  // type EC               = scala.concurrent.ExecutionContext  //this shortcut is a little messy
  //TODO add ...
  // type Ordering      = scala.math.Ordering
  type Regex            = scala.util.matching.Regex
  //--object val's for constructors
  val Future            = scala.concurrent.Future
  val Try               = scala.util.Try
  val Success           = scala.util.Success
  val Failure           = scala.util.Failure
  val ExecutionContext  = scala.concurrent.ExecutionContext
  val Regex             = scala.util.matching.Regex
  val ArrayBuffer       = scala.collection.mutable.ArrayBuffer
  val BitSet            = scala.collection.immutable.BitSet
  //--TrieMap use for jvm, note JS fails when using TrieMap
  type TrieMap[A,B]     = scala.collection.concurrent.TrieMap[A,B]
  val TrieMap           = scala.collection.concurrent.TrieMap
  // type TrieMap[A,B]     = scala.collection.mutable.Map[A,B]
  // val TrieMap           = scala.collection.mutable.Map

  // val Ordering          = scala.math.Ordering  //TODO
  //--commonly used annotations
  //TODO add ...
  // final class tailrec extends scala.annotation.StaticAnnotation
  type tailrec = scala.annotation.tailrec
}

trait Conversions{
   import scala.language.implicitConversions

   //--Implicit conversion to java types
   implicit def DrxFileToJavaFile(file:drx.File):java.io.File = file.file  //if someone doesn't know the drx.File hides the java.io.File they can still appear to have a java.io.File
   implicit def DrxURLToJava(url:drx.URL):java.net.URL = url.url //if someone doesn't know the drx.File hides the java.io.File they can still appear to have a java.io.File

   //--auto expand up to Byte
   implicit def DrxU4ToByte(u:drx.U4):Byte = u.value
   // implicit def DrxU4ToU8(u:U4):U8 = u.value
   // implicit def DrxU8ToByte(u:U8):Byte = u.value

   //--auto expand up to Short
   implicit def DrxU4ToShort(u:drx.U4):Short = u.toShort
   implicit def DrxU8ToShort(u:drx.U8):Short = u.toShort
   // implicit def DrxU16ToShort(u:U16):Short = u.value

   //--auto expand up to Int
   implicit def DrxU4ToInt(u:drx.U4):Int = u.toInt
   implicit def DrxU8ToInt(u:drx.U8):Int = u.toInt
   implicit def DrxU16ToInt(u:drx.U16):Int = u.toInt
   // implicit def DrxU32ToInt(u:U32):Int = u.value

   //--auto expand up to Long
   implicit def DrxU4ToLong(u:drx.U4):Long = u.toLong
   implicit def DrxU8ToLong(u:drx.U8):Long = u.toLong
   implicit def DrxU16ToLong(u:drx.U16):Long = u.toLong
   implicit def DrxU32ToLong(u:drx.U32):Long = u.toLong

   /* don't use this because it is too magical and breaks down typesafe checks
   //--auto expand a into a Complex if necessary
   implicit def IntToComplex(r:Int):Complex = Complex(r,0)
   implicit def LongToComplex(r:Long):Complex = Complex(r,0)
   implicit def DoubleToComplex(r:Double):Complex = Complex(r,0)
   */
   
   // implicit def DrxU64ToLong(u:U64):Long = u.toLong
}

trait Enrich{
   import scala.language.implicitConversions

   @inline implicit final def DrxU8(p:Byte):drx.U8    = new drx.U8(p)
   @inline implicit final def DrxU16(p:Short):drx.U16 = new drx.U16(p)
   @inline implicit final def DrxU32(p:Int):drx.U32   = new drx.U32(p)
   @inline implicit final def DrxU64(p:Long):drx.U64  = new drx.U64(p)

   //--from CrossVersion support core/src/main/scala-${scalaVersion}/CrossVersion.scala
   @inline implicit final def richDrxProcessBuilder(p:scala.sys.process.ProcessBuilder):drx.DrxProcessBuilder = new drx.DrxProcessBuilder(p)

   //## string contexts
   //--from core/format.scala
   @inline implicit final def DrxFormatStringContext(sc:StringContext):drx.Format.FormatStringContext = new drx.Format.FormatStringContext(sc)
   //--from core/parse.scala
   @inline implicit final def DrxParseStringContext(sc:StringContext):drx.Parse.ParseStringContext = new drx.Parse.ParseStringContext(sc)
   //--from core/url.scala
   @inline implicit final def DrxURLStringContext(sc:StringContext):drx.URL.URLStringContext = new drx.URL.URLStringContext(sc)
   @inline implicit final def MimeStringContext(sc:StringContext):drx.Mime.MimeStringContext = new drx.Mime.MimeStringContext(sc)
   //--from core/file.scala
   @inline implicit final def DrxFileStringContext(sc:StringContext):drx.File.FileStringContext = new drx.File.FileStringContext(sc)
   //--from core/glob.scala
   @inline implicit final def DrxGlobStringContext(sc:StringContext):drx.Glob.GlobStringContext = new drx.Glob.GlobStringContext(sc)
   //--from core/string.scala
   @inline implicit final def DrxSymbolStringContext(sc:StringContext):drx.SymbolStringContext = new drx.SymbolStringContext(sc)

   //--from core/bound.scala
   @inline implicit final def DrxBoundOps[A:drx.Bound.Boundable](a:A):drx.Bound.BoundOps[A] = new drx.Bound.BoundOps(a)

   //--from core/numeric.scala
   @inline implicit final def richDrxLong(v:Long)    :drx.DrxLong   = new drx.DrxLong(v)
   @inline implicit final def richDrxInt(v:Int)      :drx.DrxInt    = new drx.DrxInt(v)
   @inline implicit final def richDrxShort(v:Short)  :drx.DrxShort  = new drx.DrxShort(v)
   @inline implicit final def richDrxByte(v:Byte)    :drx.DrxByte   = new drx.DrxByte(v)
   @inline implicit final def richDrxFloat(v:Float)  :drx.DrxFloat  = new drx.DrxFloat(v)
   @inline implicit final def richDrxDouble(v:Double):drx.DrxDouble = new drx.DrxDouble(v)
   @inline implicit final def richDrxChar(v:Char)    :drx.DrxChar   = new drx.DrxChar(v)

   //--from core/boolean.scala
   @inline implicit final def richDrxBoolean(v:Boolean):drx.DrxBoolean  = new drx.DrxBoolean(v)

   //--from core/string.scala
   @inline implicit final def richDrxRegex(v:scala.util.matching.Regex):drx.DrxRegex  = new drx.DrxRegex(v)
   @inline implicit final def richDrxString(v:String):drx.DrxString  = new drx.DrxString(v)

   //--from core/collection.scala
   @inline implicit final def richDrxByteBuffer(v:java.nio.ByteBuffer):drx.DrxByteBuffer  = new drx.DrxByteBuffer(v)
   @inline implicit final def richDrxArrayByte(v:Array[Byte]):drx.DrxArrayByte = new drx.DrxArrayByte(v)
   @inline implicit final def richDrxArrayArrayByte(v:Array[Array[Byte]]):drx.DrxArrayArrayByte = new drx.DrxArrayArrayByte(v) //for byte packing
   @inline implicit final def richDrxTraversabelArrayByte(v:Iterable[Array[Byte]]):drx.DrxTraversableArrayByte = new drx.DrxTraversableArrayByte(v) //for byte packing
   @inline implicit final def richDrxArrayInt(v:Array[Int]):drx.DrxArrayInt = new drx.DrxArrayInt(v)
   @inline implicit final def richDrxArrayLong(v:Array[Long]):drx.DrxArrayLong = new drx.DrxArrayLong(v)
   @inline implicit final def richDrxArrayDouble(v:Array[Double]):drx.DrxArrayDouble = new drx.DrxArrayDouble(v)
   @inline implicit final def richDrxArray[A](v:Array[A]):drx.DrxArray[A] = new drx.DrxArray[A](v)
   @inline implicit final def richDrxIndexedSeq[A](v:IndexedSeq[A]):drx.DrxIndexedSeq[A] = new drx.DrxIndexedSeq[A](v)
   @inline implicit final def richDrxSeq[A](v:Seq[A]):drx.DrxSeq[A] = new drx.DrxSeq[A](v)
   @inline implicit final def richDrxIterable[A](v:Iterable[A]):drx.DrxIterable[A] = new drx.DrxIterable[A](v)
   @inline implicit final def richDrxIterator[A](v:Iterator[A]):drx.DrxIterator[A] = new drx.DrxIterator[A](v)
   @inline implicit final def richDrxMap[A,B](v:Map[A,B]):drx.DrxMap[A,B] = new drx.DrxMap[A,B](v)
   @inline implicit final def richDrxSet[A](v:Set[A]):drx.DrxSet[A] = new drx.DrxSet[A](v)
   @inline implicit final def richDrxList[A](v:List[A]):drx.DrxList[A] = new drx.DrxList[A](v)
   @inline implicit final def richDrxIterableIterable[A](v:List[List[A]]):drx.DrxIterableIterable[A] = new drx.DrxIterableIterable[A](v)
   @inline implicit final def richDrxBitSet(v:scala.collection.immutable.BitSet):drx.DrxBitSet = new drx.DrxBitSet(v)

   //--from core/swing.scala
   @inline implicit final def richDrxFrame(v:java.awt.Frame):drx.DrxFrame = new drx.DrxFrame(v) //??? is this really used that much?

   //--from core/package.scala
   @inline implicit final def richDrxOptionApplyIf[A](v:Option[A=>A]):drx.DrxOptionApplyIf[A] = new drx.DrxOptionApplyIf[A](v)
   @inline implicit final def richDrxAny[A](v:A):drx.DrxAny[A] = new drx.DrxAny[A](v)
   @inline implicit final def richDrxFuture[A](v:scala.concurrent.Future[A]):drx.DrxFuture[A] = new drx.DrxFuture[A](v)
   @inline implicit final def richDrxOption[A](v:Option[A]):drx.DrxOption[A] = new drx.DrxOption[A](v)
   @inline implicit final def richDrxTry[A](v:scala.util.Try[A]):drx.DrxTry[A] = new drx.DrxTry[A](v)
   @inline implicit final def richDrxTuple2[A,B](v:Tuple2[A,B]):drx.DrxTuple2[A,B] = new drx.DrxTuple2[A,B](v)
   @inline implicit final def richDrxTuple3[A,B,C](v:Tuple3[A,B,C]):drx.DrxTuple3[A,B,C] = new drx.DrxTuple3[A,B,C](v)

   /*
   @inline implicit class RichInputStream(is:java.io.InputStream){
      def toByteArray:Array[Byte] = File.toByteArray(is)
      def as(sg:File.StreamGenerator):java.io.InputStream = sg(is)
   }
   @inline implicit class RichOutputStream(os:java.io.OutputStream){
      def as(sg:File.StreamGenerator):java.io.OutputStream = sg(os)
   }
   */
}

trait All extends Constants with Style with FlatScala with Conversions with Enrich with CrossVersionPredef

object Predef extends All //an instance of the traits since traits are just more or less "ideas"




© 2015 - 2025 Weber Informatics LLC | Privacy Policy