782 * {@code NumberFormat.Field.SUFFIX} fields
783 */
784 private void appendSuffix(StringBuffer result, String suffix,
785 FieldDelegate delegate) {
786 append(result, expandAffix(suffix), delegate,
787 getFieldPositions(suffix, NumberFormat.Field.SUFFIX));
788 }
789
790 /**
791 * Appends the {@code string} to the {@code result}.
792 * {@code delegate} is notified of SIGN, PREFIX and/or SUFFIX
793 * field positions.
794 * @param result the resulting string, where the text is to be appended
795 * @param string the text to append
796 * @param delegate notified of the locations of sub fields
797 * @param positions a list of {@code FieldPostion} in the given
798 * string
799 */
800 private void append(StringBuffer result, String string,
801 FieldDelegate delegate, List<FieldPosition> positions) {
802 if (string.length() > 0) {
803 int start = result.length();
804 result.append(string);
805 for (int counter = 0; counter < positions.size(); counter++) {
806 FieldPosition fp = positions.get(counter);
807 Format.Field attribute = fp.getFieldAttribute();
808 delegate.formatted(attribute, attribute,
809 start + fp.getBeginIndex(),
810 start + fp.getEndIndex(), result);
811 }
812 }
813 }
814
815 /**
816 * Expands an affix {@code pattern} into a string of literals.
817 * All characters in the pattern are literals unless prefixed by QUOTE.
818 * The character prefixed by QUOTE is replaced with its respective
819 * localized literal.
820 * @param pattern a compact number pattern affix
821 * @return an expanded affix
822 */
1196 negativePrefix = positivePrefix;
1197 negativeSuffix = positiveSuffix;
1198 } else {
1199 negativePrefix = prefix.toString();
1200 negativeSuffix = suffix.toString();
1201 gotNegative = true;
1202 }
1203
1204 // If there is no negative pattern, or if the negative pattern is
1205 // identical to the positive pattern, then prepend the minus sign to
1206 // the positive pattern to form the negative pattern.
1207 if (!gotNegative
1208 || (negativePrefix.equals(positivePrefix)
1209 && negativeSuffix.equals(positiveSuffix))) {
1210 negativeSuffix = positiveSuffix;
1211 negativePrefix = "'-" + positivePrefix;
1212 }
1213 }
1214
1215 // If no 0s are specified in a non empty pattern, it is invalid
1216 if (pattern.length() != 0 && zeros.isEmpty()) {
1217 throw new IllegalArgumentException("Invalid pattern"
1218 + " [" + pattern + "]: all patterns must include digit"
1219 + " placement 0s");
1220 }
1221
1222 // Only if positive affix exists; else put empty strings
1223 if (!positivePrefix.isEmpty() || !positiveSuffix.isEmpty()) {
1224 positivePrefixPatterns.add(positivePrefix);
1225 negativePrefixPatterns.add(negativePrefix);
1226 positiveSuffixPatterns.add(positiveSuffix);
1227 negativeSuffixPatterns.add(negativeSuffix);
1228 divisors.add(computeDivisor(zeros, index));
1229 } else {
1230 positivePrefixPatterns.add("");
1231 negativePrefixPatterns.add("");
1232 positiveSuffixPatterns.add("");
1233 negativeSuffixPatterns.add("");
1234 divisors.add(1L);
1235 }
1236 }
|
782 * {@code NumberFormat.Field.SUFFIX} fields
783 */
784 private void appendSuffix(StringBuffer result, String suffix,
785 FieldDelegate delegate) {
786 append(result, expandAffix(suffix), delegate,
787 getFieldPositions(suffix, NumberFormat.Field.SUFFIX));
788 }
789
790 /**
791 * Appends the {@code string} to the {@code result}.
792 * {@code delegate} is notified of SIGN, PREFIX and/or SUFFIX
793 * field positions.
794 * @param result the resulting string, where the text is to be appended
795 * @param string the text to append
796 * @param delegate notified of the locations of sub fields
797 * @param positions a list of {@code FieldPostion} in the given
798 * string
799 */
800 private void append(StringBuffer result, String string,
801 FieldDelegate delegate, List<FieldPosition> positions) {
802 if (!string.isEmpty()) {
803 int start = result.length();
804 result.append(string);
805 for (int counter = 0; counter < positions.size(); counter++) {
806 FieldPosition fp = positions.get(counter);
807 Format.Field attribute = fp.getFieldAttribute();
808 delegate.formatted(attribute, attribute,
809 start + fp.getBeginIndex(),
810 start + fp.getEndIndex(), result);
811 }
812 }
813 }
814
815 /**
816 * Expands an affix {@code pattern} into a string of literals.
817 * All characters in the pattern are literals unless prefixed by QUOTE.
818 * The character prefixed by QUOTE is replaced with its respective
819 * localized literal.
820 * @param pattern a compact number pattern affix
821 * @return an expanded affix
822 */
1196 negativePrefix = positivePrefix;
1197 negativeSuffix = positiveSuffix;
1198 } else {
1199 negativePrefix = prefix.toString();
1200 negativeSuffix = suffix.toString();
1201 gotNegative = true;
1202 }
1203
1204 // If there is no negative pattern, or if the negative pattern is
1205 // identical to the positive pattern, then prepend the minus sign to
1206 // the positive pattern to form the negative pattern.
1207 if (!gotNegative
1208 || (negativePrefix.equals(positivePrefix)
1209 && negativeSuffix.equals(positiveSuffix))) {
1210 negativeSuffix = positiveSuffix;
1211 negativePrefix = "'-" + positivePrefix;
1212 }
1213 }
1214
1215 // If no 0s are specified in a non empty pattern, it is invalid
1216 if (!pattern.isEmpty() && zeros.isEmpty()) {
1217 throw new IllegalArgumentException("Invalid pattern"
1218 + " [" + pattern + "]: all patterns must include digit"
1219 + " placement 0s");
1220 }
1221
1222 // Only if positive affix exists; else put empty strings
1223 if (!positivePrefix.isEmpty() || !positiveSuffix.isEmpty()) {
1224 positivePrefixPatterns.add(positivePrefix);
1225 negativePrefixPatterns.add(negativePrefix);
1226 positiveSuffixPatterns.add(positiveSuffix);
1227 negativeSuffixPatterns.add(negativeSuffix);
1228 divisors.add(computeDivisor(zeros, index));
1229 } else {
1230 positivePrefixPatterns.add("");
1231 negativePrefixPatterns.add("");
1232 positiveSuffixPatterns.add("");
1233 negativeSuffixPatterns.add("");
1234 divisors.add(1L);
1235 }
1236 }
|