private void applyPattern(String pattern,boolean localized){
  char zeroDigit=PATTERN_ZERO_DIGIT;
  char groupingSeparator=PATTERN_GROUPING_SEPARATOR;
  char decimalSeparator=PATTERN_DECIMAL_SEPARATOR;
  char percent=PATTERN_PERCENT;
  char perMill=PATTERN_PER_MILLE;
  char digit=PATTERN_DIGIT;
  char separator=PATTERN_SEPARATOR;
  String exponent=PATTERN_EXPONENT;
  char minus=PATTERN_MINUS;
  if (localized) {
    zeroDigit=symbols.getZeroDigit();
    groupingSeparator=symbols.getGroupingSeparator();
    decimalSeparator=symbols.getDecimalSeparator();
    percent=symbols.getPercent();
    perMill=symbols.getPerMill();
    digit=symbols.getDigit();
    separator=symbols.getPatternSeparator();
    exponent=symbols.getExponentSeparator();
    minus=symbols.getMinusSign();
  }
  boolean gotNegative=false;
  decimalSeparatorAlwaysShown=false;
  isCurrencyFormat=false;
  useExponentialNotation=false;
  int phaseOneStart=0;
  int phaseOneLength=0;
  int start=0;
  for (int j=1; j >= 0 && start < pattern.length(); --j) {
    boolean inQuote=false;
    StringBuffer prefix=new StringBuffer();
    StringBuffer suffix=new StringBuffer();
    int decimalPos=-1;
    int multiplier=1;
    int digitLeftCount=0, zeroDigitCount=0, digitRightCount=0;
    byte groupingCount=-1;
    int phase=0;
    StringBuffer affix=prefix;
    for (int pos=start; pos < pattern.length(); ++pos) {
      char ch=pattern.charAt(pos);
switch (phase) {
case 0:
case 2:
        if (inQuote) {
          if (ch == QUOTE) {
            if ((pos + 1) < pattern.length() && pattern.charAt(pos + 1) == QUOTE) {
              ++pos;
              affix.append("''");
            }
 else {
              inQuote=false;
            }
            continue;
          }
        }
 else {
          if (ch == digit || ch == zeroDigit || ch == groupingSeparator || ch == decimalSeparator) {
            phase=1;
            if (j == 1) {
              phaseOneStart=pos;
            }
            --pos;
            continue;
          }
 else           if (ch == CURRENCY_SIGN) {
            boolean doubled=(pos + 1) < pattern.length() && pattern.charAt(pos + 1) == CURRENCY_SIGN;
            if (doubled) {
              ++pos;
            }
            isCurrencyFormat=true;
            affix.append(doubled ? "'\u00A4\u00A4" : "'\u00A4");
            continue;
          }
 else           if (ch == QUOTE) {
            if (ch == QUOTE) {
              if ((pos + 1) < pattern.length() && pattern.charAt(pos + 1) == QUOTE) {
                ++pos;
                affix.append("''");
              }
 else {
                inQuote=true;
              }
              continue;
            }
          }
 else           if (ch == separator) {
            if (phase == 0 || j == 0) {
              throw new IllegalArgumentException("Unquoted special character '" + ch + "' in pattern \""+ pattern+ '"');
            }
            start=pos + 1;
            pos=pattern.length();
            continue;
          }
 else           if (ch == percent) {
            if (multiplier != 1) {
              throw new IllegalArgumentException("Too many percent/per mille characters in pattern \"" + pattern + '"');
            }
            multiplier=100;
            affix.append("'%");
            continue;
          }
 else           if (ch == perMill) {
            if (multiplier != 1) {
              throw new IllegalArgumentException("Too many percent/per mille characters in pattern \"" + pattern + '"');
            }
            multiplier=1000;
            affix.append("'\u2030");
            continue;
          }
 else           if (ch == minus) {
            affix.append("'-");
            continue;
          }
        }
      affix.append(ch);
    break;
case 1:
  if (j == 1) {
    ++phaseOneLength;
  }
 else {
    if (--phaseOneLength == 0) {
      phase=2;
      affix=suffix;
    }
    continue;
  }
if (ch == digit) {
  if (zeroDigitCount > 0) {
    ++digitRightCount;
  }
 else {
    ++digitLeftCount;
  }
  if (groupingCount >= 0 && decimalPos < 0) {
    ++groupingCount;
  }
}
 else if (ch == zeroDigit) {
  if (digitRightCount > 0) {
    throw new IllegalArgumentException("Unexpected '0' in pattern \"" + pattern + '"');
  }
  ++zeroDigitCount;
  if (groupingCount >= 0 && decimalPos < 0) {
    ++groupingCount;
  }
}
 else if (ch == groupingSeparator) {
  groupingCount=0;
}
 else if (ch == decimalSeparator) {
  if (decimalPos >= 0) {
    throw new IllegalArgumentException("Multiple decimal separators in pattern \"" + pattern + '"');
  }
  decimalPos=digitLeftCount + zeroDigitCount + digitRightCount;
}
 else if (pattern.regionMatches(pos,exponent,0,exponent.length())) {
  if (useExponentialNotation) {
    throw new IllegalArgumentException("Multiple exponential " + "symbols in pattern \"" + pattern + '"');
  }
  useExponentialNotation=true;
  minExponentDigits=0;
  pos=pos + exponent.length();
  while (pos < pattern.length() && pattern.charAt(pos) == zeroDigit) {
    ++minExponentDigits;
    ++phaseOneLength;
    ++pos;
  }
  if ((digitLeftCount + zeroDigitCount) < 1 || minExponentDigits < 1) {
    throw new IllegalArgumentException("Malformed exponential " + "pattern \"" + pattern + '"');
  }
  phase=2;
  affix=suffix;
  --pos;
  continue;
}
 else {
  phase=2;
  affix=suffix;
  --pos;
  --phaseOneLength;
  continue;
}
break;
}
}
if (zeroDigitCount == 0 && digitLeftCount > 0 && decimalPos >= 0) {
int n=decimalPos;
if (n == 0) {
++n;
}
digitRightCount=digitLeftCount - n;
digitLeftCount=n - 1;
zeroDigitCount=1;
}
if ((decimalPos < 0 && digitRightCount > 0) || (decimalPos >= 0 && (decimalPos < digitLeftCount || decimalPos > (digitLeftCount + zeroDigitCount))) || groupingCount == 0 || inQuote) {
throw new IllegalArgumentException("Malformed pattern \"" + pattern + '"');
}
if (j == 1) {
posPrefixPattern=prefix.toString();
posSuffixPattern=suffix.toString();
negPrefixPattern=posPrefixPattern;
negSuffixPattern=posSuffixPattern;
int digitTotalCount=digitLeftCount + zeroDigitCount + digitRightCount;
int effectiveDecimalPos=decimalPos >= 0 ? decimalPos : digitTotalCount;
setMinimumIntegerDigits(effectiveDecimalPos - digitLeftCount);
setMaximumIntegerDigits(useExponentialNotation ? digitLeftCount + getMinimumIntegerDigits() : MAXIMUM_INTEGER_DIGITS);
setMaximumFractionDigits(decimalPos >= 0 ? (digitTotalCount - decimalPos) : 0);
setMinimumFractionDigits(decimalPos >= 0 ? (digitLeftCount + zeroDigitCount - decimalPos) : 0);
setGroupingUsed(groupingCount > 0);
this.groupingSize=(groupingCount > 0) ? groupingCount : 0;
this.multiplier=multiplier;
setDecimalSeparatorAlwaysShown(decimalPos == 0 || decimalPos == digitTotalCount);
}
 else {
negPrefixPattern=prefix.toString();
negSuffixPattern=suffix.toString();
gotNegative=true;
}
}
if (pattern.length() == 0) {
posPrefixPattern=posSuffixPattern="";
setMinimumIntegerDigits(0);
setMaximumIntegerDigits(MAXIMUM_INTEGER_DIGITS);
setMinimumFractionDigits(0);
setMaximumFractionDigits(MAXIMUM_FRACTION_DIGITS);
}
if (!gotNegative || (negPrefixPattern.equals(posPrefixPattern) && negSuffixPattern.equals(posSuffixPattern))) {
negSuffixPattern=posSuffixPattern;
negPrefixPattern="'-" + posPrefixPattern;
}
expandAffixes();
}

private void applyPattern ( String pattern , boolean localized ) { char zeroDigit = PATTERN_ZERO_DIGIT ; char groupingSeparator = PATTERN_GROUPING_SEPARATOR ; char decimalSeparator = PATTERN_DECIMAL_SEPARATOR ; char percent = PATTERN_PERCENT ; char perMill = PATTERN_PER_MILLE ; char digit = PATTERN_DIGIT ; char separator = PATTERN_SEPARATOR ; String exponent = PATTERN_EXPONENT ; char minus = PATTERN_MINUS ; if ( localized ) { zeroDigit = symbols . getZeroDigit ( ) ; groupingSeparator = symbols . getGroupingSeparator ( ) ; decimalSeparator = symbols . getDecimalSeparator ( ) ; percent = symbols . getPercent ( ) ; perMill = symbols . getPerMill ( ) ; digit = symbols . getDigit ( ) ; separator = symbols . getPatternSeparator ( ) ; exponent = symbols . getExponentSeparator ( ) ; minus = symbols . getMinusSign ( ) ; } boolean gotNegative = false ; decimalSeparatorAlwaysShown = false ; isCurrencyFormat = false ; useExponentialNotation = false ; int phaseOneStart = 0 ; int phaseOneLength = 0 ; int start = 0 ; for ( int j = 1 ; j >= 0 && start < pattern . length ( ) ; -- j ) { boolean inQuote = false ; StringBuffer prefix = new StringBuffer ( ) ; StringBuffer suffix = new StringBuffer ( ) ; int decimalPos = - 1 ; int multiplier = 1 ; int digitLeftCount = 0 , zeroDigitCount = 0 , digitRightCount = 0 ; byte groupingCount = - 1 ; int phase = 0 ; StringBuffer affix = prefix ; for ( int pos = start ; pos < pattern . length ( ) ; ++ pos ) { char ch = pattern . charAt ( pos ) ; switch ( phase ) { case 0 : case 2 : if ( inQuote ) { if ( ch == QUOTE ) { if ( ( pos + 1 ) < pattern . length ( ) && pattern . charAt ( pos + 1 ) == QUOTE ) { ++ pos ; affix . append ( "''" ) ; } else { inQuote = false ; } continue ; } } else { if ( ch == digit || ch == zeroDigit || ch == groupingSeparator || ch == decimalSeparator ) { phase = 1 ; if ( j == 1 ) { phaseOneStart = pos ; } -- pos ; continue ; } else if ( ch == CURRENCY_SIGN ) { boolean doubled = ( pos + 1 ) < pattern . length ( ) && pattern . charAt ( pos + 1 ) == CURRENCY_SIGN ; if ( doubled ) { ++ pos ; } isCurrencyFormat = true ; affix . append ( doubled ? "'¤¤" : "'¤" ) ; continue ; } else if ( ch == QUOTE ) { if ( ch == QUOTE ) { if ( ( pos + 1 ) < pattern . length ( ) && pattern . charAt ( pos + 1 ) == QUOTE ) { ++ pos ; affix . append ( "''" ) ; } else { inQuote = true ; } continue ; } } else if ( ch == separator ) { if ( phase == 0 || j == 0 ) { throw new IllegalArgumentException ( "Unquoted special character '" + ch + "' in pattern \"" + pattern + '"' ) ; } start = pos + 1 ; pos = pattern . length ( ) ; continue ; } else if ( ch == percent ) { if ( multiplier != 1 ) { throw new IllegalArgumentException ( "Too many percent/per mille characters in pattern \"" + pattern + '"' ) ; } multiplier = 100 ; affix . append ( "'%" ) ; continue ; } else if ( ch == perMill ) { if ( multiplier != 1 ) { throw new IllegalArgumentException ( "Too many percent/per mille characters in pattern \"" + pattern + '"' ) ; } multiplier = 1000 ; affix . append ( "'‰" ) ; continue ; } else if ( ch == minus ) { affix . append ( "'-" ) ; continue ; } } affix . append ( ch ) ; break ; case 1 : if ( j == 1 ) { ++ phaseOneLength ; } else { if ( -- phaseOneLength == 0 ) { phase = 2 ; affix = suffix ; } continue ; } if ( ch == digit ) { if ( zeroDigitCount > 0 ) { ++ digitRightCount ; } else { ++ digitLeftCount ; } if ( groupingCount >= 0 && decimalPos < 0 ) { ++ groupingCount ; } } else if ( ch == zeroDigit ) { if ( digitRightCount > 0 ) { throw new IllegalArgumentException ( "Unexpected '0' in pattern \"" + pattern + '"' ) ; } ++ zeroDigitCount ; if ( groupingCount >= 0 && decimalPos < 0 ) { ++ groupingCount ; } } else if ( ch == groupingSeparator ) { groupingCount = 0 ; } else if ( ch == decimalSeparator ) { if ( decimalPos >= 0 ) { throw new IllegalArgumentException ( "Multiple decimal separators in pattern \"" + pattern + '"' ) ; } decimalPos = digitLeftCount + zeroDigitCount + digitRightCount ; } else if ( pattern . regionMatches ( pos , exponent , 0 , exponent . length ( ) ) ) { if ( useExponentialNotation ) { throw new IllegalArgumentException ( "Multiple exponential " + "symbols in pattern \"" + pattern + '"' ) ; } useExponentialNotation = true ; minExponentDigits = 0 ; pos = pos + exponent . length ( ) ; while ( pos < pattern . length ( ) && pattern . charAt ( pos ) == zeroDigit ) { ++ minExponentDigits ; ++ phaseOneLength ; ++ pos ; } if ( ( digitLeftCount + zeroDigitCount ) < 1 || minExponentDigits < 1 ) { throw new IllegalArgumentException ( "Malformed exponential " + "pattern \"" + pattern + '"' ) ; } phase = 2 ; affix = suffix ; -- pos ; continue ; } else { phase = 2 ; affix = suffix ; -- pos ; -- phaseOneLength ; continue ; } break ; } } if ( zeroDigitCount == 0 && digitLeftCount > 0 && decimalPos >= 0 ) { int n = decimalPos ; if ( n == 0 ) { ++ n ; } digitRightCount = digitLeftCount - n ; digitLeftCount = n - 1 ; zeroDigitCount = 1 ; } if ( ( decimalPos < 0 && digitRightCount > 0 ) || ( decimalPos >= 0 && ( decimalPos < digitLeftCount || decimalPos > ( digitLeftCount + zeroDigitCount ) ) ) || groupingCount == 0 || inQuote ) { throw new IllegalArgumentException ( "Malformed pattern \"" + pattern + '"' ) ; } if ( j == 1 ) { posPrefixPattern = prefix . toString ( ) ; posSuffixPattern = suffix . toString ( ) ; negPrefixPattern = posPrefixPattern ; negSuffixPattern = posSuffixPattern ; int digitTotalCount = digitLeftCount + zeroDigitCount + digitRightCount ; int effectiveDecimalPos = decimalPos >= 0 ? decimalPos : digitTotalCount ; setMinimumIntegerDigits ( effectiveDecimalPos - digitLeftCount ) ; setMaximumIntegerDigits ( useExponentialNotation ? digitLeftCount + getMinimumIntegerDigits ( ) : MAXIMUM_INTEGER_DIGITS ) ; setMaximumFractionDigits ( decimalPos >= 0 ? ( digitTotalCount - decimalPos ) : 0 ) ; setMinimumFractionDigits ( decimalPos >= 0 ? ( digitLeftCount + zeroDigitCount - decimalPos ) : 0 ) ; setGroupingUsed ( groupingCount > 0 ) ; this . groupingSize = ( groupingCount > 0 ) ? groupingCount : 0 ; this . multiplier = multiplier ; setDecimalSeparatorAlwaysShown ( decimalPos == 0 || decimalPos == digitTotalCount ) ; } else { negPrefixPattern = prefix . toString ( ) ; negSuffixPattern = suffix . toString ( ) ; gotNegative = true ; } } if ( pattern . length ( ) == 0 ) { posPrefixPattern = posSuffixPattern = "" ; setMinimumIntegerDigits ( 0 ) ; setMaximumIntegerDigits ( MAXIMUM_INTEGER_DIGITS ) ; setMinimumFractionDigits ( 0 ) ; setMaximumFractionDigits ( MAXIMUM_FRACTION_DIGITS ) ; } if ( ! gotNegative || ( negPrefixPattern . equals ( posPrefixPattern ) && negSuffixPattern . equals ( posSuffixPattern ) ) ) { negSuffixPattern = posSuffixPattern ; negPrefixPattern = "'-" + posPrefixPattern ; } expandAffixes ( ) ; }
does the real work of applying a pattern .
