Package Framework

Source Code of Framework.NumericFormat$qq_Resolver

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package Framework;

import java.io.Serializable;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Currency;
import java.util.Locale;

/**
* The NumericFormat class formats a numeric value {@link IntegerData}, {@link DoubleData} or {@link DecimalData} into text, using a variety of formatting templates.
*
*/
@SuppressWarnings("serial")
public class NumericFormat extends DataFormat implements Serializable {
  private NumberFormat form;
   
  // CraigM:01/09/2008 - As we inherit from DataFormat, we will use the inherited template field.
  // private String template;

    /**
     * Resolves ambiguous constructor arguments
     */
    public class qq_Resolver {
        public static final int cTEMPLATE = 1;
    }
    public NumericFormat() {
        super();
        this.form = new NullAwareNumberFormat();
    }

    public NumericFormat(TextData format){
      this(format.toString(), qq_Resolver.cTEMPLATE);
    }

    public NumericFormat(String format){
      this(format, qq_Resolver.cTEMPLATE);
    }

    public NumericFormat(TextData pValue, int pResolver) {
        this(pValue.toString(), pResolver);
    }

    public NumericFormat(String pValue, int pResolver) {
        if (pResolver == qq_Resolver.cTEMPLATE) {
            this.setTemplate(pValue);
        }
    }
   
    /**
     * The text for the format template.
     */
    public void setTemplate(TextData pTextData) {
        this.setTemplate(pTextData.toString());
    }
    /**
     * The text for the format template.
     * @param pString
     */
    public void setTemplate(String pString) {
      this.template = new TextData(pString);
        this.form = this.getCorrectFormatter();
    }
   
    /**
     * The text for the format template.
     */
    public TextData getTemplate() {
        if (this.template == null) {
            return null;
        } else {
            return new TextData(this.template);
        }
    }
    /**
     * Converts specified numeric value into text value conforming to formatting template
     * @param pSource
     * @return
     */
    public TextData formatNumeric(int pSource) {
        if (this.form == null){
            UsageException ex = new UsageException(Messages.getString("NumericFormat.1")); //$NON-NLS-1$
            ErrorMgr.addError(ex);
            throw ex;
        }
        return new TextData(this.form.format(pSource));
    }

    /**
     * Converts specified numeric value into text value conforming to formatting template
     * @param pSource
     * @return
     */
    public TextData formatNumeric(short pSource) {
        if (this.form == null){
            UsageException ex = new UsageException(Messages.getString("NumericFormat.1")); //$NON-NLS-1$
            ErrorMgr.addError(ex);
            throw ex;
        }
        return new TextData(this.form.format(pSource));
    }

    /**
     * Converts specified numeric value into text value conforming to formatting template
     * @param pSource
     * @return
     */
    public TextData formatNumeric(long pSource) {
        if (this.form == null){
            UsageException ex = new UsageException(Messages.getString("NumericFormat.1")); //$NON-NLS-1$
            ErrorMgr.addError(ex);
            throw ex;
        }
        return new TextData(this.form.format(pSource));
    }
   
    /**
     * Converts specified numeric value into text value conforming to formatting template
     * @param pSource
     * @return
     */
    public TextData formatNumeric(double pSource) {
        if (this.form == null){
            UsageException ex = new UsageException(Messages.getString("NumericFormat.1")); //$NON-NLS-1$
            ErrorMgr.addError(ex);
            throw ex;
        }
        return new TextData(this.form.format(pSource));
    }

    /**
     * Converts specified numeric value into text value conforming to formatting template
     * @param pSource
     * @return
     */
    public TextData formatNumeric(float pSource) {
        if (this.form == null){
            UsageException ex = new UsageException(Messages.getString("NumericFormat.1")); //$NON-NLS-1$
            ErrorMgr.addError(ex);
            throw ex;
        }
        return new TextData(this.form.format(pSource));
    }

    /**
     * Converts specified numeric value into text value conforming to formatting template
     * @param pSource
     * @return
     */
    public TextData formatNumeric(Number pSource) {
        if (this.form == null){
            UsageException ex = new UsageException(Messages.getString("NumericFormat.1")); //$NON-NLS-1$
            ErrorMgr.addError(ex);
            throw ex;
        }
        return new TextData(this.form.format(pSource));
    }

    /**
     * Converts specified numeric value into text value conforming to formatting template
     * @param pSource
     * @return
     */
    public TextData formatNumeric(NumericData pSource) {
        if (this.form == null){
            UsageException ex = new UsageException(Messages.getString("NumericFormat.1")); //$NON-NLS-1$
            ErrorMgr.addError(ex);
            throw ex;
        }
        // AD:6/6/2008 Pass java Number instead. Was causing runtime errors with NumericData
        // TF:24 sept. 2008:Fixed this up to be able to handle a null being passed in.
        if (pSource == null || pSource.isNull()) {
          return new TextData(this.form.format(null));
        }
        else {
          return new TextData(this.form.format(pSource.toNumber()));
        }
    }

    /**
     * Returns maximum possible length in format.
     */
    public int getMaxLength() {
        int length = 0;
        // TF:16/07/2008:If we have a template like #0;-#0;#0;;, we need to return
        // the longest part of the sub-parts of the template
        // TF:17/07/2008:Testing shows that this always returns 0!
//        if (this.template != null) {
//          String[] parts = this.template.split(";");
//          for (String part : parts) {
//            if (part.length() > length) {
//              length = part.length();
//            }
//          }
//        }
        return length;
    }

    /**
     * undocumented in forte
     * @param pSource
     * @return
     */
    public DecimalData decodeDecimal(TextData pSource, short scale) {
        if (this.form == null){
            UsageException ex = new UsageException(Messages.getString("NumericFormat.1")); //$NON-NLS-1$
            ErrorMgr.addError(ex);
            throw ex;
        }
        DecimalData dd = new DecimalData(decodeNumber(pSource.toString()).doubleValue());
        dd.setScale(scale);
        return dd;
    }

    /**
     * Decodes specified text value into double value conforming to formatting template.
     * @param pSource
     * @return
     */
    public DoubleData decodeDouble(TextData pSource) {
      // AD:14/10/2008: Removed Duplicate method
        return decodeDouble(pSource.toString());
    }

    /**
     * Decodes specified text value into double value conforming to formatting template.
     * @param pSource
     * @return
     */
    public DoubleData decodeDouble(String pSource) {
        if (this.form == null){
            UsageException ex = new UsageException(Messages.getString("NumericFormat.1")); //$NON-NLS-1$
            ErrorMgr.addError(ex);
            throw ex;
        }
        // AD:14/10/2008: KIN-9 Don't ignore Suffix characters
        return new DoubleData(decodeNumber(pSource).doubleValue());
    }

   
    private Number decodeNumber(String pSource) {
      // AD:14/10/2008: KIN-9 Don't ignore Suffix characters
        ParsePosition parsePosition = new ParsePosition(0);
        Number num = this.form.parse(pSource, parsePosition);
        if (parsePosition.getIndex() == 0) {
          UsageException  ex = new UsageException();
          String errmsg = Messages.getString("NumericFormat.2"); //$NON-NLS-1$
          ex.reasonCode = UsageException.PARAMETERERROR;
          ex.setError(ErrorDescriptor.USER, errmsg, this.template, pSource);
            ErrorMgr.addError(ex);
            throw ex;
        }
       
        // If the index hasn't processed all the source then it must be ignoring characters
        // so raise an exception like forte
        if (parsePosition.getIndex() < pSource.length()) {
            UsageException  ex = new UsageException();
            String errmsg = Messages.getString("NumericFormat.2"); //$NON-NLS-1$
            ex.reasonCode = UsageException.PARAMETERERROR;
            ex.setError(ErrorDescriptor.USER, errmsg, this.template, pSource);
            ErrorMgr.addError(ex);
            throw ex;
    }
        return num;
    }
    /**
     * Get the formatter that is correct for the current template
     * @return
     */
    public NumberFormat getCorrectFormatter() {
        Locale defaultlocale = Locale.getDefault();
        NumberFormat lForm = null;
        if (this.template.toString().compareTo("DECIMAL") == 0) { //$NON-NLS-1$
            lForm = NumberFormat.getInstance(defaultlocale);
        } else if ((this.template.toString().indexOf('!') != -1)
                || (this.template.toString().compareTo("CURRENCY") == 0)) { //$NON-NLS-1$
            lForm = NumberFormat.getCurrencyInstance(defaultlocale);
        } else if ((this.template.toString().compareTo("EURO_CURRENCY") == 0)) { //$NON-NLS-1$
            Currency euro = Currency.getInstance("EUR"); //$NON-NLS-1$
            lForm = NumberFormat.getCurrencyInstance();
            lForm.setCurrency(euro);
        } else if (this.template.toString().compareTo("INTEGER") == 0) { //$NON-NLS-1$
            lForm = NumberFormat.getIntegerInstance(defaultlocale);
          // AD:14/10/2008: KIN-9 Process any decimal points like Forte
            lForm.setParseIntegerOnly(false);
        } else {
          lForm = new NullAwareNumberFormat(this.template);
        }
        return lForm;
    }

//    public Object clone() {
//        return this.clone(false);
//    }
//
//    public Object clone(boolean pDeep) {
//        NumericFormat result = new NumericFormat();
//        this.duplicateIntoTarget(result, pDeep);
//        return result;
//    }

//    public void duplicateIntoTarget(NumericFormat pData, boolean pDeep) {
//        super.duplicateIntoTarget(pData,pDeep);
//        if (pDeep) {
//            pData.template = this.template;
//            pData.form = (this.form == null) ? null : (NumberFormat)this.form.clone();
//        }
//        else {
//            pData.form = this.form;
//            pData.template = this.template;
//        }
//    }
   
    public NumberFormat getFormatter() {
        return form;
    }
   
    public static void main(String[] args) {
        NumericFormat aFmt = new NumericFormat();

        aFmt.setTemplate("#");
        System.out.println(aFmt.formatNumeric(123456));
        System.out.println(aFmt.formatNumeric(-123));
        System.out.println(aFmt.formatNumeric(0));
        System.out.println(aFmt.formatNumeric((NumericData)null));

        aFmt.setTemplate("#;-#");
        System.out.println(aFmt.formatNumeric(123456));
        System.out.println(aFmt.formatNumeric(-123));
        System.out.println(aFmt.formatNumeric(0));
        System.out.println(aFmt.formatNumeric((NumericData)null));

        aFmt.setTemplate("######");
        System.out.println(aFmt.formatNumeric(123456789));
        System.out.println(aFmt.formatNumeric(-123456789));
        System.out.println(aFmt.formatNumeric(0));
        System.out.println(aFmt.formatNumeric((NumericData)null));

        aFmt.setTemplate("#,######;-#,######;0");
        System.out.println(aFmt.formatNumeric(6000871));
        System.out.println(aFmt.formatNumeric(-6000871));
        System.out.println(aFmt.formatNumeric(0));
        System.out.println(aFmt.formatNumeric((NumericData)null));

        aFmt.setTemplate("#.##;;0.0");
        System.out.println(aFmt.formatNumeric(1234567.89));
        System.out.println(aFmt.formatNumeric(-1234567.89));
        System.out.println(aFmt.formatNumeric(0));
        System.out.println(aFmt.formatNumeric((NumericData)null));

        aFmt.setTemplate("#,##0.00;<#,##0.00>;0.0;;");
        System.out.println(aFmt.formatNumeric(1234500));
        System.out.println(aFmt.formatNumeric(-1234500));
        System.out.println(aFmt.formatNumeric(0));
        System.out.println(aFmt.formatNumeric((NumericData)null));

        aFmt.setTemplate("$#,##0.00 CR;DB $#,##0.00;ZERO;nil");
        System.out.println(aFmt.formatNumeric(1234.57));
        System.out.println(aFmt.formatNumeric(-1234.57));
        System.out.println(aFmt.formatNumeric(0));
        System.out.println(aFmt.formatNumeric((NumericData)null));

        aFmt.setTemplate("#,##0.00 DM;-#,##0.00;0.0;It�sNULL");
        System.out.println(aFmt.formatNumeric(1234));
        System.out.println(aFmt.formatNumeric(-1234));
        System.out.println(aFmt.formatNumeric(0));
        System.out.println(aFmt.formatNumeric((NumericData)null));

//        aFmt.setTemplate("Acct\\. Num\\. 000000");
//        System.out.println(aFmt.formatNumeric(1234));
//        System.out.println(aFmt.formatNumeric(-1234));
//        System.out.println(aFmt.formatNumeric(0));
//        System.out.println(aFmt.formatNumeric((NumericData)null));

        aFmt.setTemplate("#,##0.00 DM;-#,##0.00;0.0;It�sNULL");
        System.out.println(aFmt.formatNumeric(1234));
        System.out.println(aFmt.formatNumeric(-1234));
        System.out.println(aFmt.formatNumeric(0));
        System.out.println(aFmt.formatNumeric((NumericData)null));

        aFmt.setTemplate("0.00%;(0.00%);;;");
        System.out.println(aFmt.formatNumeric(1234.57));
        System.out.println(aFmt.formatNumeric(-1234.57));
        System.out.println(aFmt.formatNumeric(0));
        System.out.println(aFmt.formatNumeric((NumericData)null));

        aFmt.setTemplate("$#,##0.00;($#,##0);0;nil");
        System.out.println(aFmt.formatNumeric(1234.57));
        System.out.println(aFmt.formatNumeric(-1234.57));
        System.out.println(aFmt.formatNumeric(0));
        System.out.println(aFmt.formatNumeric((NumericData)null));

        aFmt.setTemplate("$#,##0.00;($#,##0);;nil");
        System.out.println(aFmt.formatNumeric(1234.57));
        System.out.println(aFmt.formatNumeric(-1234.57));
        System.out.println(aFmt.formatNumeric(0));
        System.out.println(aFmt.formatNumeric((NumericData)null));

        aFmt.setTemplate("0000000#");
        System.out.println(aFmt.formatNumeric(1234));
        System.out.println(aFmt.formatNumeric(12345));
        System.out.println(aFmt.formatNumeric(123456));
        System.out.println(aFmt.formatNumeric(1234567));
        System.out.println(aFmt.formatNumeric(12345678));
        System.out.println(aFmt.formatNumeric(123456789));
    }
}
TOP

Related Classes of Framework.NumericFormat$qq_Resolver

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.