|  | 
| 10 | 10 |  */ | 
| 11 | 11 | package org.javamoney.moneta; | 
| 12 | 12 | 
 | 
| 13 |  | -import java.util.Locale; | 
| 14 |  | -import java.util.Objects; | 
|  | 13 | +import org.javamoney.moneta.internal.ConfigurableCurrencyUnitProvider; | 
| 15 | 14 | 
 | 
| 16 | 15 | import javax.money.CurrencyUnit; | 
|  | 16 | +import javax.money.MonetaryException; | 
|  | 17 | +import java.util.Locale; | 
|  | 18 | +import java.util.Objects; | 
| 17 | 19 | 
 | 
| 18 |  | -import org.javamoney.moneta.internal.ConfigurableCurrencyUnitProvider; | 
| 19 |  | - | 
| 20 |  | -public final class BuildableCurrencyUnit implements CurrencyUnit, Comparable<CurrencyUnit> { | 
| 21 |  | - | 
| 22 |  | -	private String currencyCode; | 
| 23 |  | -	private int numericCode; | 
| 24 |  | -	private int defaultFractionDigits; | 
| 25 |  | - | 
| 26 |  | -	private BuildableCurrencyUnit(Builder builder) { | 
| 27 |  | -		Objects.requireNonNull(builder.currencyCode, "currencyCode required"); | 
| 28 |  | -		if (builder.numericCode < -1) { | 
| 29 |  | -			throw new IllegalArgumentException("numericCode must be >= -1"); | 
| 30 |  | -		} | 
| 31 |  | -		if (builder.defaultFractionDigits < 0) { | 
| 32 |  | -			throw new IllegalArgumentException( | 
| 33 |  | -					"defaultFractionDigits must be >= 0"); | 
| 34 |  | -		} | 
| 35 |  | -		this.defaultFractionDigits = builder.defaultFractionDigits; | 
| 36 |  | -		this.numericCode = builder.numericCode; | 
| 37 |  | -		this.currencyCode = builder.currencyCode; | 
| 38 |  | -	} | 
| 39 |  | - | 
| 40 |  | -	@Override | 
| 41 |  | -	public String getCurrencyCode() { | 
| 42 |  | -		return currencyCode; | 
| 43 |  | -	} | 
| 44 |  | - | 
| 45 |  | -	@Override | 
| 46 |  | -	public int getNumericCode() { | 
| 47 |  | -		return numericCode; | 
| 48 |  | -	} | 
| 49 |  | - | 
| 50 |  | -	@Override | 
| 51 |  | -	public int getDefaultFractionDigits() { | 
| 52 |  | -		return defaultFractionDigits; | 
| 53 |  | -	} | 
| 54 |  | -	 | 
| 55 |  | -	@Override | 
| 56 |  | -	public int compareTo(CurrencyUnit o) { | 
| 57 |  | -		return this.currencyCode.compareTo(o.getCurrencyCode()); | 
| 58 |  | -	} | 
| 59 |  | - | 
| 60 |  | -	/* (non-Javadoc) | 
| 61 |  | -	 * @see java.lang.Object#hashCode() | 
| 62 |  | -	 */ | 
| 63 |  | -	@Override | 
| 64 |  | -	public int hashCode() { | 
| 65 |  | -		final int prime = 31; | 
| 66 |  | -		int result = 1; | 
| 67 |  | -		result = prime * result | 
| 68 |  | -				+ ((currencyCode == null) ? 0 : currencyCode.hashCode()); | 
| 69 |  | -		return result; | 
| 70 |  | -	} | 
| 71 |  | - | 
| 72 |  | -	/* (non-Javadoc) | 
| 73 |  | -	 * @see java.lang.Object#equals(java.lang.Object) | 
| 74 |  | -	 */ | 
| 75 |  | -	@Override | 
| 76 |  | -	public boolean equals(Object obj) { | 
| 77 |  | -		if (this == obj) | 
| 78 |  | -			return true; | 
| 79 |  | -		if (obj == null) | 
| 80 |  | -			return false; | 
| 81 |  | -		if (getClass() != obj.getClass()) | 
| 82 |  | -			return false; | 
| 83 |  | -		BuildableCurrencyUnit other = (BuildableCurrencyUnit) obj; | 
| 84 |  | -		if (currencyCode == null) { | 
| 85 |  | -			if (other.currencyCode != null) | 
| 86 |  | -				return false; | 
| 87 |  | -		} else if (!currencyCode.equals(other.currencyCode)) | 
| 88 |  | -			return false; | 
| 89 |  | -		return true; | 
| 90 |  | -	} | 
| 91 |  | - | 
| 92 |  | -	/* (non-Javadoc) | 
| 93 |  | -	 * @see java.lang.Object#toString() | 
| 94 |  | -	 */ | 
| 95 |  | -	@Override | 
| 96 |  | -	public String toString() { | 
| 97 |  | -		return "BuildableCurrencyUnit [currencyCode=" + currencyCode | 
| 98 |  | -				+ ", numericCode=" + numericCode + ", defaultFractionDigits=" | 
| 99 |  | -				+ defaultFractionDigits + "]"; | 
| 100 |  | -	} | 
| 101 |  | - | 
| 102 |  | - | 
| 103 |  | - | 
| 104 |  | -	public static final class Builder { | 
| 105 |  | -		private String currencyCode; | 
| 106 |  | -		private int numericCode = -1; | 
| 107 |  | -		private int defaultFractionDigits = 2; | 
| 108 |  | - | 
| 109 |  | -		public Builder(String currencyCode) { | 
| 110 |  | -			Objects.requireNonNull(currencyCode, "currencyCode required"); | 
| 111 |  | -			this.currencyCode = currencyCode; | 
| 112 |  | -		} | 
| 113 |  | - | 
| 114 |  | -		public Builder setCurrencyCode(String currencyCode) { | 
| 115 |  | -			Objects.requireNonNull(currencyCode, "currencyCode required"); | 
| 116 |  | -			this.currencyCode = currencyCode; | 
| 117 |  | -			return this; | 
| 118 |  | -		} | 
| 119 |  | - | 
| 120 |  | -		public Builder setNumericCode(int numericCode) { | 
| 121 |  | -			if (numericCode < -1) { | 
| 122 |  | -				throw new IllegalArgumentException("numericCode must be >= -1"); | 
| 123 |  | -			} | 
| 124 |  | -			this.numericCode = numericCode; | 
| 125 |  | -			return this; | 
| 126 |  | -		} | 
| 127 |  | - | 
| 128 |  | -		public Builder setDefaultFractionDigits(int defaultFractionDigits) { | 
| 129 |  | -			if (defaultFractionDigits < 0) { | 
| 130 |  | -				throw new IllegalArgumentException( | 
| 131 |  | -						"defaultFractionDigits must be >= 0"); | 
| 132 |  | -			} | 
| 133 |  | -			this.defaultFractionDigits = defaultFractionDigits; | 
| 134 |  | -			return this; | 
| 135 |  | -		} | 
| 136 |  | - | 
| 137 |  | -		public BuildableCurrencyUnit create() { | 
| 138 |  | -			return build(false); | 
| 139 |  | -		} | 
| 140 |  | -		 | 
| 141 |  | -		public BuildableCurrencyUnit build(boolean register) { | 
| 142 |  | -			BuildableCurrencyUnit cu = new BuildableCurrencyUnit(this); | 
| 143 |  | -			if(register){ | 
| 144 |  | -				ConfigurableCurrencyUnitProvider.registerCurrencyUnit(cu); | 
| 145 |  | -			} | 
| 146 |  | -			return cu; | 
| 147 |  | -		} | 
| 148 |  | -		 | 
| 149 |  | -		public BuildableCurrencyUnit build(boolean register, Locale locale) { | 
| 150 |  | -			BuildableCurrencyUnit cu = new BuildableCurrencyUnit(this); | 
| 151 |  | -			if(register){ | 
| 152 |  | -				ConfigurableCurrencyUnitProvider.registerCurrencyUnit(cu); | 
| 153 |  | -				ConfigurableCurrencyUnitProvider.registerCurrencyUnit(cu, locale); | 
| 154 |  | -			} | 
| 155 |  | -			return cu; | 
| 156 |  | -		} | 
| 157 |  | -	} | 
|  | 20 | +/** | 
|  | 21 | + * Implementation of {@link javax.money.CurrencyUnit} that allows to create new instances using a fluent API. | 
|  | 22 | + * Instances created also can be added to the {@link org.javamoney.moneta.internal.ConfigurableCurrencyUnitProvider} | 
|  | 23 | + * singleton, which publishes the instances, so they are visible from the {@link javax.money.MonetaryCurrencies} | 
|  | 24 | + * singleton. | 
|  | 25 | + */ | 
|  | 26 | +public final class BuildableCurrencyUnit implements CurrencyUnit, Comparable<CurrencyUnit>{ | 
|  | 27 | + | 
|  | 28 | +    /** | 
|  | 29 | +     * The uniqie currency code. | 
|  | 30 | +     */ | 
|  | 31 | +    private String currencyCode; | 
|  | 32 | +    /** | 
|  | 33 | +     * The (optional) numeric code. | 
|  | 34 | +     */ | 
|  | 35 | +    private int numericCode; | 
|  | 36 | +    /** | 
|  | 37 | +     * The default fraction digits. | 
|  | 38 | +     */ | 
|  | 39 | +    private int defaultFractionDigits; | 
|  | 40 | + | 
|  | 41 | +    /** | 
|  | 42 | +     * Constructor, called from the Builder. | 
|  | 43 | +     * | 
|  | 44 | +     * @param builder the builder, never null. | 
|  | 45 | +     */ | 
|  | 46 | +    private BuildableCurrencyUnit(Builder builder){ | 
|  | 47 | +        Objects.requireNonNull(builder.currencyCode, "currencyCode required"); | 
|  | 48 | +        if(builder.numericCode < -1){ | 
|  | 49 | +            throw new MonetaryException("numericCode must be >= -1"); | 
|  | 50 | +        } | 
|  | 51 | +        if(builder.defaultFractionDigits < 0){ | 
|  | 52 | +            throw new MonetaryException("defaultFractionDigits must be >= 0"); | 
|  | 53 | +        } | 
|  | 54 | +        this.defaultFractionDigits = builder.defaultFractionDigits; | 
|  | 55 | +        this.numericCode = builder.numericCode; | 
|  | 56 | +        this.currencyCode = builder.currencyCode; | 
|  | 57 | +    } | 
|  | 58 | + | 
|  | 59 | +    @Override | 
|  | 60 | +    public String getCurrencyCode(){ | 
|  | 61 | +        return currencyCode; | 
|  | 62 | +    } | 
|  | 63 | + | 
|  | 64 | +    @Override | 
|  | 65 | +    public int getNumericCode(){ | 
|  | 66 | +        return numericCode; | 
|  | 67 | +    } | 
|  | 68 | + | 
|  | 69 | +    @Override | 
|  | 70 | +    public int getDefaultFractionDigits(){ | 
|  | 71 | +        return defaultFractionDigits; | 
|  | 72 | +    } | 
|  | 73 | + | 
|  | 74 | +    @Override | 
|  | 75 | +    public int compareTo(CurrencyUnit o){ | 
|  | 76 | +        return this.currencyCode.compareTo(o.getCurrencyCode()); | 
|  | 77 | +    } | 
|  | 78 | + | 
|  | 79 | +    /* (non-Javadoc) | 
|  | 80 | +     * @see java.lang.Object#hashCode() | 
|  | 81 | +     */ | 
|  | 82 | +    @Override | 
|  | 83 | +    public int hashCode(){ | 
|  | 84 | +        final int prime = 31; | 
|  | 85 | +        int result = 1; | 
|  | 86 | +        result = prime * result + ((currencyCode == null) ? 0 : currencyCode.hashCode()); | 
|  | 87 | +        return result; | 
|  | 88 | +    } | 
|  | 89 | + | 
|  | 90 | +    /* (non-Javadoc) | 
|  | 91 | +     * @see java.lang.Object#equals(java.lang.Object) | 
|  | 92 | +     */ | 
|  | 93 | +    @Override | 
|  | 94 | +    public boolean equals(Object obj){ | 
|  | 95 | +        if(this == obj){ | 
|  | 96 | +            return true; | 
|  | 97 | +        } | 
|  | 98 | +        if(obj == null){ | 
|  | 99 | +            return false; | 
|  | 100 | +        } | 
|  | 101 | +        if(getClass() != obj.getClass()){ | 
|  | 102 | +            return false; | 
|  | 103 | +        } | 
|  | 104 | +        BuildableCurrencyUnit other = (BuildableCurrencyUnit) obj; | 
|  | 105 | +        if(currencyCode == null){ | 
|  | 106 | +            if(other.currencyCode != null){ | 
|  | 107 | +                return false; | 
|  | 108 | +            } | 
|  | 109 | +        }else if(!currencyCode.equals(other.currencyCode)){ | 
|  | 110 | +            return false; | 
|  | 111 | +        } | 
|  | 112 | +        return true; | 
|  | 113 | +    } | 
|  | 114 | + | 
|  | 115 | +    /* (non-Javadoc) | 
|  | 116 | +     * @see java.lang.Object#toString() | 
|  | 117 | +     */ | 
|  | 118 | +    @Override | 
|  | 119 | +    public String toString(){ | 
|  | 120 | +        return "BuildableCurrencyUnit [currencyCode=" + currencyCode + ", numericCode=" + numericCode + | 
|  | 121 | +                ", defaultFractionDigits=" + defaultFractionDigits + "]"; | 
|  | 122 | +    } | 
|  | 123 | + | 
|  | 124 | + | 
|  | 125 | +    /** | 
|  | 126 | +     * Builder for constructing new instances o{@link org.javamoney.moneta.BuildableCurrencyUnit} using a fluent | 
|  | 127 | +     * API. | 
|  | 128 | +     */ | 
|  | 129 | +    public static final class Builder{ | 
|  | 130 | +        /** | 
|  | 131 | +         * The currency code. | 
|  | 132 | +         */ | 
|  | 133 | +        private String currencyCode; | 
|  | 134 | +        /** | 
|  | 135 | +         * The (optional) numeric code. | 
|  | 136 | +         */ | 
|  | 137 | +        private int numericCode = -1; | 
|  | 138 | +        /** | 
|  | 139 | +         * The default fraction digits. | 
|  | 140 | +         */ | 
|  | 141 | +        private int defaultFractionDigits = 2; | 
|  | 142 | + | 
|  | 143 | +        /** | 
|  | 144 | +         * Creats a new Builder. | 
|  | 145 | +         * | 
|  | 146 | +         * @param currencyCode the (unique) and identifying currency code, not null. | 
|  | 147 | +         */ | 
|  | 148 | +        public Builder(String currencyCode){ | 
|  | 149 | +            Objects.requireNonNull(currencyCode, "currencyCode required"); | 
|  | 150 | +            this.currencyCode = currencyCode; | 
|  | 151 | +        } | 
|  | 152 | + | 
|  | 153 | +        /** | 
|  | 154 | +         * Allows to set the currenc< code, for creating multiple instances, using one Builder. | 
|  | 155 | +         * | 
|  | 156 | +         * @param currencyCode the (unique) and identifying currency code, not null. | 
|  | 157 | +         * @return the Builder, for chaining. | 
|  | 158 | +         * @see javax.money.CurrencyUnit#getCurrencyCode() | 
|  | 159 | +         */ | 
|  | 160 | +        public Builder setCurrencyCode(String currencyCode){ | 
|  | 161 | +            Objects.requireNonNull(currencyCode, "currencyCode required"); | 
|  | 162 | +            this.currencyCode = currencyCode; | 
|  | 163 | +            return this; | 
|  | 164 | +        } | 
|  | 165 | + | 
|  | 166 | +        /** | 
|  | 167 | +         * Set the numeric code (optional). | 
|  | 168 | +         * | 
|  | 169 | +         * @param numericCode The numeric currency code, >= -1. .1 hereby means <i>undefined</i>. | 
|  | 170 | +         * @return the Builder, for chaining. | 
|  | 171 | +         * @see javax.money.CurrencyUnit#getNumericCode() | 
|  | 172 | +         */ | 
|  | 173 | +        public Builder setNumericCode(int numericCode){ | 
|  | 174 | +            if(numericCode < -1){ | 
|  | 175 | +                throw new IllegalArgumentException("numericCode must be >= -1"); | 
|  | 176 | +            } | 
|  | 177 | +            this.numericCode = numericCode; | 
|  | 178 | +            return this; | 
|  | 179 | +        } | 
|  | 180 | + | 
|  | 181 | +        /** | 
|  | 182 | +         * Set the default fraction digits. | 
|  | 183 | +         * | 
|  | 184 | +         * @param defaultFractionDigits the default fraction digits, >= 0. | 
|  | 185 | +         * @return | 
|  | 186 | +         * @see javax.money.CurrencyUnit#getDefaultFractionDigits() | 
|  | 187 | +         */ | 
|  | 188 | +        public Builder setDefaultFractionDigits(int defaultFractionDigits){ | 
|  | 189 | +            if(defaultFractionDigits < 0){ | 
|  | 190 | +                throw new IllegalArgumentException("defaultFractionDigits must be >= 0"); | 
|  | 191 | +            } | 
|  | 192 | +            this.defaultFractionDigits = defaultFractionDigits; | 
|  | 193 | +            return this; | 
|  | 194 | +        } | 
|  | 195 | + | 
|  | 196 | +        /** | 
|  | 197 | +         * Creates a new instance of {@link org.javamoney.moneta.BuildableCurrencyUnit}. | 
|  | 198 | +         * | 
|  | 199 | +         * @return the new CurrencyUnit instance. | 
|  | 200 | +         * @throws MonetaryException, if creation fails | 
|  | 201 | +         */ | 
|  | 202 | +        public BuildableCurrencyUnit create(){ | 
|  | 203 | +            return create(false); | 
|  | 204 | +        } | 
|  | 205 | + | 
|  | 206 | +        /** | 
|  | 207 | +         * Creates a new instance of {@link org.javamoney.moneta.BuildableCurrencyUnit} and publishes it so it is | 
|  | 208 | +         * accessible from the {@code MonetaryCurrencies} singleton. | 
|  | 209 | +         * | 
|  | 210 | +         * @param register if {@code true} the instance created is published so it is accessible from | 
|  | 211 | +         *                 the {@code MonetaryCurrencies} singleton. | 
|  | 212 | +         * @return the new CurrencyUnit instance. | 
|  | 213 | +         * @see javax.money.MonetaryCurrencies#getCurrency(String) | 
|  | 214 | +         */ | 
|  | 215 | +        public BuildableCurrencyUnit create(boolean register){ | 
|  | 216 | +            BuildableCurrencyUnit cu = new BuildableCurrencyUnit(this); | 
|  | 217 | +            if(register){ | 
|  | 218 | +                ConfigurableCurrencyUnitProvider.registerCurrencyUnit(cu); | 
|  | 219 | +            } | 
|  | 220 | +            return cu; | 
|  | 221 | +        } | 
|  | 222 | + | 
|  | 223 | +        /** | 
|  | 224 | +         * Creates a new instance of {@link org.javamoney.moneta.BuildableCurrencyUnit} and publishes it so it is | 
|  | 225 | +         * accessible from the {@code MonetaryCurrencies} singleton. | 
|  | 226 | +         * | 
|  | 227 | +         * @param register if {@code true} the instance created is published so it is accessible from | 
|  | 228 | +         *                 the {@code MonetaryCurrencies} singleton. | 
|  | 229 | +         * @param locale   country Locale for making the currency for the given country. | 
|  | 230 | +         * @return the new CurrencyUnit instance. | 
|  | 231 | +         * @see javax.money.MonetaryCurrencies#getCurrency(String) | 
|  | 232 | +         * @see javax.money.MonetaryCurrencies#getCurrency(java.util.Locale) | 
|  | 233 | +         */ | 
|  | 234 | +        public BuildableCurrencyUnit create(boolean register, Locale locale){ | 
|  | 235 | +            BuildableCurrencyUnit cu = new BuildableCurrencyUnit(this); | 
|  | 236 | +            if(register){ | 
|  | 237 | +                ConfigurableCurrencyUnitProvider.registerCurrencyUnit(cu); | 
|  | 238 | +                ConfigurableCurrencyUnitProvider.registerCurrencyUnit(cu, locale); | 
|  | 239 | +            } | 
|  | 240 | +            return cu; | 
|  | 241 | +        } | 
|  | 242 | +    } | 
| 158 | 243 | 
 | 
| 159 | 244 | } | 
0 commit comments