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

org.apache.fulcrum.intake.model.BigDecimalField Maven / Gradle / Ivy

The newest version!
package org.apache.fulcrum.intake.model;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.math.BigDecimal;

import org.apache.fulcrum.intake.IntakeException;
import org.apache.fulcrum.intake.validator.BigDecimalValidator;

/**
 * @author John McNally
 * @author Daniel Rall
 * @author Henning P. Schmiedehausen
 * @author Quinton McCombs
 * @author Thomas Vandahl
 * @version $Id$
 */
public class BigDecimalField
        extends Field
{
    /** Serial version */
	private static final long serialVersionUID = -8356433008715252236L;

	/**
     * Constructor.
     *
     * @param field xml field definition object
     * @param group xml group definition object
     * @throws IntakeException thrown by superclass
     */
    public BigDecimalField(XmlField field, Group group)
            throws IntakeException
    {
        super(field, group);
    }

    /**
     * Sets the default value for a BigDecimal field
     *
     * @param prop Parameter for the default values
     */
    @Override
	public void setDefaultValue(String prop)
    {
        defaultValue = null;

        if (prop == null)
        {
            return;
        }

        defaultValue = new BigDecimal(prop);
    }

    /**
     * Set the empty Value. This value is used if Intake
     * maps a field to a parameter returned by the user and
     * the corresponding field is either empty (empty string)
     * or non-existant.
     *
     * @param prop The value to use if the field is empty.
     */
    @Override
	public void setEmptyValue(String prop)
    {
        emptyValue = null;

        if (prop == null)
        {
            return;
        }

        emptyValue = new BigDecimal(prop);
    }

    /**
     * A suitable validator.
     *
     * @return A suitable validator
     */
    @Override
	protected String getDefaultValidator()
    {
        return BigDecimalValidator.class.getName();
    }

    /**
     * Sets the value of the field from data in the parser.
     */
    @Override
	protected void doSetValue()
    {
        if (isMultiValued())
        {
            BigDecimal[] values = parser.getBigDecimals(getKey());

            for (int i = 0; i < values.length; i++)
            {
                if (values[i] == null)
                {
                    values[i] = getEmptyValue();
                }
            }

            setTestValue(values);
        }
        else
        {
            setTestValue(parser.getBigDecimal(getKey(), getEmptyValue()));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy