1515 */
1616package org .javamoney .moneta ;
1717
18+ import org .javamoney .moneta .ToStringMonetaryAmountFormat .ToStringMonetaryAmountFormatStyle ;
1819import org .javamoney .moneta .internal .FastMoneyAmountBuilder ;
1920import org .javamoney .moneta .spi .DefaultNumberValue ;
2021import org .javamoney .moneta .spi .MonetaryConfig ;
2122import org .javamoney .moneta .spi .MoneyUtils ;
2223
2324import javax .money .*;
25+ import javax .money .format .MonetaryAmountFormat ;
26+
2427import java .io .Serializable ;
2528import java .math .BigDecimal ;
2629import java .util .Objects ;
@@ -237,7 +240,8 @@ public static FastMoney of(Number number, String currencyCode){
237240 /*
238241 * @see java.lang.Comparable#compareTo(java.lang.Object)
239242 */
240- public int compareTo (MonetaryAmount o ){
243+ @ Override
244+ public int compareTo (MonetaryAmount o ){
241245 Objects .requireNonNull (o );
242246 int compare = getCurrency ().getCurrencyCode ().compareTo (o .getCurrency ().getCurrencyCode ());
243247 if (compare == 0 ){
@@ -276,7 +280,8 @@ public boolean equals(Object obj){
276280 * (non-Javadoc)
277281 * @see javax.money.MonetaryAmount#abs()
278282 */
279- public FastMoney abs (){
283+ @ Override
284+ public FastMoney abs (){
280285 if (this .isPositiveOrZero ()){
281286 return this ;
282287 }
@@ -289,7 +294,8 @@ public FastMoney abs(){
289294 * (non-Javadoc)
290295 * @see javax.money.MonetaryAmount#add(javax.money.MonetaryAmount)
291296 */
292- public FastMoney add (MonetaryAmount amount ){
297+ @ Override
298+ public FastMoney add (MonetaryAmount amount ){
293299 checkAmountParameter (amount );
294300 if (amount .isZero ()){
295301 return this ;
@@ -313,7 +319,8 @@ private void checkAmountParameter(MonetaryAmount amount){
313319 * (non-Javadoc)
314320 * @see javax.money.MonetaryAmount#divide(java.lang.Number)
315321 */
316- public FastMoney divide (Number divisor ){
322+ @ Override
323+ public FastMoney divide (Number divisor ){
317324 checkNumber (divisor );
318325 if (isOne (divisor )){
319326 return this ;
@@ -325,7 +332,8 @@ public FastMoney divide(Number divisor){
325332 * (non-Javadoc)
326333 * @see javax.money.MonetaryAmount#divideAndRemainder(java.lang.Number)
327334 */
328- public FastMoney [] divideAndRemainder (Number divisor ){
335+ @ Override
336+ public FastMoney [] divideAndRemainder (Number divisor ){
329337 checkNumber (divisor );
330338 if (isOne (divisor )){
331339 return new FastMoney []{this , FastMoney .of (0 , getCurrency ())};
@@ -339,7 +347,8 @@ public FastMoney[] divideAndRemainder(Number divisor){
339347 * (non-Javadoc)
340348 * @see javax.money.MonetaryAmount#divideToIntegralValue(java.lang.Number)
341349 */
342- public FastMoney divideToIntegralValue (Number divisor ){
350+ @ Override
351+ public FastMoney divideToIntegralValue (Number divisor ){
343352 checkNumber (divisor );
344353 if (isOne (divisor )){
345354 return this ;
@@ -348,7 +357,8 @@ public FastMoney divideToIntegralValue(Number divisor){
348357 return new FastMoney (getBigDecimal ().divideToIntegralValue (div ), getCurrency (), false );
349358 }
350359
351- public FastMoney multiply (Number multiplicand ){
360+ @ Override
361+ public FastMoney multiply (Number multiplicand ){
352362 checkNumber (multiplicand );
353363 if (isOne (multiplicand )){
354364 return this ;
@@ -363,15 +373,17 @@ public FastMoney multiply(Number multiplicand){
363373 * (non-Javadoc)
364374 * @see javax.money.MonetaryAmount#negate()
365375 */
366- public FastMoney negate (){
376+ @ Override
377+ public FastMoney negate (){
367378 return new FastMoney (this .number * -1 , getCurrency ());
368379 }
369380
370381 /*
371382 * (non-Javadoc)
372383 * @see javax.money.MonetaryAmount#plus()
373384 */
374- public FastMoney plus (){
385+ @ Override
386+ public FastMoney plus (){
375387 if (this .number >= 0 ){
376388 return this ;
377389 }
@@ -382,7 +394,8 @@ public FastMoney plus(){
382394 * (non-Javadoc)
383395 * @see javax.money.MonetaryAmount#subtract(javax.money.MonetaryAmount)
384396 */
385- public FastMoney subtract (MonetaryAmount subtrahend ){
397+ @ Override
398+ public FastMoney subtract (MonetaryAmount subtrahend ){
386399 checkAmountParameter (subtrahend );
387400 if (subtrahend .isZero ()){
388401 return this ;
@@ -395,7 +408,8 @@ public FastMoney subtract(MonetaryAmount subtrahend){
395408 * (non-Javadoc)
396409 * @see javax.money.MonetaryAmount#remainder(java.lang.Number)
397410 */
398- public FastMoney remainder (Number divisor ){
411+ @ Override
412+ public FastMoney remainder (Number divisor ){
399413 checkNumber (divisor );
400414 if (isOne (divisor )){
401415 return new FastMoney (0 , getCurrency ());
@@ -419,47 +433,53 @@ private boolean isOne(Number number){
419433 * (non-Javadoc)
420434 * @see javax.money.MonetaryAmount#scaleByPowerOfTen(int)
421435 */
422- public FastMoney scaleByPowerOfTen (int n ){
436+ @ Override
437+ public FastMoney scaleByPowerOfTen (int n ){
423438 return new FastMoney (getNumber ().numberValue (BigDecimal .class ).scaleByPowerOfTen (n ), getCurrency (), true );
424439 }
425440
426441 /*
427442 * (non-Javadoc)
428443 * @see javax.money.MonetaryAmount#isZero()
429444 */
430- public boolean isZero (){
445+ @ Override
446+ public boolean isZero (){
431447 return this .number == 0L ;
432448 }
433449
434450 /*
435451 * (non-Javadoc)
436452 * @see javax.money.MonetaryAmount#isPositive()
437453 */
438- public boolean isPositive (){
454+ @ Override
455+ public boolean isPositive (){
439456 return this .number > 0L ;
440457 }
441458
442459 /*
443460 * (non-Javadoc)
444461 * @see javax.money.MonetaryAmount#isPositiveOrZero()
445462 */
446- public boolean isPositiveOrZero (){
463+ @ Override
464+ public boolean isPositiveOrZero (){
447465 return this .number >= 0L ;
448466 }
449467
450468 /*
451469 * (non-Javadoc)
452470 * @see javax.money.MonetaryAmount#isNegative()
453471 */
454- public boolean isNegative (){
472+ @ Override
473+ public boolean isNegative (){
455474 return this .number < 0L ;
456475 }
457476
458477 /*
459478 * (non-Javadoc)
460479 * @see javax.money.MonetaryAmount#isNegativeOrZero()
461480 */
462- public boolean isNegativeOrZero (){
481+ @ Override
482+ public boolean isNegativeOrZero (){
463483 return this .number <= 0L ;
464484 }
465485
@@ -484,7 +504,8 @@ public int getPrecision(){
484504 * @see javax.money.MonetaryAmount#signum()
485505 */
486506
487- public int signum (){
507+ @ Override
508+ public int signum (){
488509 if (this .number < 0 ){
489510 return -1 ;
490511 }
@@ -498,7 +519,8 @@ public int signum(){
498519 * (non-Javadoc)
499520 * @see javax.money.MonetaryAmount#lessThan(javax.money.MonetaryAmount)
500521 */
501- public boolean isLessThan (MonetaryAmount amount ){
522+ @ Override
523+ public boolean isLessThan (MonetaryAmount amount ){
502524 checkAmountParameter (amount );
503525 return getBigDecimal ().compareTo (amount .getNumber ().numberValue (BigDecimal .class )) < 0 ;
504526 }
@@ -516,7 +538,8 @@ public boolean isLessThan(Number number){
516538 * (non-Javadoc)
517539 * @see javax.money.MonetaryAmount#lessThanOrEqualTo(javax.money.MonetaryAmount)
518540 */
519- public boolean isLessThanOrEqualTo (MonetaryAmount amount ){
541+ @ Override
542+ public boolean isLessThanOrEqualTo (MonetaryAmount amount ){
520543 checkAmountParameter (amount );
521544 return getBigDecimal ().compareTo (amount .getNumber ().numberValue (BigDecimal .class )) <= 0 ;
522545 }
@@ -534,7 +557,8 @@ public boolean isLessThanOrEqualTo(Number number){
534557 * (non-Javadoc)
535558 * @see javax.money.MonetaryAmount#greaterThan(javax.money.MonetaryAmount)
536559 */
537- public boolean isGreaterThan (MonetaryAmount amount ){
560+ @ Override
561+ public boolean isGreaterThan (MonetaryAmount amount ){
538562 checkAmountParameter (amount );
539563 return getBigDecimal ().compareTo (amount .getNumber ().numberValue (BigDecimal .class )) > 0 ;
540564 }
@@ -552,7 +576,8 @@ public boolean isGreaterThan(Number number){
552576 * (non-Javadoc)
553577 * @see javax.money.MonetaryAmount#greaterThanOrEqualTo(javax.money.MonetaryAmount ) #see
554578 */
555- public boolean isGreaterThanOrEqualTo (MonetaryAmount amount ){
579+ @ Override
580+ public boolean isGreaterThanOrEqualTo (MonetaryAmount amount ){
556581 checkAmountParameter (amount );
557582 return getBigDecimal ().compareTo (amount .getNumber ().numberValue (BigDecimal .class )) >= 0 ;
558583 }
@@ -570,7 +595,8 @@ public boolean isGreaterThanOrEqualTo(Number number){
570595 * (non-Javadoc)
571596 * @see javax.money.MonetaryAmount#isEqualTo(javax.money.MonetaryAmount)
572597 */
573- public boolean isEqualTo (MonetaryAmount amount ){
598+ @ Override
599+ public boolean isEqualTo (MonetaryAmount amount ){
574600 checkAmountParameter (amount );
575601 return getBigDecimal ().compareTo (amount .getNumber ().numberValue (BigDecimal .class )) == 0 ;
576602 }
@@ -600,12 +626,6 @@ public NumberValue getNumber(){
600626 return new DefaultNumberValue (getBigDecimal ());
601627 }
602628
603- // Static Factory Methods
604-
605- /*
606- * (non-Javadoc)
607- * @see java.lang.Object#toString()
608- */
609629 @ Override
610630 public String toString (){
611631 return currency .toString () + ' ' + getBigDecimal ();
@@ -678,12 +698,36 @@ public <R> R query(MonetaryQuery<R> query){
678698 }
679699
680700 public static FastMoney from (MonetaryAmount amount ){
681- if (FastMoney .class == amount . getClass ()) {
682- return ( FastMoney ) amount ;
701+ if (FastMoney .class . isInstance ( amount )) {
702+ return FastMoney . class . cast ( amount ) ;
683703 }
684704 return new FastMoney (amount .getNumber (), amount .getCurrency (), false );
685705 }
686706
707+ /**
708+ * Obtains an instance of FastMoney from a text string such as 'EUR 25.25'.
709+ * @param text the text to parse not null
710+ * @return FastMoney instance
711+ * @throws NullPointerException
712+ * @throws NumberFormatException
713+ * @throws UnknownCurrencyException
714+ */
715+ public static FastMoney parse (CharSequence text ) {
716+ return parse (text , DEFAULT_FORMATTER );
717+ }
718+ /**
719+ * Obtains an instance of FastMoney from a text using specific formatter.
720+ * @param text the text to parse not null
721+ * @param formatter the formatter to use not null
722+ * @return FastMoney instance
723+ */
724+ public static FastMoney parse (CharSequence text , MonetaryAmountFormat formatter ) {
725+ return from (formatter .parse (text ));
726+ }
727+
728+ private static ToStringMonetaryAmountFormat DEFAULT_FORMATTER = ToStringMonetaryAmountFormat
729+ .of (ToStringMonetaryAmountFormatStyle .FAST_MONEY );
730+
687731 private BigDecimal getBigDecimal (){
688732 return BigDecimal .valueOf (this .number ).movePointLeft (SCALE );
689733 }
0 commit comments