001/*
002 * CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE
003 * CONDITION THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT.
004 * PLEASE READ THE TERMS AND CONDITIONS OF THIS AGREEMENT CAREFULLY. BY
005 * DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF THE
006 * AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE"
007 * BUTTON AT THE BOTTOM OF THIS PAGE.
008 * 
009 * Specification: JSR-354 Money and Currency API ("Specification")
010 * 
011 * Copyright (c) 2012-2013, Credit Suisse All rights reserved.
012 */
013package org.javamoney.moneta.format;
014
015import java.math.RoundingMode;
016import java.text.DecimalFormat;
017import java.text.DecimalFormatSymbols;
018import java.util.Locale;
019
020import javax.money.MonetaryAdjuster;
021
022public final class AmountStyle {
023
024        private static final char[] EMPTY_CHAR_ARRAY = new char[0];
025        private static final int[] EMPTY_INT_ARRAY = new int[0];
026        private DecimalFormat format;
027        private MonetaryAdjuster rounding;
028        private int[] groupSizes;
029        private char[] groupChars;
030
031        private AmountStyle(DecimalFormat format, int[] groupSizes,
032                        char[] groupChars, MonetaryAdjuster rounding) {
033                if (format == null) {
034                        throw new IllegalArgumentException("DecimalFormat required.");
035                }
036                this.groupSizes = groupSizes;
037                this.groupChars = groupChars;
038                this.rounding = rounding;
039                this.format = format;
040        }
041
042        public MonetaryAdjuster getMoneyRounding() {
043                return this.rounding;
044        }
045
046        public int[] getNumberGroupSizes() {
047                if(this.groupSizes==null){
048                        return EMPTY_INT_ARRAY;
049                }
050                return this.groupSizes.clone();
051        }
052
053        public char[] getNumberGroupChars() {
054                if(this.groupChars==null){
055                        return EMPTY_CHAR_ARRAY;
056                }
057                return this.groupChars.clone();
058        }
059
060        public String getPattern() {
061                return this.format.toPattern();
062        }
063        
064        public String getLocalizedPattern(String pattern) {
065                return this.format.toLocalizedPattern();
066        }
067
068        public DecimalFormatSymbols getDecimalSymbols(){
069                return this.format.getDecimalFormatSymbols();
070        }
071        
072        public int getMaximumFractionDigits(){
073                return this.format.getMaximumFractionDigits();
074        }
075        
076        public int withMaximumIntegerDigits(){
077                return this.format.getMaximumIntegerDigits();
078        }
079        
080        public int getMinimumFractionDigits(){
081                return this.format.getMinimumFractionDigits();
082        }
083        
084        public int getMinimumIntegerDigits(){
085                return this.format.getMinimumIntegerDigits();
086        }
087        
088        public int getMultiplier(){
089                return this.format.getMultiplier();
090        }
091        
092        public String getNegativePrefix(){
093                return this.format.getNegativePrefix();
094        }
095        
096        public String getNegativeSuffix(){
097                return this.format.getNegativeSuffix();
098        }
099        
100        public String getPositivePrefix(){
101                return this.format.getPositivePrefix();
102        }
103        
104        public String getPositiveSuffix(){
105                return this.format.getPositiveSuffix();
106        }
107
108        public RoundingMode getRounding(){
109                return this.format.getRoundingMode();
110        }
111        
112        public boolean isDecimalSeparatorAlwaysShown(){
113                return this.format.isDecimalSeparatorAlwaysShown();
114        }
115        
116        public boolean isParseIntegerOnly(){
117                return this.format.isParseIntegerOnly();
118        }
119
120        public static final class Builder {
121
122                private DecimalFormat format;
123                private MonetaryAdjuster rounding;
124                private int[] groupSizes;
125                private char[] groupChars;
126
127                public Builder(Locale locale) {
128                        if(locale==null){       
129                                throw new IllegalArgumentException("Locale required.");
130                        }
131                        this.format = (DecimalFormat) DecimalFormat.getInstance(locale);
132                }
133                
134                public Builder withRounding(MonetaryAdjuster rounding) {
135                        this.rounding = rounding;
136                        return this;
137                }
138
139                public Builder withNumberGroupSizes(int... groupSizes) {
140                        this.groupSizes = groupSizes;
141                        return this;
142                }
143
144                public Builder withNumberGroupChars(char... groupChars) {
145                        this.groupChars = groupChars;
146                        return this;
147                }
148
149                public Builder withPattern(String pattern) {
150                        this.format.applyPattern(pattern);
151                        return this;
152                }
153                
154                public Builder withLocalizedPattern(String pattern) {
155                        this.format.applyLocalizedPattern(pattern);
156                        return this;
157                }
158
159                public Builder withDecimalFormat(DecimalFormat format){
160                        if(format==null){       
161                                throw new IllegalArgumentException("format required.");
162                        }
163                        this.format = format;
164                        return this;
165                }
166                
167                public Builder withDecimalSymbols(DecimalFormatSymbols symbols){
168                        if(symbols==null){      
169                                throw new IllegalArgumentException("symbols required.");
170                        }
171                        this.format.setDecimalFormatSymbols(symbols);
172                        return this;
173                }
174                
175                public Builder withMaximumFractionDigits(int maxFractionDigits){
176                        this.format.setMaximumFractionDigits(maxFractionDigits);
177                        return this;
178                }
179                
180                public Builder withMaximumIntegerDigits(int maxIntegerDigits){
181                        this.format.setMaximumIntegerDigits(maxIntegerDigits);
182                        return this;
183                }
184                
185                public Builder withMinimumFractionDigits(int minFractionDigits){
186                        this.format.setMinimumFractionDigits(minFractionDigits);
187                        return this;
188                }
189                
190                public Builder withMinimumIntegerDigits(int minIntegerDigits){
191                        this.format.setMinimumIntegerDigits(minIntegerDigits);
192                        return this;
193                }
194                
195                public Builder withMultiplier(int multiplier){
196                        this.format.setMultiplier(multiplier);
197                        return this;
198                }
199                
200                public Builder withNegativePrefix(String prefix){
201                        this.format.setNegativePrefix(prefix);
202                        return this;
203                }
204                
205                public Builder withNegativeSuffix(String suffix){
206                        this.format.setNegativeSuffix(suffix);
207                        return this;
208                }
209                
210                public Builder withPositivePrefix(String prefix){
211                        this.format.setPositivePrefix(prefix);
212                        return this;
213                }
214                
215                public Builder withPositiveSuffix(String suffix){
216                        this.format.setPositiveSuffix(suffix);
217                        return this;
218                }
219
220                public Builder withRounding(RoundingMode roundingMode){
221                        this.format.setRoundingMode(roundingMode);
222                        return this;
223                }
224                
225                public Builder withDecimalSeparatorAlwaysShown(boolean value){
226                        this.format.setDecimalSeparatorAlwaysShown(value);
227                        return this;
228                }
229                
230                public Builder withParseIntegerOnly(boolean value){
231                        this.format.setParseIntegerOnly(value);
232                        return this;
233                }
234
235                public AmountStyle build() {
236                        return new AmountStyle(format, groupSizes, groupChars, rounding);
237                }
238
239                public Builder withCurrencyFormat(Locale locale) {
240                        if(locale==null){       
241                                throw new IllegalArgumentException("locale required.");
242                        }
243                        this.format = (DecimalFormat) DecimalFormat.getCurrencyInstance(locale);
244                        return this;
245                }
246                
247        }
248
249        DecimalFormat getDecimalFormat() {
250                return this.format;
251        }
252
253}