// -*- c++ -*-

/*
 * Jakelib2 - General purpose C++ library
 * Copyright (C) 2001 Florian Wolff  (florian@donuz.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: DecimalFormat.h,v 1.7 2003/09/16 20:16:24 florian Exp $
 */

#ifndef INCLUDE_JAKELIB2_TEXT_DECIMALFORMAT_H
# define INCLUDE_JAKELIB2_TEXT_DECIMALFORMAT_H

#include "NumberFormat.h"
#include "ParsePosition.h"
#include "ParseException.h"
#include "DecimalFormatSyms.h"
#include "FieldPosition.h"


namespace jakelib {
  namespace util {
    class Locale;
    class Locales;
  }

  namespace text  {
    /**
     * @class DecimalFormat
     */
    class DecimalFormat : public jakelib::text::NumberFormat
    {
    public:

      void applyLocalizedPattern(jakelib::lang::String* pattern);

      void applyPattern(jakelib::lang::String* pattern);

      DecimalFormat();

      DecimalFormat(jakelib::lang::String* pattern);

      DecimalFormat(jakelib::lang::String* pattern, 
                    jakelib::text::DecimalFormatSyms* symbols);

      jboolean equals(jakelib::lang::Object* obj);

      jakelib::lang::StringBuffer* format(jdouble number, 
                                          jakelib::lang::StringBuffer* sbuf,
                                          jakelib::text::FieldPosition* fp);

      jakelib::lang::StringBuffer* format(jlong number, 
                                          jakelib::lang::StringBuffer* sbuf,
                                          jakelib::text::FieldPosition* fp);

      jakelib::text::DecimalFormatSyms* getDecimalFormatSymbols();

      jint getGroupingSize();

      jint getMultiplier();

      jakelib::lang::String* getNegativePrefix();

      jakelib::lang::String* getNegativeSuffix();

      jakelib::lang::String* getPositivePrefix();

      jakelib::lang::String* getPositiveSuffix();

      jint hashCode();

      jboolean isDecimalSeparatorAlwaysShown();

      jakelib::lang::Number* parse(jakelib::lang::String* str, 
                                   jakelib::text::ParsePosition* pos);

      void setDecimalFormatSymbols(jakelib::text::DecimalFormatSyms* newSymbols);

      void setDecimalSeparatorAlwaysShown(jboolean newValue);

      void setGroupingSize(jint groupSize);

      void setMaximumFractionDigits(jint newValue);

      void setMaximumIntegerDigits(jint newValue);

      void setMinimumFractionDigits(jint newValue);

      void setMinimumIntegerDigits(jint newValue);

      void setMultiplier(jint newValue);

      void setNegativePrefix(jakelib::lang::String* newValue);

      void setNegativeSuffix(jakelib::lang::String* newValue);

      void setPositivePrefix(jakelib::lang::String* newValue);

      void setPositiveSuffix(jakelib::lang::String* newValue);

      jakelib::lang::String* toLocalizedPattern();

      jakelib::lang::String* toPattern();

      /**
       * @brief Called by \c Object::initJakelib2().
       * Do not use otherwise.
       */
      static void initDecimalFormatClass();

    private:

      void quoteFix(jakelib::lang::StringBuffer* buf, 
                    jakelib::lang::String* text, 
                    jakelib::lang::String* patChars);

      jakelib::lang::String* computePattern(jakelib::text::DecimalFormatSyms* syms);

      // These names are fixed by the serialization spec.
      jboolean decimalSeparatorAlwaysShown;
      jbyte groupingSize;
      jbyte minExponentDigits;
      jint multiplier;
      jakelib::lang::String* negativePrefix;
      jakelib::lang::String* negativeSuffix;
      jakelib::lang::String* positivePrefix;
      jakelib::lang::String* positiveSuffix;
      jakelib::text::DecimalFormatSyms* symbols;
      jboolean useExponentialNotation;

      // The locale-independent pattern symbols happen to be the same as
      // the US symbols->
      static jakelib::text::DecimalFormatSyms* nonLocalizedSymbols;

      jboolean equals(jakelib::lang::String* s1, jakelib::lang::String* s2);
      
      // This is a helper for applyPatternWithSymbols.  It reads a prefix
      // or a suffix.  It can cause some side-effects.
      jint scanFix(jakelib::lang::String* pattern, jint index, 
                   jakelib::lang::StringBuffer* buf,
                   jakelib::lang::String* patChars, 
                   jakelib::text::DecimalFormatSyms* syms,
                   jboolean is_suffix);

      // A helper which reads a number format.
      jint scanFormat(jakelib::lang::String* pattern, 
                      jint index,
                      jakelib::lang::String* patChars, 
                      jakelib::text::DecimalFormatSyms* syms,
                      jboolean is_positive);

      // This helper function creates a string consisting of all the
      // characters which can appear in a pattern and must be quoted.
      jakelib::lang::String* patternChars(jakelib::text::DecimalFormatSyms* syms);

      void applyPatternWithSymbols(jakelib::lang::String* pattern,
                                   jakelib::text::DecimalFormatSyms* syms);

      JAKELIB_DECLARE_CLASS(DecimalFormat);
    };

    JAKELIB_DECLARE_ARRAY(DecimalFormat);
  }
}

#endif
