alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (BooleanProp.scala)

This example Scala source code file (BooleanProp.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

boolean, boolean, booleanprop, booleanprop, booleanpropimpl, booleanpropimpl, constantimpl, prop, propimpl, some, string, string, t1, unit

The Scala BooleanProp.scala source code

/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2011, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

package scala.sys

/** A few additional conveniences for Boolean properties.
 */
trait BooleanProp extends Prop[Boolean] {
  /** The semantics of value are determined at Prop creation.  See methods
   *  `valueIsTrue` and `keyExists` in object BooleanProp for examples.
   *
   *  @return   true if the current String is considered true, false otherwise
   */
  def value: Boolean
  
  /** Alter this property so that `value` will be true. */
  def enable(): Unit
  
  /** Alter this property so that `value` will be false. */
  def disable(): Unit

  /** Toggle the property between enabled and disabled states. */
  def toggle(): Unit
}

object BooleanProp {
  private[sys]
  class BooleanPropImpl(key: String, valueFn: String => Boolean) extends PropImpl(key, valueFn) with BooleanProp {
    override def setValue[T1 >: Boolean](newValue: T1): Boolean = newValue match {
      case x: Boolean if !x   => val old = value ; clear() ; old
      case x                  => super.setValue(newValue)
    }
    def enable()  = this setValue true
    def disable() = this.clear()
    def toggle()  = if (value) disable() else enable()
  }
  private[sys]
  class ConstantImpl(val key: String, val value: Boolean) extends BooleanProp {
    val isSet = value
    def set(newValue: String) = "" + value
    def setValue[T1 >: Boolean](newValue: T1): Boolean = value
    def get: String = "" + value
    val clear, enable, disable, toggle = ()
    def option = if (isSet) Some(value) else None
    
    protected def zero = false
  }
  
  /** The java definition of property truth is that the key be in the map and
   *  the value be equal to the String "true", case insensitively.  This method
   *  creates a BooleanProp instance which adheres to that definition.
   *
   *  @return   A BooleanProp which acts like java's Boolean.getBoolean
   */
  def valueIsTrue[T](key: String): BooleanProp = new BooleanPropImpl(key, _.toLowerCase == "true")
  
  /** As an alternative, this method creates a BooleanProp which is true
   *  if the key exists in the map.  This way -Dfoo.bar is enough to be
   *  considered true.
   *
   *  @return   A BooleanProp with a liberal truth policy
   */
  def keyExists[T](key: String): BooleanProp = new BooleanPropImpl(key, _ => true)
  
  /** A constant true or false property which ignores all method calls.
   */
  def constant(key: String, isOn: Boolean): BooleanProp = new ConstantImpl(key, isOn)
  
  implicit def booleanPropAsBoolean(b: BooleanProp): Boolean = b.value
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala BooleanProp.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.