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

Lift Framework example source code file (DecimalField.scala)

This example Lift Framework source code file (DecimalField.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 - Lift Framework tags/keywords

bigdecimal, bigdecimal, box, decimaltypedfield, field, int, int, jvalue, math, mathcontext, mathcontext, ownertype, ownertype, record, string

The Lift Framework DecimalField.scala source code

/*
 * Copyright 2007-2011 WorldWide Conferencing, LLC
 *
 * 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 net.liftweb
package record
package field

import java.math.{BigDecimal => JBigDecimal,MathContext,RoundingMode}
import scala.xml._
import net.liftweb.common._
import net.liftweb.http.{S}
import net.liftweb.json.JsonAST.JValue
import net.liftweb.util._
import Helpers._
import S._


trait DecimalTypedField extends NumericTypedField[BigDecimal] {
  protected val scale: Int
  protected val context: MathContext
  private val zero = BigDecimal("0")

  def defaultValue = zero.setScale(scale)

  def setFromAny(in : Any): Box[BigDecimal] = setNumericFromAny(in, n => BigDecimal(n.toString))

  def setFromString (s : String) : Box[BigDecimal] = setBox(tryo(BigDecimal(s)))

  def set_!(in: BigDecimal): BigDecimal = new BigDecimal(in.bigDecimal.setScale(scale, context.getRoundingMode))

  def asJValue = asJString(_.toString)
  def setFromJValue(jvalue: JValue) = setFromJString(jvalue)(s => tryo(BigDecimal(s)))
}


/**
 * <p>
 * A field that maps to a decimal value. Decimal precision and rounding
 * are controlled via the context parameter. The default value is zero.
 * </p>
 *
 * <p>Note:
* Using MathContext.UNLIMITED, whether explicitly or implicitly, means * that no precision or scaling will be used for the SQL field definition; the * default scale for DECIMAL is zero per the SQL standard, but the precision * for DECIMAL is vendor-specific. For example, PostgreSQL uses maximum precision * if it's not specified, but SQL Server uses a default precision of 18. * </p> * * @author Derek Chen-Becker * * @param rec The Record that owns this field * @param context The MathContext that controls precision and rounding * @param scale Controls the scale of the underlying BigDecimal */ class DecimalField[OwnerType <: Record[OwnerType]](rec: OwnerType, val context : MathContext, val scale : Int) extends Field[BigDecimal, OwnerType] with MandatoryTypedField[BigDecimal] with DecimalTypedField { /** * Constructs a DecimalField with the specified initial value. The context * is set to MathContext.UNLIMITED (see note above about default precision). * The scale is taken from the initial value. * * @param rec The Record that owns this field * @param value The initial value */ def this(rec : OwnerType, value : BigDecimal) = { this(rec, MathContext.UNLIMITED, value.scale) set(value) } /** * Constructs a DecimalField with the specified initial value and context. * The scale is taken from the initial value. * * @param rec The Record that owns this field * @param value The initial value * @param context The MathContext that controls precision and rounding */ def this(rec : OwnerType, value : BigDecimal, context : MathContext) = { this(rec, context, value.scale) set(value) } def owner = rec } /** * <p> * A field that maps to a decimal value. Decimal precision and rounding * are controlled via the context parameter. The default value is zero. * </p> * * <p>Note:
* Using MathContext.UNLIMITED, whether explicitly or implicitly, means * that no precision or scaling will be used for the SQL field definition; the * default scale for DECIMAL is zero per the SQL standard, but the precision * for DECIMAL is vendor-specific. For example, PostgreSQL uses maximum precision * if it's not specified, but SQL Server uses a default precision of 18. * </p> * * @author Derek Chen-Becker * * @param rec The Record that owns this field * @param context The MathContext that controls precision and rounding * @param scale Controls the scale of the underlying BigDecimal */ class OptionalDecimalField[OwnerType <: Record[OwnerType]](rec: OwnerType, val context : MathContext, val scale : Int) extends Field[BigDecimal, OwnerType] with OptionalTypedField[BigDecimal] with DecimalTypedField { /** * Constructs a DecimalField with the specified initial value. The context * is set to MathContext.UNLIMITED (see note above about default precision). * The scale is taken from the initial value. * * @param rec The Record that owns this field * @param value The initial value * @param scale the scale of the decimal field, since there might be no value */ def this(rec : OwnerType, value : Box[BigDecimal], scale : Int) = { this(rec, MathContext.UNLIMITED, scale) setBox(value) } /** * Constructs a DecimalField with the specified initial value and context. * The scale is taken from the initial value. * * @param rec The Record that owns this field * @param value The initial value * @param scale the scale of the decimal field, since there might be no value * @param context The MathContext that controls precision and rounding */ def this(rec : OwnerType, value : Box[BigDecimal], scale : Int, context : MathContext) = { this(rec, context, scale) setBox(value) } def owner = rec }

Other Lift Framework examples (source code examples)

Here is a short list of links related to this Lift Framework DecimalField.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.